mirror of
https://github.com/flextype/flextype.git
synced 2025-08-08 06:06:45 +02:00
feat(shortcodes): add filesystem
shortcode
This commit is contained in:
37
src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php
Normal file
37
src/flextype/core/Parsers/Shortcodes/FilesystemShortcode.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
/**
|
||||
* Flextype - Hybrid Content Management System with the freedom of a headless CMS
|
||||
* and with the full functionality of a traditional CMS!
|
||||
*
|
||||
* Copyright (c) Sergey Romanenko (https://awilum.github.io)
|
||||
*
|
||||
* Licensed under The MIT License.
|
||||
*
|
||||
* For full copyright and license information, please see the LICENSE
|
||||
* Redistributions of files must retain the above copyright notice.
|
||||
*/
|
||||
|
||||
namespace Flextype\Parsers\Shortcodes;
|
||||
|
||||
use Thunder\Shortcode\Shortcode\ShortcodeInterface;
|
||||
|
||||
// Shortcode: [filesystem]
|
||||
parsers()->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 '';
|
||||
});
|
@@ -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
|
||||
#
|
||||
|
4
tests/fixtures/settings/settings.yaml
vendored
4
tests/fixtures/settings/settings.yaml
vendored
@@ -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.
|
||||
|
@@ -0,0 +1,20 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
beforeEach(function (): void {
|
||||
$this->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"]'));
|
||||
});
|
Reference in New Issue
Block a user