Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Setono/twig
Add twig extension
- Loading branch information
Showing
8 changed files
with
93 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,6 @@ | |
"badge": "1.x" | ||
} | ||
}, | ||
"minMsi": 81.25, | ||
"minMsi": 60.87, | ||
"minCoveredMsi": 92.86 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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']]), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters