diff --git a/composer.json b/composer.json index 0e6ee17d..1f53e546 100644 --- a/composer.json +++ b/composer.json @@ -29,6 +29,7 @@ "symfony/http-foundation": "^3.3", "symfony/psr-http-message-bridge": "^1.0", "twbs/bootstrap": "^3.3", + "twig/extensions": "^1.5", "twig/twig": "^2.5", "zendframework/zend-diactoros": "^1.7" }, diff --git a/config/app.php b/config/app.php index 19118f1d..e309abe4 100644 --- a/config/app.php +++ b/config/app.php @@ -10,13 +10,13 @@ return [ \Engelsystem\Config\ConfigServiceProvider::class, \Engelsystem\Http\UrlGeneratorServiceProvider::class, \Engelsystem\Renderer\RendererServiceProvider::class, - \Engelsystem\Renderer\TwigServiceProvider::class, \Engelsystem\Database\DatabaseServiceProvider::class, \Engelsystem\Http\RequestServiceProvider::class, \Engelsystem\Http\SessionServiceProvider::class, \Engelsystem\Helpers\TranslationServiceProvider::class, \Engelsystem\Http\ResponseServiceProvider::class, \Engelsystem\Http\Psr7ServiceProvider::class, + \Engelsystem\Renderer\TwigServiceProvider::class, \Engelsystem\Middleware\RouteDispatcherServiceProvider::class, \Engelsystem\Middleware\RequestHandlerServiceProvider::class, ], diff --git a/src/Renderer/Twig/Extensions/Translation.php b/src/Renderer/Twig/Extensions/Translation.php new file mode 100644 index 00000000..63f9800e --- /dev/null +++ b/src/Renderer/Twig/Extensions/Translation.php @@ -0,0 +1,57 @@ +translator = $translator; + $this->tokenParser = $tokenParser; + } + + /** + * @return array + */ + public function getFilters() + { + return [ + new TwigFilter('trans', [$this->translator, 'translate']), + ]; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('__', [$this->translator, 'translate']), + ]; + } + + /** + * @return TwigTokenParser[] + */ + public function getTokenParsers() + { + return [$this->tokenParser]; + } +} diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php index 4a5b1de3..0f453989 100644 --- a/src/Renderer/TwigServiceProvider.php +++ b/src/Renderer/TwigServiceProvider.php @@ -6,6 +6,7 @@ use Engelsystem\Container\ServiceProvider; use Engelsystem\Renderer\Twig\Extensions\Config; use Engelsystem\Renderer\Twig\Extensions\Globals; use Engelsystem\Renderer\Twig\Extensions\Session; +use Engelsystem\Renderer\Twig\Extensions\Translation; use Engelsystem\Renderer\Twig\Extensions\Url; use Twig_Environment as Twig; use Twig_LoaderInterface as TwigLoaderInterface; @@ -14,10 +15,11 @@ class TwigServiceProvider extends ServiceProvider { /** @var array */ protected $extensions = [ - 'config' => Config::class, - 'globals' => Globals::class, - 'session' => Session::class, - 'url' => Url::class, + 'config' => Config::class, + 'globals' => Globals::class, + 'session' => Session::class, + 'url' => Url::class, + 'translation' => Translation::class, ]; public function register() diff --git a/templates/errors/default.twig b/templates/errors/default.twig index a04afc4e..5fb8bcbd 100644 --- a/templates/errors/default.twig +++ b/templates/errors/default.twig @@ -1,6 +1,6 @@ {% extends "layouts/app.twig" %} -{% block title %}Error {{ status }}{% endblock %} +{% block title %}{% if status == 404 %}{{ __("Page not found") }}{% else %}Error {{ status }}{% endif %}{% endblock %} {% block content %}
{{ content }}
diff --git a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php index 10f2e69a..e1c5a378 100644 --- a/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php +++ b/tests/Unit/Renderer/Twig/Extensions/ExtensionTest.php @@ -7,6 +7,27 @@ use Twig_Function as TwigFunction; abstract class ExtensionTest extends TestCase { + /** + * Assert that a twig filter was registered + * + * @param string $name + * @param callable $callback + * @param TwigFunction[] $functions + */ + protected function assertFilterExists($name, $callback, $functions) + { + foreach ($functions as $function) { + if ($function->getName() != $name) { + continue; + } + + $this->assertEquals($callback, $function->getCallable()); + return; + } + + $this->fail(sprintf('Filter %s not found', $name)); + } + /** * Assert that a twig function was registered * @@ -45,4 +66,19 @@ abstract class ExtensionTest extends TestCase $this->fail(sprintf('Global %s not found', $name)); } + + /** + * Assert that a token parser was set + * + * @param $tokenParser + * @param $tokenParsers + */ + protected function assertTokenParserExists($tokenParser, $tokenParsers) + { + $this->assertArraySubset( + [$tokenParser], + $tokenParsers, + sprintf('Token parser %s not found', get_class($tokenParser)) + ); + } } diff --git a/tests/Unit/Renderer/Twig/Extensions/TranslationTest.php b/tests/Unit/Renderer/Twig/Extensions/TranslationTest.php new file mode 100644 index 00000000..f1548604 --- /dev/null +++ b/tests/Unit/Renderer/Twig/Extensions/TranslationTest.php @@ -0,0 +1,60 @@ +createMock(Translator::class); + /** @var TranslationTokenParser|MockObject $parser */ + $parser = $this->createMock(TranslationTokenParser::class); + + $extension = new Translation($translator, $parser); + $filters = $extension->getFilters(); + + $this->assertExtensionExists('trans', [$translator, 'translate'], $filters); + } + + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getFunctions + */ + public function testGetFunctions() + { + /** @var Translator|MockObject $translator */ + $translator = $this->createMock(Translator::class); + /** @var TranslationTokenParser|MockObject $parser */ + $parser = $this->createMock(TranslationTokenParser::class); + + $extension = new Translation($translator, $parser); + $functions = $extension->getFunctions(); + + $this->assertExtensionExists('__', [$translator, 'translate'], $functions); + } + + /** + * @covers \Engelsystem\Renderer\Twig\Extensions\Translation::getTokenParsers + */ + public function testGetTokenParsers() + { + /** @var Translator|MockObject $translator */ + $translator = $this->createMock(Translator::class); + /** @var TranslationTokenParser|MockObject $parser */ + $parser = $this->createMock(TranslationTokenParser::class); + + $extension = new Translation($translator, $parser); + $tokenParsers = $extension->getTokenParsers(); + + $this->assertTokenParserExists($parser, $tokenParsers); + } +}