diff --git a/src/Resources/config/twig.php b/src/Resources/config/twig.php index 7dcd48fd..7757cd55 100644 --- a/src/Resources/config/twig.php +++ b/src/Resources/config/twig.php @@ -10,15 +10,15 @@ namespace Symfony\Component\DependencyInjection\Loader\Configurator; use Nucleos\MatomoBundle\Twig\Extension\MatomoTwigExtension; -use Symfony\Component\DependencyInjection\Reference; +use Nucleos\MatomoBundle\Twig\Runtime\MatomoRuntime; return static function (ContainerConfigurator $container): void { $container->services() ->set(MatomoTwigExtension::class) ->tag('twig.extension') - ->args([ - new Reference('twig'), - ]) + + ->set(MatomoRuntime::class) + ->tag('twig.runtime') ; }; diff --git a/src/Twig/Extension/MatomoTwigExtension.php b/src/Twig/Extension/MatomoTwigExtension.php index 78d7e4f3..b3439baa 100644 --- a/src/Twig/Extension/MatomoTwigExtension.php +++ b/src/Twig/Extension/MatomoTwigExtension.php @@ -11,50 +11,19 @@ namespace Nucleos\MatomoBundle\Twig\Extension; -use Twig\Environment; -use Twig\Error\LoaderError; -use Twig\Error\RuntimeError; -use Twig\Error\SyntaxError; +use Nucleos\MatomoBundle\Twig\Runtime\MatomoRuntime; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; final class MatomoTwigExtension extends AbstractExtension { - private Environment $environment; - - public function __construct(Environment $environment) - { - $this->environment = $environment; - } - public function getFunctions(): array { return [ - new TwigFunction('matomo_tracker', [$this, 'renderTracker'], [ - 'is_safe' => ['html'], + new TwigFunction('matomo_tracker', [MatomoRuntime::class, 'renderTracker'], [ + 'needs_environment' => true, + 'is_safe' => ['html'], ]), ]; } - - /** - * @param array $options - * - * @throws LoaderError - * @throws RuntimeError - * @throws SyntaxError - */ - public function renderTracker(array $options = []): string - { - $data = array_merge([ - 'site_id' => null, - 'matomo_host' => 'localhost', - 'cookie_domain' => null, - ], $options); - - if (null === $data['site_id']) { - return ''; - } - - return $this->environment->render('@NucleosMatomo/tracker_code.html.twig', $data); - } } diff --git a/src/Twig/Runtime/MatomoRuntime.php b/src/Twig/Runtime/MatomoRuntime.php new file mode 100644 index 00000000..e43e84be --- /dev/null +++ b/src/Twig/Runtime/MatomoRuntime.php @@ -0,0 +1,34 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nucleos\MatomoBundle\Twig\Runtime; + +use Twig\Environment; +use Twig\Extension\RuntimeExtensionInterface; + +final class MatomoRuntime implements RuntimeExtensionInterface +{ + /** + * @param array $options + */ + public function renderTracker(Environment $environment, array $options = []): string + { + $data = array_merge([ + 'site_id' => null, + 'matomo_host' => 'localhost', + 'cookie_domain' => null, + ], $options); + + if (null === $data['site_id']) { + return ''; + } + + return $environment->render('@NucleosMatomo/tracker_code.html.twig', $data); + } +} diff --git a/tests/Twig/Extension/MatomoTwigExtensionTest.php b/tests/Twig/Extension/MatomoTwigExtensionTest.php index 7e318c07..2398cf47 100644 --- a/tests/Twig/Extension/MatomoTwigExtensionTest.php +++ b/tests/Twig/Extension/MatomoTwigExtensionTest.php @@ -12,26 +12,14 @@ namespace Nucleos\MatomoBundle\Tests\Twig\Extension; use Nucleos\MatomoBundle\Twig\Extension\MatomoTwigExtension; -use PHPUnit\Framework\MockObject\MockObject; use PHPUnit\Framework\TestCase; -use Twig\Environment; use Twig\TwigFunction; final class MatomoTwigExtensionTest extends TestCase { - /** - * @var Environment&MockObject - */ - private Environment $environment; - - protected function setUp(): void - { - $this->environment = $this->createMock(Environment::class); - } - public function testGetFunctions(): void { - $extension = new MatomoTwigExtension($this->environment); + $extension = new MatomoTwigExtension(); $functions = $extension->getFunctions(); @@ -39,33 +27,8 @@ public function testGetFunctions(): void foreach ($functions as $function) { static::assertInstanceOf(TwigFunction::class, $function); - static::assertIsCallable($function->getCallable()); + static::assertIsArray($callable = $function->getCallable()); + static::assertTrue(method_exists($callable[0], $callable[1])); } } - - public function testRenderTracker(): void - { - $this->environment->method('render')->with('@NucleosMatomo/tracker_code.html.twig', [ - 'site_id' => 13, - 'matomo_host' => 'localhost', - 'cookie_domain' => null, - ]) - ->willReturn('HTML CONTENT') - ; - - $extension = new MatomoTwigExtension($this->environment); - - static::assertSame('HTML CONTENT', $extension->renderTracker([ - 'site_id' => 13, - ])); - } - - public function testRenderTrackerWithoutSiteId(): void - { - $extension = new MatomoTwigExtension($this->environment); - - $this->environment->expects(static::never())->method('render'); - - static::assertSame('', $extension->renderTracker()); - } } diff --git a/tests/Twig/Runtime/MatomoRuntimeTest.php b/tests/Twig/Runtime/MatomoRuntimeTest.php new file mode 100644 index 00000000..1a3f605b --- /dev/null +++ b/tests/Twig/Runtime/MatomoRuntimeTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Nucleos\MatomoBundle\Tests\Twig\Runtime; + +use Nucleos\MatomoBundle\Twig\Runtime\MatomoRuntime; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Twig\Environment; + +final class MatomoRuntimeTest extends TestCase +{ + /** + * @var Environment&MockObject + */ + private Environment $environment; + + private MatomoRuntime $runtime; + + protected function setUp(): void + { + $this->environment = $this->createMock(Environment::class); + $this->runtime = new MatomoRuntime(); + } + + public function testRenderTracker(): void + { + $this->environment->method('render')->with('@NucleosMatomo/tracker_code.html.twig', [ + 'site_id' => 13, + 'matomo_host' => 'localhost', + 'cookie_domain' => null, + ]) + ->willReturn('HTML CONTENT') + ; + + static::assertSame('HTML CONTENT', $this->runtime->renderTracker($this->environment, [ + 'site_id' => 13, + ])); + } + + public function testRenderTrackerWithoutSiteId(): void + { + $this->environment->expects(static::never())->method('render'); + + static::assertSame('', $this->runtime->renderTracker($this->environment)); + } +}