summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-08-26 12:23:47 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-08-29 23:46:31 +0200
commitdf6360044b5c2396b2bee0dfa9e8d744bfa424d5 (patch)
tree177b81f1feca7b1b77b4cd20d006bc1820baba12 /src
parentbb3d16d273bb3e4552e4869dd22cb2c2d81f5387 (diff)
Added Twig template functions
Diffstat (limited to 'src')
-rw-r--r--src/Config/ConfigServiceProvider.php1
-rw-r--r--src/Http/SessionServiceProvider.php1
-rw-r--r--src/Http/UrlGeneratorServiceProvider.php1
-rw-r--r--src/Middleware/LegacyMiddleware.php12
-rw-r--r--src/Renderer/Twig/Extensions/Config.php31
-rw-r--r--src/Renderer/Twig/Extensions/Globals.php23
-rw-r--r--src/Renderer/Twig/Extensions/Session.php31
-rw-r--r--src/Renderer/Twig/Extensions/Url.php43
-rw-r--r--src/Renderer/TwigServiceProvider.php44
9 files changed, 176 insertions, 11 deletions
diff --git a/src/Config/ConfigServiceProvider.php b/src/Config/ConfigServiceProvider.php
index 01b648df..21cbbeb6 100644
--- a/src/Config/ConfigServiceProvider.php
+++ b/src/Config/ConfigServiceProvider.php
@@ -12,6 +12,7 @@ class ConfigServiceProvider extends ServiceProvider
$configFile = config_path('config.php');
$config = $this->app->make(Config::class);
+ $this->app->instance(Config::class, $config);
$this->app->instance('config', $config);
$config->set(require $defaultConfigFile);
diff --git a/src/Http/SessionServiceProvider.php b/src/Http/SessionServiceProvider.php
index 55e3f48b..59121a3b 100644
--- a/src/Http/SessionServiceProvider.php
+++ b/src/Http/SessionServiceProvider.php
@@ -17,6 +17,7 @@ class SessionServiceProvider extends ServiceProvider
$this->app->bind(SessionStorageInterface::class, 'session.storage');
$session = $this->app->make(Session::class);
+ $this->app->instance(Session::class, $session);
$this->app->instance('session', $session);
/** @var Request $request */
diff --git a/src/Http/UrlGeneratorServiceProvider.php b/src/Http/UrlGeneratorServiceProvider.php
index 37304076..9b9988aa 100644
--- a/src/Http/UrlGeneratorServiceProvider.php
+++ b/src/Http/UrlGeneratorServiceProvider.php
@@ -9,6 +9,7 @@ class UrlGeneratorServiceProvider extends ServiceProvider
public function register()
{
$urlGenerator = $this->app->make(UrlGenerator::class);
+ $this->app->instance(UrlGenerator::class, $urlGenerator);
$this->app->instance('http.urlGenerator', $urlGenerator);
}
}
diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php
index 37ae9331..bf849611 100644
--- a/src/Middleware/LegacyMiddleware.php
+++ b/src/Middleware/LegacyMiddleware.php
@@ -284,21 +284,11 @@ class LegacyMiddleware implements MiddlewareInterface
}
return response(view('layouts/app', [
- 'theme' => isset($user) ? $user['color'] : config('theme'),
'title' => $title,
- 'atom_link' => ($page == 'news' || $page == 'user_meetings')
- ? ' <link href="'
- . page_link_to('atom', $parameters)
- . '" type = "application/atom+xml" rel = "alternate" title = "Atom Feed">'
- : '',
- 'start_page_url' => page_link_to('/'),
- 'credits_url' => page_link_to('credits'),
+ 'atom_feed' => ($page == 'news' || $page == 'user_meetings') ? $parameters : [],
'menu' => make_menu(),
'content' => msg() . $content,
'header_toolbar' => header_toolbar(),
- 'faq_url' => config('faq_url'),
- 'contact_email' => config('contact_email'),
- 'locale' => locale(),
'event_info' => EventConfig_info($event_config) . ' <br />'
]), $status);
}
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/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/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php
index 23810863..4a5b1de3 100644
--- a/src/Renderer/TwigServiceProvider.php
+++ b/src/Renderer/TwigServiceProvider.php
@@ -3,14 +3,40 @@
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\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,
+ ];
+
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()
@@ -20,12 +46,30 @@ class TwigServiceProvider extends ServiceProvider
$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']);
+ }
}