1
0
mirror of https://github.com/flextype/flextype.git synced 2025-08-07 21:56:33 +02:00

feat(directives): add @php directive

This commit is contained in:
Awilum
2022-05-21 18:10:02 +03:00
parent 0925cc49a8
commit 9318fe6255
4 changed files with 63 additions and 2 deletions

View File

@@ -0,0 +1,38 @@
<?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: @php
emitter()->addListener('onEntriesFetchSingleField', static function (): void {
if (! registry()->get('flextype.settings.entries.directives.php.enabled')) {
return;
}
$field = entries()->registry()->get('methods.fetch.field');
if (is_string($field['value'])) {
if (strings($field['value'])->contains('@php')) {
ob_start();
eval(strings($field['value'])->replace('@php', '')->trim()->toString());
$field['value'] = ob_get_clean();
}
}
entries()->registry()->set('methods.fetch.field.key', $field['key']);
entries()->registry()->set('methods.fetch.field.value', $field['value']);
});

View File

@@ -104,6 +104,9 @@ entries:
textile:
enabled: true
path: "/src/flextype/core/Entries/Directives/TextileDirective.php"
php:
enabled: true
path: "/src/flextype/core/Entries/Directives/PhpDirective.php"
macros:
debug: false
vars:
@@ -611,7 +614,7 @@ parsers:
uuid:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/UuidShortcode.php"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

View File

@@ -100,6 +100,9 @@ entries:
textile:
enabled: true
path: "/src/flextype/core/Entries/Directives/TextileDirective.php"
php:
enabled: true
path: "/src/flextype/core/Entries/Directives/PhpDirective.php"
macros:
debug: false
vars:
@@ -600,7 +603,7 @@ parsers:
uuid:
enabled: true
path: "/src/flextype/core/Parsers/Shortcodes/UuidShortcode.php"
# CORS
#
# CORS (Cross-origin resource sharing) allows JavaScript web apps to make HTTP requests to other domains.

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('php directive', function () {
entries()->create('type-php', ['title' => '@php echo "Foo";']);
$this->assertEquals('Foo', entries()->fetch('type-php')['title']);
});