From 18c68c09c1f5a5c0c16c3071ec400346f301d4bf Mon Sep 17 00:00:00 2001 From: ArthurHoaro Date: Thu, 23 Nov 2023 12:24:11 -0500 Subject: [PATCH] Add middleware to auto-detect basePath --- application/front/ShaarliErrorHandler.php | 16 ++++--- application/front/ShaarliMiddleware.php | 2 +- composer.json | 1 + composer.lock | 54 ++++++++++++++++++++++- index.php | 4 +- 5 files changed, 66 insertions(+), 11 deletions(-) diff --git a/application/front/ShaarliErrorHandler.php b/application/front/ShaarliErrorHandler.php index 99e323c1c..42a17fb87 100644 --- a/application/front/ShaarliErrorHandler.php +++ b/application/front/ShaarliErrorHandler.php @@ -12,6 +12,7 @@ use Shaarli\Front\Controller\PageTrait; use Shaarli\Front\Exception\ShaarliFrontException; use Shaarli\Render\TemplatePage; +use Slim\App; use Slim\Exception\HttpNotFoundException; use Slim\Handlers\ErrorHandler; use Slim\Interfaces\CallableResolverInterface; @@ -23,15 +24,17 @@ class ShaarliErrorHandler extends ErrorHandler { use PageTrait; + private App $app; + private ?Container $container; public function __construct( - CallableResolverInterface $callableResolver, - ResponseFactoryInterface $responseFactory, + App $app, ?LoggerInterface $logger = null, - ?Container $container = null + ?Container $container = null, ) { - parent::__construct($callableResolver, $responseFactory, $logger); + parent::__construct($app->getCallableResolver(), $app->getResponseFactory(), $logger); + $this->app = $app; $this->container = $container; } @@ -84,10 +87,9 @@ protected function showError404($request): ResponseInterface if (false !== strpos($request->getRequestTarget(), '/api/v1')) { return $response->withStatus(404); } - $basePathFromRequest = $request->getAttribute(RouteContext::BASE_PATH) ?? ''; - // This is required because the middleware is ignored if the route is not found. - $this->container->set('basePath', rtrim($basePathFromRequest, '/')); + // This is required because the request handler throw the error before setting the base path. + $this->container->set('basePath', rtrim($this->app->getBasePath(), '/')); $this->assignView('error_message', t('Requested page could not be found.')); diff --git a/application/front/ShaarliMiddleware.php b/application/front/ShaarliMiddleware.php index 7163a470a..621078628 100644 --- a/application/front/ShaarliMiddleware.php +++ b/application/front/ShaarliMiddleware.php @@ -113,7 +113,7 @@ protected function checkOpenShaarli(Request $request, RequestHandler $handler): protected function initBasePath(Request $request): void { if (null === $this->container->get('basePath')) { - $this->container->set('basePath', rtrim($request->getAttribute('basePath'), '/')); + $this->container->set('basePath', rtrim($request->getAttribute(RouteContext::BASE_PATH), '/')); } } diff --git a/composer.json b/composer.json index 677acf936..200eb6457 100644 --- a/composer.json +++ b/composer.json @@ -27,6 +27,7 @@ "malkusch/lock": "^2.1", "php-di/php-di": "^6.4", "pubsubhubbub/publisher": "dev-master", + "selective/basepath": "^2.1", "shaarli/netscape-bookmark-parser": "^4.0", "slim/psr7": "^1.6", "slim/slim": "4.*" diff --git a/composer.lock b/composer.lock index 882e418b0..0b5e03b22 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "f528234eae54fec2c08e480246366930", + "content-hash": "cd0bf70a2b6566e8585f46b238109116", "packages": [ { "name": "arthurhoaro/web-thumbnailer", @@ -1264,6 +1264,56 @@ }, "time": "2019-03-08T08:55:37+00:00" }, + { + "name": "selective/basepath", + "version": "2.1.0", + "source": { + "type": "git", + "url": "https://github.com/selective-php/basepath.git", + "reference": "63961fbfcaf492bd0ae8e40653f6c3c750c2f8ad" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/selective-php/basepath/zipball/63961fbfcaf492bd0ae8e40653f6c3c750c2f8ad", + "reference": "63961fbfcaf492bd0ae8e40653f6c3c750c2f8ad", + "shasum": "" + }, + "require": { + "php": "^7.2 || ^8.0", + "psr/http-server-middleware": "^1" + }, + "require-dev": { + "friendsofphp/php-cs-fixer": "^2", + "overtrue/phplint": "^2", + "phpstan/phpstan": "0.*", + "phpunit/phpunit": "^8 || ^9", + "slim/psr7": "^1", + "slim/slim": "^4", + "squizlabs/php_codesniffer": "^3" + }, + "type": "library", + "autoload": { + "psr-4": { + "Selective\\BasePath\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "description": "A URL base path detector for Slim 4", + "homepage": "https://github.com/selective-php/basepath", + "keywords": [ + "basepath", + "slim", + "slim4" + ], + "support": { + "issues": "https://github.com/selective-php/basepath/issues", + "source": "https://github.com/selective-php/basepath/tree/2.1.0" + }, + "time": "2021-07-17T10:03:02+00:00" + }, { "name": "shaarli/netscape-bookmark-parser", "version": "v4.0.0", @@ -3960,5 +4010,5 @@ "platform-overrides": { "php": "7.4.33" }, - "plugin-api-version": "2.3.0" + "plugin-api-version": "2.6.0" } diff --git a/index.php b/index.php index 7e92dca8d..a731b8766 100644 --- a/index.php +++ b/index.php @@ -28,6 +28,7 @@ use Katzgrau\KLogger\Logger; use Psr\Log\LogLevel; +use Selective\BasePath\BasePathMiddleware; use Shaarli\Api\Controllers as ApiControllers; use Shaarli\Config\ConfigManager; use Shaarli\Container\ContainerBuilder; @@ -108,6 +109,7 @@ $container = $containerBuilder->build(); AppFactory::setContainer($container); $app = AppFactory::create(); +$app->add(new BasePathMiddleware($app)); // Main Shaarli routes $app->group('', function (RouteCollectorProxy $group) { @@ -202,7 +204,7 @@ $errorMiddleware = $app->addErrorMiddleware($displayErrorDetails, true, true); $errorMiddleware->setDefaultErrorHandler( - new ShaarliErrorHandler($app->getCallableResolver(), $app->getResponseFactory(), $logger, $container) + new ShaarliErrorHandler($app, $logger, $container) );