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:
38
src/flextype/core/Entries/Directives/PhpDirective.php
Normal file
38
src/flextype/core/Entries/Directives/PhpDirective.php
Normal 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']);
|
||||
});
|
@@ -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.
|
||||
|
5
tests/fixtures/settings/settings.yaml
vendored
5
tests/fixtures/settings/settings.yaml
vendored
@@ -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.
|
||||
|
@@ -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']);
|
||||
});
|
Reference in New Issue
Block a user