Skip to content

Commit

Permalink
Merge pull request #9 from Setono/twig
Browse files Browse the repository at this point in the history
Add twig extension
  • Loading branch information
loevgaard committed Mar 1, 2024
2 parents a9a2220 + 518713f commit 5ece3b0
Show file tree
Hide file tree
Showing 8 changed files with 93 additions and 1 deletion.
1 change: 1 addition & 0 deletions composer.json
Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion infection.json.dist
Expand Up @@ -11,6 +11,6 @@
"badge": "1.x"
}
},
"minMsi": 81.25,
"minMsi": 60.87,
"minCoveredMsi": 92.86
}
4 changes: 4 additions & 0 deletions psalm.xml
Expand Up @@ -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"
>
<projectFiles>
Expand Down
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Expand Up @@ -6,5 +6,6 @@
<import resource="services/block_renderer.xml"/>
<import resource="services/parser.xml"/>
<import resource="services/renderer.xml"/>
<import resource="services/twig.xml"/>
</imports>
</container>
21 changes: 21 additions & 0 deletions src/Resources/config/services/twig.xml
@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8" ?>

<container xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://symfony.com/schema/dic/services"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
<services>
<service id="setono_editorjs.twig.extension" class="Setono\EditorJSBundle\Twig\Extension">
<tag name="twig.extension"/>
</service>

<service id="setono_editorjs.twig.runtime" class="Setono\EditorJSBundle\Twig\Runtime">
<argument type="service" id="setono_editorjs.parser"/>
<argument type="service" id="setono_editorjs.renderer"/>

<tag name="twig.runtime"/>

<call method="setLogger">
<argument type="service" id="logger"/>
</call>
</service>
</services>
</container>
21 changes: 21 additions & 0 deletions src/Twig/Extension.php
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Setono\EditorJSBundle\Twig;

use Twig\Extension\AbstractExtension;
use Twig\TwigFilter;

final class Extension extends AbstractExtension
{
/**
* @return list<TwigFilter>
*/
public function getFilters(): array
{
return [
new TwigFilter('editorjs_render', [Runtime::class, 'render'], ['is_safe' => ['html']]),
];
}
}
40 changes: 40 additions & 0 deletions src/Twig/Runtime.php
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Setono\EditorJSBundle\Twig;

use Psr\Log\LoggerAwareInterface;
use Psr\Log\LoggerInterface;
use Psr\Log\NullLogger;
use Setono\EditorJS\Parser\ParserInterface;
use Setono\EditorJS\Renderer\RendererInterface;
use Twig\Extension\RuntimeExtensionInterface;

final class Runtime implements RuntimeExtensionInterface, LoggerAwareInterface
{
private LoggerInterface $logger;

public function __construct(
private readonly ParserInterface $parser,
private readonly RendererInterface $renderer,
) {
$this->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;
}
}
4 changes: 4 additions & 0 deletions tests/DependencyInjection/SetonoEditorJSExtensionTest.php
Expand Up @@ -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
Expand All @@ -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) {
Expand Down

0 comments on commit 5ece3b0

Please sign in to comment.