1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-06 05:07:41 +02:00

feat(shortcodes): update [registry] shortcode logic

This commit is contained in:
Awilum
2022-05-09 11:58:49 +03:00
parent 9a0e5414b7
commit a49ae0935c
2 changed files with 21 additions and 10 deletions

View File

@@ -21,11 +21,25 @@ use Thunder\Shortcode\Shortcode\ShortcodeInterface;
use function parsers;
use function registry;
// Shortcode: [registry-get id="STRING" default="STRING"]
parsers()->shortcodes()->addHandler('registry-get', static function (ShortcodeInterface $s) {
// Shortcode: [registry]
parsers()->shortcodes()->addHandler('registry', static function (ShortcodeInterface $s) {
if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.registry.enabled')) {
return '';
}
return registry()->get($s->getParameter('id'), $s->getParameter('default'));
$varsDelimeter = $s->getParameter('varsDelimeter') ?: '|';
if ($s->getParameter('get') != null) {
// Get vars
foreach($s->getParameters() as $key => $value) {
$vars = $value !== null ? strings($value)->contains($varsDelimeter) ? explode($varsDelimeter, $value) : [$value] : [];
}
$result = registry()->get($vars[0], $vars[1] ?? null);
return is_array($result) ? serializers()->json()->encode($result) : $result;
}
return '';
});

View File

@@ -2,12 +2,9 @@
declare(strict_types=1);
test('[registry-get] shortcode', function () {
$this->assertEquals('Flextype',
parsers()->shortcodes()->parse('[registry-get id="flextype.manifest.name"]'));
$this->assertEquals('default-value',
parsers()->shortcodes()->parse('[registry-get id="item-name" default="default-value"]'));
test('[registry] shortcode', function () {
expect(strings(parsers()->shortcodes()->parse('[registry get="flextype.manifest"]'))->isJson())->toBeTrue();
expect(parsers()->shortcodes()->parse('[registry get="flextype.manifest.name"]'))->toBe('Flextype');
expect(parsers()->shortcodes()->parse('[registry get="flextype.manifest.foo|Default"]'))->toBe('Default');
registry()->set('flextype.settings.parsers.shortcodes.shortcodes.registry.enabled', false);
$this->assertEquals('', parsers()->shortcodes()->parse('[registry-get id="item-name" default="default-value"]'));
});