summaryrefslogtreecommitdiff
path: root/src/Renderer
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-26 02:54:52 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:11 +0200
commitbb3d16d273bb3e4552e4869dd22cb2c2d81f5387 (patch)
tree7de41548a5bd3a56aead1d9d528aa93e058e973a /src/Renderer
parenta1bc763a16ee8be109de5c9053fbc5eded53824e (diff)
Added Twig template renderer, closes #338
Diffstat (limited to 'src/Renderer')
-rw-r--r--src/Renderer/TwigEngine.php41
-rw-r--r--src/Renderer/TwigLoader.php26
-rw-r--r--src/Renderer/TwigServiceProvider.php31
3 files changed, 98 insertions, 0 deletions
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..23810863
--- /dev/null
+++ b/src/Renderer/TwigServiceProvider.php
@@ -0,0 +1,31 @@
+<?php
+
+namespace Engelsystem\Renderer;
+
+use Engelsystem\Container\ServiceProvider;
+use Twig_Environment as Twig;
+use Twig_LoaderInterface as TwigLoaderInterface;
+
+class TwigServiceProvider extends ServiceProvider
+{
+ public function register()
+ {
+ $this->registerTwigEngine();
+ }
+
+ 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);
+
+ $twig = $this->app->make(Twig::class);
+ $this->app->instance(Twig::class, $twig);
+
+ $twigEngine = $this->app->make(TwigEngine::class);
+ $this->app->instance('renderer.twigEngine', $twigEngine);
+ $this->app->tag('renderer.twigEngine', ['renderer.engine']);
+ }
+}