1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 13:46:42 +02:00

feat(directives): add expressions directive

This commit is contained in:
Awilum
2022-05-27 18:33:29 +03:00
parent f3247c618c
commit cf42aaf820
4 changed files with 66 additions and 0 deletions

View File

@@ -0,0 +1,40 @@
<?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.
*/
use Glowy\Arrays\Arrays as Collection;
// Directive: [[ ]]
emitter()->addListener('onEntriesFetchSingleField', static function (): void {
if (! registry()->get('flextype.settings.entries.directives.expressions.enabled')) {
return;
}
if (! registry()->get('flextype.settings.entries.directives.expressions.enabled_globally')) {
return;
}
$field = entries()->registry()->get('methods.fetch.field');
if (is_string($field['value'])) {
$field['value'] = preg_replace_callback('/\[\[ (.*?) \]\]/s', function($matches) {
return expression()->evaluate($matches[1]);
}, $field['value']);
}
entries()->registry()->set('methods.fetch.field.key', $field['key']);
entries()->registry()->set('methods.fetch.field.value', $field['value']);
});

View File

@@ -77,6 +77,10 @@ entries:
vars:
debug: false
directives:
expressions:
enabled: true
enabled_globally: true
path: "/src/flextype/core/Entries/Directives/ExpressionsDirective.php"
shortcodes:
enabled: true
enabled_globally: true
@@ -630,6 +634,7 @@ parsers:
type:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/TypeShortcode.php"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

View File

@@ -73,6 +73,10 @@ entries:
vars:
debug: false
directives:
expressions:
enabled: true
enabled_globally: true
path: "/src/flextype/core/Entries/Directives/ExpressionsDirective.php"
shortcodes:
enabled: true
enabled_globally: true

View File

@@ -0,0 +1,17 @@
<?php
use Flextype\Component\Filesystem\Filesystem;
beforeEach(function() {
filesystem()->directory(PATH['project'] . '/entries')->create();
});
afterEach(function (): void {
filesystem()->directory(PATH['project'] . '/entries')->delete();
});
test('expressions directive', function () {
entries()->create('expressions', ['test' => '[[ 1+1 ]]']);
$this->assertEquals(2, entries()->fetch('expressions')['test']);
});