diff --git a/src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php b/src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php new file mode 100644 index 00000000..b632696e --- /dev/null +++ b/src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php @@ -0,0 +1,37 @@ +shortcodes()->addHandler('filesystem', static function (ShortcodeInterface $s) { + + $varsDelimeter = $s->getParameter('varsDelimeter') ?: '|'; + + if ($s->getParameter('get') != null && registry()->get('flextype.settings.parsers.shortcodes.shortcodes.filesystem.get.enabled') === true) { + + // Get vars + foreach($s->getParameters() as $key => $value) { + $vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : []; + } + + return filesystem()->file($vars[0])->exists() ? filesystem()->file($vars[0])->get() : ''; + } + + return ''; +}); \ No newline at end of file diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index d7b2d76f..f09b1eb4 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -556,7 +556,11 @@ parsers: strings: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php" - + filesystem: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php" + get: + enabled: true # CORS # diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index b03e7970..ce9316f0 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -540,7 +540,9 @@ parsers: strings: enabled: true path: "/src/flextype/core/Parsers/Shortcodes/StringsShortcode.php" - + filesystem: + enabled: true + path: "/src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php" # CORS # # CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains. diff --git a/tests/src/flextype/core/Parsers/Shortcodes/FilesystemShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/FilesystemShortcodeTest.php new file mode 100644 index 00000000..804ee7b5 --- /dev/null +++ b/tests/src/flextype/core/Parsers/Shortcodes/FilesystemShortcodeTest.php @@ -0,0 +1,20 @@ +tempDir = __DIR__ . '/tmp-foo'; + @mkdir($this->tempDir); +}); + +afterEach(function (): void { + $filesystem = filesystem(); + $filesystem->directory($this->tempDir)->delete(); + unset($this->tempDir); +}); + +test('[filesystem] shortcode', function () { + $filesystem = filesystem(); + $filesystem->file($this->tempDir . '/foo.txt')->put('Foo'); + $this->assertEquals("Foo", parsers()->shortcodes()->parse('[filesystem get="' . $this->tempDir .'/foo.txt"]')); +});