slovocast/app/src/Middlewares.php

61 lines
1.7 KiB
PHP
Raw Normal View History

2024-07-18 01:32:25 +00:00
<?php
namespace Slovocast;
use League\Config\Configuration;
use Odan\Session\SessionInterface;
use Psr\Container\ContainerExceptionInterface;
use Psr\Container\ContainerInterface;
use Psr\Container\NotFoundExceptionInterface;
use Slim\App;
use Slim\Views\Twig;
use Slim\Views\TwigMiddleware;
use Slovocast\Middleware\SessionMiddleware;
2024-07-18 01:32:25 +00:00
use Twig\Error\LoaderError;
class Middlewares
{
/**
* @param App $app The Slim application
2024-07-18 01:32:25 +00:00
* @throws NotFoundExceptionInterface
* @throws ContainerExceptionInterface
* @throws LoaderError
*/
public static function setup(App $app): void
{
/** @var ContainerInterface $container */
$container = $app->getContainer();
/**
* @var Configuration $config
*/
$config = $container->get('config');
/**
* Global session start
*/
$app->add(SessionMiddleware::class);
2024-07-18 01:32:25 +00:00
// Twig
$templateCache = false;
2024-11-25 03:22:57 +00:00
$twig = Twig::create(APP_TEMPLATES_DIR, [
'cache' => $templateCache,
'debug' => true,
]);
2024-07-18 01:32:25 +00:00
// Add the global variables
$twig->getEnvironment()->addGlobal('site_name', $config->get('site.name'));
$twig->getEnvironment()->addGlobal('site_description', $config->get('site.description'));
2024-11-25 03:10:36 +00:00
$session = $container->get(SessionInterface::class);
$flash = $session->getFlash();
$twig->getEnvironment()->addGlobal('session', $session);
2024-07-18 01:32:25 +00:00
$twig->getEnvironment()->addGlobal('flash', $flash);
2024-11-25 03:10:36 +00:00
2024-07-18 01:32:25 +00:00
$app->add(TwigMiddleware::create($app, $twig));
// Add the error handling middleware
$app->addErrorMiddleware(true, true, true);
}
}