diff --git a/composer.json b/composer.json index 0e8b835..f75681a 100644 --- a/composer.json +++ b/composer.json @@ -11,6 +11,7 @@ ], "require": { "php": ">=8.1", + "psr/log": "^1.1 || ^2.0 || ^3.0", "setono/editorjs-php": "^1.0", "symfony/config": "^5.4 || ^6.0 || ^7.0", "symfony/dependency-injection": "^5.4 || ^6.0 || ^7.0", diff --git a/infection.json.dist b/infection.json.dist index ab011a7..a62c7ce 100644 --- a/infection.json.dist +++ b/infection.json.dist @@ -11,6 +11,6 @@ "badge": "1.x" } }, - "minMsi": 81.25, + "minMsi": 60.87, "minCoveredMsi": 92.86 } diff --git a/psalm.xml b/psalm.xml index 3079f17..2f6a155 100644 --- a/psalm.xml +++ b/psalm.xml @@ -3,6 +3,10 @@ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://getpsalm.org/schema/config" xsi:schemaLocation="https://getpsalm.org/schema/config vendor/vimeo/psalm/config.xsd" + findUnusedCode="false" + findUnusedVariablesAndParams="false" + findUnusedPsalmSuppress="false" + findUnusedBaselineEntry="false" errorLevel="1" > diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 4d2a7cc..48fd2fb 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -6,5 +6,6 @@ + diff --git a/src/Resources/config/services/twig.xml b/src/Resources/config/services/twig.xml new file mode 100644 index 0000000..d59afce --- /dev/null +++ b/src/Resources/config/services/twig.xml @@ -0,0 +1,21 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/Twig/Extension.php b/src/Twig/Extension.php new file mode 100644 index 0000000..a7fd9db --- /dev/null +++ b/src/Twig/Extension.php @@ -0,0 +1,21 @@ + + */ + public function getFilters(): array + { + return [ + new TwigFilter('editorjs_render', [Runtime::class, 'render'], ['is_safe' => ['html']]), + ]; + } +} diff --git a/src/Twig/Runtime.php b/src/Twig/Runtime.php new file mode 100644 index 0000000..26211e7 --- /dev/null +++ b/src/Twig/Runtime.php @@ -0,0 +1,40 @@ +logger = new NullLogger(); + } + + public function render(string $json): string + { + try { + return $this->renderer->render($this->parser->parse($json)); + } catch (\Throwable $e) { + $this->logger->error($e->getMessage()); + } + + return ''; + } + + public function setLogger(LoggerInterface $logger): void + { + $this->logger = $logger; + } +} diff --git a/tests/DependencyInjection/SetonoEditorJSExtensionTest.php b/tests/DependencyInjection/SetonoEditorJSExtensionTest.php index 8d2f73c..3667146 100644 --- a/tests/DependencyInjection/SetonoEditorJSExtensionTest.php +++ b/tests/DependencyInjection/SetonoEditorJSExtensionTest.php @@ -11,6 +11,8 @@ use Setono\EditorJS\Renderer\RendererInterface; use Setono\EditorJSBundle\BlockRenderer\TwigBlockRenderer; use Setono\EditorJSBundle\DependencyInjection\SetonoEditorJSExtension; +use Setono\EditorJSBundle\Twig\Extension; +use Setono\EditorJSBundle\Twig\Runtime; /** * @covers \Setono\EditorJSBundle\DependencyInjection\SetonoEditorJSExtension @@ -37,6 +39,8 @@ public function it_loads_services(): void ['id' => 'setono_editorjs.parser', 'class' => Parser::class], ['id' => RendererInterface::class, 'class' => Renderer::class], ['id' => 'setono_editorjs.renderer', 'class' => Renderer::class], + ['id' => 'setono_editorjs.twig.extension', 'class' => Extension::class, 'tag' => 'twig.extension'], + ['id' => 'setono_editorjs.twig.runtime', 'class' => Runtime::class, 'tag' => 'twig.runtime'], ]; foreach ($services as $service) {