Skip to content

Commit

Permalink
Add middleware to auto-detect basePath
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurHoaro committed Nov 23, 2023
1 parent c56a8e4 commit 18c68c0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 11 deletions.
16 changes: 9 additions & 7 deletions application/front/ShaarliErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand Down Expand Up @@ -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.'));

Expand Down
2 changes: 1 addition & 1 deletion application/front/ShaarliMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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), '/'));
}
}

Expand Down
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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.*"
Expand Down
54 changes: 52 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
);


Expand Down

0 comments on commit 18c68c0

Please sign in to comment.