From 9274fb42b1bfd5ba8068e85df3978f1d8738b881 Mon Sep 17 00:00:00 2001 From: Awilum Date: Fri, 27 May 2022 16:46:24 +0300 Subject: [PATCH] feat(shortcodes): add ability to use php directive with `php` shortcode --- .../core/Parsers/Shortcodes/PhpShortcode.php | 12 ++++++++---- .../core/Parsers/Shortcodes/PhpShortcodeTest.php | 16 +++++++++++++--- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/src/flextype/core/Parsers/Shortcodes/PhpShortcode.php b/src/flextype/core/Parsers/Shortcodes/PhpShortcode.php index a51cf56b..d63e87ee 100644 --- a/src/flextype/core/Parsers/Shortcodes/PhpShortcode.php +++ b/src/flextype/core/Parsers/Shortcodes/PhpShortcode.php @@ -24,8 +24,12 @@ parsers()->shortcodes()->addHandler('php', static function (ShortcodeInterface $ if (! registry()->get('flextype.settings.parsers.shortcodes.shortcodes.php.enabled')) { return ''; } - - ob_start(); - eval($s->getContent()); - return ob_get_clean(); + + if ($s->getContent() != null) { + ob_start(); + eval($s->getContent()); + return ob_get_clean(); + } + + return '@php'; }); \ No newline at end of file diff --git a/tests/src/flextype/core/Parsers/Shortcodes/PhpShortcodeTest.php b/tests/src/flextype/core/Parsers/Shortcodes/PhpShortcodeTest.php index 0543e5d4..5c4556f2 100644 --- a/tests/src/flextype/core/Parsers/Shortcodes/PhpShortcodeTest.php +++ b/tests/src/flextype/core/Parsers/Shortcodes/PhpShortcodeTest.php @@ -2,7 +2,17 @@ declare(strict_types=1); -test('[php] shortcode', function () { - $this->assertEquals("Foo", - parsers()->shortcodes()->parse('(php)echo "Foo";(/php)')); +beforeEach(function() { + filesystem()->directory(PATH['project'] . '/entries')->ensureExists(0755, true); +}); + +afterEach(function () { + filesystem()->directory(PATH['project'] . '/entries')->delete(); +}); + +test('php shortcode', function () { + $this->assertEquals("Foo", parsers()->shortcodes()->parse('(php)echo "Foo";(/php)')); + + expect(entries()->create('bar', ['test' => '(php) echo "Bar";']))->toBeTrue(); + expect(entries()->fetch('bar')['test'])->toBe('Bar'); });