slovocast/app/src/Middlewares.php

64 lines
1.8 KiB
PHP
Raw Normal View History

2024-07-18 01:32:25 +00:00
<?php
namespace Slovocast;
use League\Config\Configuration;
2024-12-09 14:29:03 +00:00
use Odan\Session\Middleware\SessionStartMiddleware;
2024-07-18 01:32:25 +00:00
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 Twig\Error\LoaderError;
class Middlewares
{
/**
* Set up all the application middleware. This is where the application
* should determine the environment the code is running in as well.
*
* @param App $app The Slim application
* @return void
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 */
2024-07-18 01:32:25 +00:00
$config = $container->get('config');
/**
* Global session start
*/
2024-12-09 14:29:03 +00:00
$app->add(SessionStartMiddleware::class);
2024-07-18 01:32:25 +00:00
// Twig
$templateCache = false;
$twig = Twig::create(APP_TEMPLATES_DIR, [
2024-11-25 03:22:57 +00:00
'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
/** @var SessionInterface $session */
$session = $container->get(SessionInterface::class);
$twig->getEnvironment()->addGlobal('session', $session);
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);
2024-07-18 01:32:25 +00:00
}
}