1
0
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:
Awilum
2022-05-09 16:58:22 +03:00
parent ab1ef9ccbf
commit 09a30caa3e
4 changed files with 65 additions and 2 deletions

View 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 '';
});

View File

@@ -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
#

View File

@@ -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.

View File

@@ -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"]'));
});