From 9410d4b49bbb33660e31dbf7c3fbf71e917e045e Mon Sep 17 00:00:00 2001 From: Pierre Rudloff Date: Sun, 20 Feb 2022 13:55:44 +0100 Subject: [PATCH] LinkHeaderMiddleware should use the same URL as ViewFactory This way the X-Forwarded-Path header is used to generate the Link header --- classes/App.php | 2 +- classes/Factory/ViewFactory.php | 41 +++++++++++++-------- classes/Middleware/LinkHeaderMiddleware.php | 18 ++------- 3 files changed, 31 insertions(+), 30 deletions(-) diff --git a/classes/App.php b/classes/App.php index 3d352b9..69a110a 100644 --- a/classes/App.php +++ b/classes/App.php @@ -62,7 +62,7 @@ class App extends \Slim\App // Middlewares. $this->add(new LocaleMiddleware($container)); $this->add(new CspMiddleware($container)); - $this->add(new LinkHeaderMiddleware($container)); + $this->add(new LinkHeaderMiddleware()); $this->add(new RouterPathMiddleware($container)); // Controllers. diff --git a/classes/Factory/ViewFactory.php b/classes/Factory/ViewFactory.php index 9e73e39..caa2167 100644 --- a/classes/Factory/ViewFactory.php +++ b/classes/Factory/ViewFactory.php @@ -57,22 +57,13 @@ class ViewFactory } /** - * Create Smarty view object. + * Create a URI suitable for templates. * - * @param ContainerInterface $container Slim dependency container - * @param Request|null $request PSR-7 request - * - * @return Smarty - * @throws SmartyException + * @param Request $request + * @return Uri */ - public static function create(ContainerInterface $container, Request $request = null): Smarty + public static function prepareUri(Request $request): Uri { - if (!isset($request)) { - $request = $container->get('request'); - } - - $view = new Smarty($container->get('root_path') . '/templates/'); - /** @var Uri $uri */ $uri = $request->getUri(); if (in_array('https', $request->getHeader('X-Forwarded-Proto'))) { @@ -92,11 +83,31 @@ class ViewFactory $uri = $uri->withBasePath($path); } + return self::cleanBasePath($uri); + } + + /** + * Create Smarty view object. + * + * @param ContainerInterface $container Slim dependency container + * @param Request|null $request PSR-7 request + * + * @return Smarty + * @throws SmartyException + */ + public static function create(ContainerInterface $container, Request $request = null): Smarty + { + if (!isset($request)) { + $request = $container->get('request'); + } + + $view = new Smarty($container->get('root_path') . '/templates/'); + + $uri = self::prepareUri($request); + /** @var LocaleManager $localeManager */ $localeManager = $container->get('locale'); - $uri = self::cleanBasePath($uri); - $smartyPlugins = new SmartyPlugins($container->get('router'), $uri->withUserInfo('')); $view->registerPlugin('function', 'path_for', [$smartyPlugins, 'pathFor']); $view->registerPlugin('function', 'base_url', [$smartyPlugins, 'baseUrl']); diff --git a/classes/Middleware/LinkHeaderMiddleware.php b/classes/Middleware/LinkHeaderMiddleware.php index da1d64d..12b1733 100644 --- a/classes/Middleware/LinkHeaderMiddleware.php +++ b/classes/Middleware/LinkHeaderMiddleware.php @@ -2,6 +2,7 @@ namespace Alltube\Middleware; +use Alltube\Factory\ViewFactory; use Psr\Container\ContainerInterface; use Slim\Http\Request; use Slim\Http\Response; @@ -13,19 +14,6 @@ use Slim\Router; */ class LinkHeaderMiddleware { - /** - * @var Router - */ - private $router; - - /** - * LinkHeaderMiddleware constructor. - * @param ContainerInterface $container - */ - public function __construct(ContainerInterface $container) - { - $this->router = $container->get('router'); - } /** * @param Request $request @@ -35,12 +23,14 @@ class LinkHeaderMiddleware */ public function __invoke(Request $request, Response $response, callable $next) { + $uri = ViewFactory::prepareUri($request); + $response = $response->withHeader( 'Link', implode( '; ', [ - '<' . $this->router->getBasePath() . '/css/style.css>', + '<' . $uri->getBasePath() . '/css/style.css>', 'rel=preload', 'as=style' ] )