diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/Application.php | 1 | ||||
-rw-r--r-- | src/Middleware/LegacyMiddleware.php | 2 | ||||
-rw-r--r-- | src/Renderer/TwigEngine.php | 41 | ||||
-rw-r--r-- | src/Renderer/TwigLoader.php | 26 | ||||
-rw-r--r-- | src/Renderer/TwigServiceProvider.php | 31 | ||||
-rw-r--r-- | src/helpers.php | 4 |
6 files changed, 102 insertions, 3 deletions
diff --git a/src/Application.php b/src/Application.php index 6644a6cf..86397a2c 100644 --- a/src/Application.php +++ b/src/Application.php @@ -107,6 +107,7 @@ class Application extends Container $this->instance('path', $appPath); $this->instance('path.config', $appPath . DIRECTORY_SEPARATOR . 'config'); $this->instance('path.lang', $appPath . DIRECTORY_SEPARATOR . 'locale'); + $this->instance('path.views', $appPath . DIRECTORY_SEPARATOR . 'templates'); } /** diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php index 276fb3ee..37ae9331 100644 --- a/src/Middleware/LegacyMiddleware.php +++ b/src/Middleware/LegacyMiddleware.php @@ -283,7 +283,7 @@ class LegacyMiddleware implements MiddlewareInterface $content = info($content, true); } - return response(view(__DIR__ . '/../../templates/layout.html', [ + return response(view('layouts/app', [ 'theme' => isset($user) ? $user['color'] : config('theme'), 'title' => $title, 'atom_link' => ($page == 'news' || $page == 'user_meetings') 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']); + } +} diff --git a/src/helpers.php b/src/helpers.php index a90b2462..336f81fe 100644 --- a/src/helpers.php +++ b/src/helpers.php @@ -5,8 +5,8 @@ use Engelsystem\Application; use Engelsystem\Config\Config; use Engelsystem\Http\Request; use Engelsystem\Http\Response; -use Engelsystem\Renderer\Renderer; use Engelsystem\Http\UrlGenerator; +use Engelsystem\Renderer\Renderer; use Symfony\Component\HttpFoundation\Session\SessionInterface; /** @@ -139,7 +139,7 @@ function url($path = null, $parameters = []) * @param mixed[] $data * @return Renderer|string */ -function view($template = null, $data = null) +function view($template = null, $data = []) { $renderer = app('renderer'); |