diff options
Diffstat (limited to 'src/Renderer')
-rw-r--r-- | src/Renderer/RendererServiceProvider.php | 2 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Config.php | 31 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Globals.php | 23 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Session.php | 31 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Translation.php | 57 | ||||
-rw-r--r-- | src/Renderer/Twig/Extensions/Url.php | 43 | ||||
-rw-r--r-- | src/Renderer/TwigEngine.php | 41 | ||||
-rw-r--r-- | src/Renderer/TwigLoader.php | 26 | ||||
-rw-r--r-- | src/Renderer/TwigServiceProvider.php | 77 |
9 files changed, 331 insertions, 0 deletions
diff --git a/src/Renderer/RendererServiceProvider.php b/src/Renderer/RendererServiceProvider.php index 3e8d69bc..2e41837b 100644 --- a/src/Renderer/RendererServiceProvider.php +++ b/src/Renderer/RendererServiceProvider.php @@ -24,12 +24,14 @@ class RendererServiceProvider extends ServiceProvider protected function registerRenderer() { $renderer = $this->app->make(Renderer::class); + $this->app->instance(Renderer::class, $renderer); $this->app->instance('renderer', $renderer); } protected function registerHtmlEngine() { $htmlEngine = $this->app->make(HtmlEngine::class); + $this->app->instance(HtmlEngine::class, $htmlEngine); $this->app->instance('renderer.htmlEngine', $htmlEngine); $this->app->tag('renderer.htmlEngine', ['renderer.engine']); } diff --git a/src/Renderer/Twig/Extensions/Config.php b/src/Renderer/Twig/Extensions/Config.php new file mode 100644 index 00000000..dbbe93e7 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Config.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Config\Config as EngelsystemConfig; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Config extends TwigExtension +{ + /** @var EngelsystemConfig */ + protected $config; + + /** + * @param EngelsystemConfig $config + */ + public function __construct(EngelsystemConfig $config) + { + $this->config = $config; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('config', [$this->config, 'get']), + ]; + } +} diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php new file mode 100644 index 00000000..f9bffbc8 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Globals.php @@ -0,0 +1,23 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Twig_Extension as TwigExtension; +use Twig_Extension_GlobalsInterface as GlobalsInterface; + +class Globals extends TwigExtension implements GlobalsInterface +{ + /** + * Returns a list of global variables to add to the existing list. + * + * @return array An array of global variables + */ + public function getGlobals() + { + global $user; + + return [ + 'user' => isset($user) ? $user : [], + ]; + } +} diff --git a/src/Renderer/Twig/Extensions/Session.php b/src/Renderer/Twig/Extensions/Session.php new file mode 100644 index 00000000..4690f701 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Session.php @@ -0,0 +1,31 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Symfony\Component\HttpFoundation\Session\Session as SymfonySession; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Session extends TwigExtension +{ + /** @var SymfonySession */ + protected $session; + + /** + * @param SymfonySession $session + */ + public function __construct(SymfonySession $session) + { + $this->session = $session; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('session_get', [$this->session, 'get']), + ]; + } +} 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 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Helpers\Translator; +use Twig_Extension as TwigExtension; +use Twig_Extensions_TokenParser_Trans as TranslationTokenParser; +use Twig_Filter as TwigFilter; +use Twig_Function as TwigFunction; +use Twig_TokenParserInterface as TwigTokenParser; + +class Translation extends TwigExtension +{ + /** @var Translator */ + protected $translator; + + /** @var TranslationTokenParser */ + protected $tokenParser; + + /** + * @param Translator $translator + * @param TranslationTokenParser $tokenParser + */ + public function __construct(Translator $translator, TranslationTokenParser $tokenParser) + { + $this->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/Twig/Extensions/Url.php b/src/Renderer/Twig/Extensions/Url.php new file mode 100644 index 00000000..62e59782 --- /dev/null +++ b/src/Renderer/Twig/Extensions/Url.php @@ -0,0 +1,43 @@ +<?php + +namespace Engelsystem\Renderer\Twig\Extensions; + +use Engelsystem\Http\UrlGenerator; +use Twig_Extension as TwigExtension; +use Twig_Function as TwigFunction; + +class Url extends TwigExtension +{ + /** @var UrlGenerator */ + protected $urlGenerator; + + /** + * @param UrlGenerator $urlGenerator + */ + public function __construct(UrlGenerator $urlGenerator) + { + $this->urlGenerator = $urlGenerator; + } + + /** + * @return TwigFunction[] + */ + public function getFunctions() + { + return [ + new TwigFunction('url', [$this, 'getUrl']), + ]; + } + + /** + * @param string $path + * @param array $parameters + * @return UrlGenerator|string + */ + public function getUrl($path, $parameters = []) + { + $path = str_replace('_', '-', $path); + + return $this->urlGenerator->to($path, $parameters); + } +} diff --git a/src/Renderer/TwigEngine.php b/src/Renderer/TwigEngine.php new file mode 100644 index 00000000..55a2e299 --- /dev/null +++ b/src/Renderer/TwigEngine.php @@ -0,0 +1,41 @@ +<?php + +namespace Engelsystem\Renderer; + +use Twig_Environment as Twig; +use Twig_Error_Loader as LoaderError; +use Twig_Error_Runtime as RuntimeError; +use Twig_Error_Syntax as SyntaxError; + +class TwigEngine implements EngineInterface +{ + /** @var Twig */ + protected $twig; + + public function __construct(Twig $twig) + { + $this->twig = $twig; + } + + /** + * Render a twig template + * + * @param string $path + * @param array $data + * @return string + * @throws LoaderError|RuntimeError|SyntaxError + */ + public function get($path, $data = []) + { + return $this->twig->render($path, $data); + } + + /** + * @param string $path + * @return bool + */ + public function canRender($path) + { + return $this->twig->getLoader()->exists($path); + } +} diff --git a/src/Renderer/TwigLoader.php b/src/Renderer/TwigLoader.php new file mode 100644 index 00000000..154e6dbb --- /dev/null +++ b/src/Renderer/TwigLoader.php @@ -0,0 +1,26 @@ +<?php + +namespace Engelsystem\Renderer; + +use Twig_Error_Loader; +use Twig_Loader_Filesystem as FilesystemLoader; + +class TwigLoader extends FilesystemLoader +{ + /** + * @param string $name + * @param bool $throw + * @return false|string + * @throws Twig_Error_Loader + */ + public function findTemplate($name, $throw = true) + { + $extension = '.twig'; + $extensionLength = strlen($extension); + if (substr($name, -$extensionLength, $extensionLength) !== $extension) { + $name .= $extension; + } + + return parent::findTemplate($name, $throw); + } +} diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php new file mode 100644 index 00000000..0f453989 --- /dev/null +++ b/src/Renderer/TwigServiceProvider.php @@ -0,0 +1,77 @@ +<?php + +namespace Engelsystem\Renderer; + +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; + +class TwigServiceProvider extends ServiceProvider +{ + /** @var array */ + protected $extensions = [ + 'config' => Config::class, + 'globals' => Globals::class, + 'session' => Session::class, + 'url' => Url::class, + 'translation' => Translation::class, + ]; + + public function register() + { + $this->registerTwigEngine(); + + foreach ($this->extensions as $alias => $class) { + $this->registerTwigExtensions($class, $alias); + } + } + + public function boot() + { + /** @var Twig $renderer */ + $renderer = $this->app->get('twig.environment'); + + foreach ($this->app->tagged('twig.extension') as $extension) { + $renderer->addExtension($extension); + } + } + + protected function registerTwigEngine() + { + $viewsPath = $this->app->get('path.views'); + + $twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]); + $this->app->instance(TwigLoader::class, $twigLoader); + $this->app->instance(TwigLoaderInterface::class, $twigLoader); + $this->app->instance('twig.loader', $twigLoader); + + $twig = $this->app->make(Twig::class); + $this->app->instance(Twig::class, $twig); + $this->app->instance('twig.environment', $twig); + + $twigEngine = $this->app->make(TwigEngine::class); + $this->app->instance('renderer.twigEngine', $twigEngine); + $this->app->tag('renderer.twigEngine', ['renderer.engine']); + } + + /** + * @param string $class + * @param string $alias + */ + protected function registerTwigExtensions($class, $alias) + { + $alias = 'twig.extension.' . $alias; + + $extension = $this->app->make($class); + + $this->app->instance($class, $extension); + $this->app->instance($alias, $extension); + + $this->app->tag($alias, ['twig.extension']); + } +} |