summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorIgor Scheller <igor.scheller@igorshp.de>2018-09-23 19:13:19 +0200
committerIgor Scheller <igor.scheller@igorshp.de>2018-09-23 20:11:37 +0200
commit66038eda14d5d4e624b6636a6156570e3e940e49 (patch)
tree6e4b7557b7d91786ef47f22f7ddef85eed1dfb42 /src
parent590adffa9316b98544cb8d67b03b80e44ba9c8b7 (diff)
parent9d34f371cb9c5ab0d60bd3158678b9cc9da6cc80 (diff)
Merge branch 'twig-templates'
Diffstat (limited to 'src')
-rw-r--r--src/Controllers/BaseController.php8
-rw-r--r--src/Controllers/CreditsController.php24
-rw-r--r--src/Http/RequestServiceProvider.php4
-rw-r--r--src/Http/ResponseServiceProvider.php3
-rw-r--r--src/Middleware/LegacyMiddleware.php25
-rw-r--r--src/Middleware/RequestHandler.php31
-rw-r--r--src/Renderer/Twig/Extensions/Authentication.php40
-rw-r--r--src/Renderer/Twig/Extensions/Globals.php40
-rw-r--r--src/Renderer/Twig/Extensions/Legacy.php49
-rw-r--r--src/Renderer/TwigServiceProvider.php24
10 files changed, 218 insertions, 30 deletions
diff --git a/src/Controllers/BaseController.php b/src/Controllers/BaseController.php
new file mode 100644
index 00000000..6a27a066
--- /dev/null
+++ b/src/Controllers/BaseController.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace Engelsystem\Controllers;
+
+abstract class BaseController
+{
+
+}
diff --git a/src/Controllers/CreditsController.php b/src/Controllers/CreditsController.php
new file mode 100644
index 00000000..568811c7
--- /dev/null
+++ b/src/Controllers/CreditsController.php
@@ -0,0 +1,24 @@
+<?php
+
+namespace Engelsystem\Controllers;
+
+use Engelsystem\Http\Response;
+
+class CreditsController extends BaseController
+{
+ /** @var Response */
+ protected $response;
+
+ public function __construct(Response $response)
+ {
+ $this->response = $response;
+ }
+
+ /**
+ * @return Response
+ */
+ public function index()
+ {
+ return $this->response->withView('pages/credits.twig');
+ }
+}
diff --git a/src/Http/RequestServiceProvider.php b/src/Http/RequestServiceProvider.php
index bbf2579c..37f8c604 100644
--- a/src/Http/RequestServiceProvider.php
+++ b/src/Http/RequestServiceProvider.php
@@ -3,6 +3,7 @@
namespace Engelsystem\Http;
use Engelsystem\Container\ServiceProvider;
+use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
class RequestServiceProvider extends ServiceProvider
{
@@ -18,6 +19,9 @@ class RequestServiceProvider extends ServiceProvider
/** @var Request $request */
$request = $this->app->call([Request::class, 'createFromGlobals']);
$this->setTrustedProxies($request, $trustedProxies);
+
+ $this->app->instance(Request::class, $request);
+ $this->app->instance(SymfonyRequest::class, $request);
$this->app->instance('request', $request);
}
diff --git a/src/Http/ResponseServiceProvider.php b/src/Http/ResponseServiceProvider.php
index f0d238ef..c1489f06 100644
--- a/src/Http/ResponseServiceProvider.php
+++ b/src/Http/ResponseServiceProvider.php
@@ -3,12 +3,15 @@
namespace Engelsystem\Http;
use Engelsystem\Container\ServiceProvider;
+use Symfony\Component\HttpFoundation\Response as SymfonyResponse;
class ResponseServiceProvider extends ServiceProvider
{
public function register()
{
$response = $this->app->make(Response::class);
+ $this->app->instance(Response::class, $response);
+ $this->app->instance(SymfonyResponse::class, $response);
$this->app->instance('response', $response);
}
}
diff --git a/src/Middleware/LegacyMiddleware.php b/src/Middleware/LegacyMiddleware.php
index f4d6bbcc..ebf456eb 100644
--- a/src/Middleware/LegacyMiddleware.php
+++ b/src/Middleware/LegacyMiddleware.php
@@ -18,7 +18,6 @@ class LegacyMiddleware implements MiddlewareInterface
'angeltypes',
'api',
'atom',
- 'credits',
'ical',
'login',
'public_dashboard',
@@ -60,6 +59,7 @@ class LegacyMiddleware implements MiddlewareInterface
): ResponseInterface {
global $user;
global $privileges;
+ global $page;
/** @var Request $appRequest */
$appRequest = $this->container->get('request');
@@ -248,11 +248,6 @@ class LegacyMiddleware implements MiddlewareInterface
$title = admin_log_title();
$content = admin_log();
return [$title, $content];
- case 'credits':
- require_once realpath(__DIR__ . '/../../includes/pages/guest_credits.php');
- $title = credits_title();
- $content = guest_credits();
- return [$title, $content];
}
require_once realpath(__DIR__ . '/../../includes/pages/guest_start.php');
@@ -271,27 +266,13 @@ class LegacyMiddleware implements MiddlewareInterface
*/
protected function renderPage($page, $title, $content)
{
- global $user;
- $event_config = EventConfig();
- $parameters = [
- 'key' => (isset($user) ? $user['api_key'] : ''),
- ];
-
- if ($page == 'user_meetings') {
- $parameters['meetings'] = 1;
- }
-
if (!empty($page) && is_int($page)) {
return response($content, (int)$page);
}
return response(view('layouts/app', [
- 'title' => $title,
- 'atom_feed' => ($page == 'news' || $page == 'user_meetings') ? $parameters : [],
- 'menu' => make_menu(),
- 'content' => msg() . $content,
- 'header_toolbar' => header_toolbar(),
- 'event_info' => EventConfig_info($event_config) . ' <br />'
+ 'title' => $title,
+ 'content' => msg() . $content,
]), 200);
}
}
diff --git a/src/Middleware/RequestHandler.php b/src/Middleware/RequestHandler.php
index e1381abf..ebe1ff9e 100644
--- a/src/Middleware/RequestHandler.php
+++ b/src/Middleware/RequestHandler.php
@@ -35,7 +35,7 @@ class RequestHandler implements MiddlewareInterface
public function process(ServerRequestInterface $request, RequestHandlerInterface $handler): ResponseInterface
{
$requestHandler = $request->getAttribute('route-request-handler');
- $requestHandler = $this->resolveMiddleware($requestHandler);
+ $requestHandler = $this->resolveRequestHandler($requestHandler);
if ($requestHandler instanceof MiddlewareInterface) {
return $requestHandler->process($request, $handler);
@@ -47,4 +47,33 @@ class RequestHandler implements MiddlewareInterface
throw new InvalidArgumentException('Unable to process request handler of type ' . gettype($requestHandler));
}
+
+ /**
+ * @param string|callable|MiddlewareInterface|RequestHandlerInterface $handler
+ * @return MiddlewareInterface|RequestHandlerInterface
+ */
+ protected function resolveRequestHandler($handler)
+ {
+ if (is_string($handler) && strpos($handler, '@') !== false) {
+ list($class, $method) = explode('@', $handler, 2);
+ if (!class_exists($class) && !$this->container->has($class)) {
+ $class = sprintf('Engelsystem\\Controllers\\%s', $class);
+ }
+
+ $handler = [$class, $method];
+ }
+
+ if (
+ is_array($handler)
+ && is_string($handler[0])
+ && (
+ class_exists($handler[0])
+ || $this->container->has($handler[0])
+ )
+ ) {
+ $handler[0] = $this->container->make($handler[0]);
+ }
+
+ return $this->resolveMiddleware($handler);
+ }
}
diff --git a/src/Renderer/Twig/Extensions/Authentication.php b/src/Renderer/Twig/Extensions/Authentication.php
new file mode 100644
index 00000000..6a72d825
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Authentication.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Authentication extends TwigExtension
+{
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ return [
+ new TwigFunction('is_user', [$this, 'isAuthenticated']),
+ new TwigFunction('is_guest', [$this, 'isGuest']),
+ new TwigFunction('has_permission_to', [$this, 'checkAuth']),
+ ];
+ }
+
+ public function isAuthenticated()
+ {
+ global $user;
+
+ return !empty($user);
+ }
+
+ public function isGuest()
+ {
+ return !$this->isAuthenticated();
+ }
+
+ public function checkAuth($privilege)
+ {
+ global $privileges;
+
+ return in_array($privilege, $privileges);
+ }
+}
diff --git a/src/Renderer/Twig/Extensions/Globals.php b/src/Renderer/Twig/Extensions/Globals.php
index f9bffbc8..1a4df42c 100644
--- a/src/Renderer/Twig/Extensions/Globals.php
+++ b/src/Renderer/Twig/Extensions/Globals.php
@@ -2,6 +2,7 @@
namespace Engelsystem\Renderer\Twig\Extensions;
+use Carbon\Carbon;
use Twig_Extension as TwigExtension;
use Twig_Extension_GlobalsInterface as GlobalsInterface;
@@ -16,8 +17,45 @@ class Globals extends TwigExtension implements GlobalsInterface
{
global $user;
+ $eventConfig = $this->getEventConfig();
+ if (empty($eventConfig)) {
+ $eventConfig = [];
+ }
+
return [
- 'user' => isset($user) ? $user : [],
+ 'user' => isset($user) ? $user : [],
+ 'event_config' => $this->filterEventConfig($eventConfig),
];
}
+
+ /**
+ * @return array
+ * @codeCoverageIgnore
+ */
+ protected function getEventConfig()
+ {
+ return EventConfig();
+ }
+
+ /**
+ * @param $eventConfig
+ * @return mixed
+ */
+ protected function filterEventConfig($eventConfig)
+ {
+ array_walk($eventConfig, function (&$value, $key) {
+ if (is_null($value) || !in_array($key, [
+ 'buildup_start_date',
+ 'event_start_date',
+ 'event_end_date',
+ 'teardown_end_date',
+ ])) {
+ return;
+ }
+
+ $value = Carbon::createFromTimestamp($value);
+ });
+
+ return $eventConfig;
+ }
}
diff --git a/src/Renderer/Twig/Extensions/Legacy.php b/src/Renderer/Twig/Extensions/Legacy.php
new file mode 100644
index 00000000..79de32cb
--- /dev/null
+++ b/src/Renderer/Twig/Extensions/Legacy.php
@@ -0,0 +1,49 @@
+<?php
+
+namespace Engelsystem\Renderer\Twig\Extensions;
+
+use Engelsystem\Http\Request;
+use Twig_Extension as TwigExtension;
+use Twig_Function as TwigFunction;
+
+class Legacy extends TwigExtension
+{
+ /** @var Request */
+ protected $request;
+
+ /**
+ * @param Request $request
+ */
+ public function __construct(Request $request)
+ {
+ $this->request = $request;
+ }
+
+ /**
+ * @return TwigFunction[]
+ */
+ public function getFunctions()
+ {
+ $isSafeHtml = ['is_safe' => ['html']];
+ return [
+ new TwigFunction('menu', 'make_navigation', $isSafeHtml),
+ new TwigFunction('menuUserShiftState', 'User_shift_state_render', $isSafeHtml),
+ new TwigFunction('menuUserMessages', 'user_unread_messages', $isSafeHtml),
+ new TwigFunction('menuUserHints', 'header_render_hints', $isSafeHtml),
+ new TwigFunction('menuUserSubmenu', 'make_user_submenu', $isSafeHtml),
+ new TwigFunction('page', [$this, 'getPage']),
+ ];
+ }
+
+ /**
+ * @return string
+ */
+ public function getPage()
+ {
+ if ($this->request->has('p')) {
+ return $this->request->get('p');
+ }
+
+ return $this->request->path();
+ }
+}
diff --git a/src/Renderer/TwigServiceProvider.php b/src/Renderer/TwigServiceProvider.php
index c70fb303..49a0eb90 100644
--- a/src/Renderer/TwigServiceProvider.php
+++ b/src/Renderer/TwigServiceProvider.php
@@ -2,26 +2,32 @@
namespace Engelsystem\Renderer;
+use Engelsystem\Config\Config as EngelsystemConfig;
use Engelsystem\Container\ServiceProvider;
+use Engelsystem\Renderer\Twig\Extensions\Authentication;
use Engelsystem\Renderer\Twig\Extensions\Assets;
use Engelsystem\Renderer\Twig\Extensions\Config;
use Engelsystem\Renderer\Twig\Extensions\Globals;
+use Engelsystem\Renderer\Twig\Extensions\Legacy;
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_Extension_Core as TwigCore;
use Twig_LoaderInterface as TwigLoaderInterface;
class TwigServiceProvider extends ServiceProvider
{
/** @var array */
protected $extensions = [
- 'assets' => Assets::class,
- 'config' => Config::class,
- 'globals' => Globals::class,
- 'session' => Session::class,
- 'url' => Url::class,
- 'translation' => Translation::class,
+ 'assets' => Assets::class,
+ 'authentication' => Authentication::class,
+ 'config' => Config::class,
+ 'globals' => Globals::class,
+ 'session' => Session::class,
+ 'legacy' => Legacy::class,
+ 'translation' => Translation::class,
+ 'url' => Url::class,
];
public function register()
@@ -46,6 +52,8 @@ class TwigServiceProvider extends ServiceProvider
protected function registerTwigEngine()
{
$viewsPath = $this->app->get('path.views');
+ /** @var EngelsystemConfig $config */
+ $config = $this->app->get('config');
$twigLoader = $this->app->make(TwigLoader::class, ['paths' => $viewsPath]);
$this->app->instance(TwigLoader::class, $twigLoader);
@@ -56,6 +64,10 @@ class TwigServiceProvider extends ServiceProvider
$this->app->instance(Twig::class, $twig);
$this->app->instance('twig.environment', $twig);
+ /** @var TwigCore $twigCore */
+ $twigCore = $twig->getExtension(TwigCore::class);
+ $twigCore->setTimezone($config->get('timezone'));
+
$twigEngine = $this->app->make(TwigEngine::class);
$this->app->instance('renderer.twigEngine', $twigEngine);
$this->app->tag('renderer.twigEngine', ['renderer.engine']);