diff --git a/src/flextype/core/Entries/Directives/PhpDirective.php b/src/flextype/core/Entries/Directives/PhpDirective.php new file mode 100644 index 00000000..5cf77a27 --- /dev/null +++ b/src/flextype/core/Entries/Directives/PhpDirective.php @@ -0,0 +1,38 @@ +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']); +}); \ No newline at end of file diff --git a/src/flextype/settings.yaml b/src/flextype/settings.yaml index 158fae7b..a2557d89 100644 --- a/src/flextype/settings.yaml +++ b/src/flextype/settings.yaml @@ -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. diff --git a/tests/fixtures/settings/settings.yaml b/tests/fixtures/settings/settings.yaml index 8e8ee090..458e1190 100644 --- a/tests/fixtures/settings/settings.yaml +++ b/tests/fixtures/settings/settings.yaml @@ -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. diff --git a/tests/src/flextype/core/Entries/Directives/PhpDirectiveTest.php b/tests/src/flextype/core/Entries/Directives/PhpDirectiveTest.php new file mode 100644 index 00000000..eeae6606 --- /dev/null +++ b/tests/src/flextype/core/Entries/Directives/PhpDirectiveTest.php @@ -0,0 +1,17 @@ +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']); +}); \ No newline at end of file