3 namespace Shaarli\Front
;
5 use Shaarli\Container\ShaarliContainer
;
6 use Shaarli\Front\Exception\ShaarliFrontException
;
7 use Shaarli\Front\Exception\UnauthorizedException
;
9 use Slim\Http\Response
;
12 * Class ShaarliMiddleware
14 * This will be called before accessing any Shaarli controller.
16 class ShaarliMiddleware
18 /** @var ShaarliContainer contains all Shaarli DI */
21 public function __construct(ShaarliContainer
$container)
23 $this->container
= $container;
27 * Middleware execution:
28 * - execute the controller
29 * - return the response
31 * In case of error, the error template will be displayed with the exception message.
33 * @param Request $request Slim request
34 * @param Response $response Slim response
35 * @param callable $next Next action
37 * @return Response response.
39 public function __invoke(Request
$request, Response
$response, callable
$next)
42 $this->container
->basePath
= rtrim($request->getUri()->getBasePath(), '/');
44 $response = $next($request, $response);
45 } catch (ShaarliFrontException
$e) {
46 $this->container
->pageBuilder
->assign('message', $e->getMessage());
47 if ($this->container
->conf
->get('dev.debug', false)) {
48 $this->container
->pageBuilder
->assign(
50 nl2br(get_class($this) .': '. $e->getTraceAsString())
54 $response = $response->withStatus($e->getCode());
55 $response = $response->write($this->container
->pageBuilder
->render('error'));
56 } catch (UnauthorizedException
$e) {
57 return $response->withRedirect($request->getUri()->getBasePath() . '/login');