summaryrefslogtreecommitdiff
path: root/src/Renderer/TwigServiceProvider.php
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-08 12:48:08 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-09 12:11:12 +0200
commit222c9fed7d4ca2b3b44c05907fbb7080c1efd342 (patch)
tree62b1bfa9233000985331b9dea703d51dc24515a2 /src/Renderer/TwigServiceProvider.php
parent2d6bca1357faff28bc1f86a56b432cc463ff7574 (diff)
parent8257864829ffdfb410f05e0dd0a9c781f48b741a (diff)
Merge remote-tracking branch 'MyIgel/templating'
Diffstat (limited to 'src/Renderer/TwigServiceProvider.php')
-rw-r--r--src/Renderer/TwigServiceProvider.php77
1 files changed, 77 insertions, 0 deletions
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']);
+ }
+}