Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add twig extension #9

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
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>
21 changes: 21 additions & 0 deletions src/Twig/Extension.php
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

Check warning on line 15 in src/Twig/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Extension.php#L15

Added line #L15 was not covered by tests
{
return [
new TwigFilter('editorjs_render', [Runtime::class, 'render'], ['is_safe' => ['html']]),
];

Check warning on line 19 in src/Twig/Extension.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Extension.php#L17-L19

Added lines #L17 - L19 were not covered by tests
}
}
40 changes: 40 additions & 0 deletions src/Twig/Runtime.php
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(

Check warning on line 18 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L18

Added line #L18 was not covered by tests
private readonly ParserInterface $parser,
private readonly RendererInterface $renderer,
) {
$this->logger = new NullLogger();

Check warning on line 22 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L22

Added line #L22 was not covered by tests
}

public function render(string $json): string

Check warning on line 25 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L25

Added line #L25 was not covered by tests
{
try {
return $this->renderer->render($this->parser->parse($json));
} catch (\Throwable $e) {
$this->logger->error($e->getMessage());

Check warning on line 30 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L28-L30

Added lines #L28 - L30 were not covered by tests
}

return '';

Check warning on line 33 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L33

Added line #L33 was not covered by tests
}

public function setLogger(LoggerInterface $logger): void

Check warning on line 36 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L36

Added line #L36 was not covered by tests
{
$this->logger = $logger;

Check warning on line 38 in src/Twig/Runtime.php

View check run for this annotation

Codecov / codecov/patch

src/Twig/Runtime.php#L38

Added line #L38 was not covered by tests
}
}
4 changes: 4 additions & 0 deletions tests/DependencyInjection/SetonoEditorJSExtensionTest.php
Original file line number Diff line number Diff line change
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