Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 58
Deprecated : Constant E_STRICT is deprecated in /home/pastorz/old-espace-client/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler
<?php
namespace App\EventSubscriber ;
use App\Entity\User ;
use Doctrine\ORM\EntityManagerInterface ;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface ;
use Symfony\Component\EventDispatcher\EventSubscriberInterface ;
use Symfony\Component\HttpFoundation\RequestStack ;
use Symfony\Component\HttpFoundation\Response ;
use Symfony\Component\HttpFoundation\Session\SessionInterface ;
use Symfony\Component\HttpKernel\Event\RequestEvent ;
use Symfony\Component\HttpKernel\KernelEvents ;
use Symfony\Component\Security\Core\Security ;
use Symfony\Component\Security\Http\Util\TargetPathTrait ;
use Twig\Environment ;
class RequestSubscriber implements EventSubscriberInterface
{
use TargetPathTrait ;
public function __construct (
private SessionInterface $session ,
private Security $security ,
private Environment $twig ,
private ParameterBagInterface $parameterBag ,
private EntityManagerInterface $em ,
private RequestStack $requestStack
)
{
}
public static function getSubscribedEvents (): array
{
return [
KernelEvents :: REQUEST => [[ 'onKernelRequest' ], [ 'onMaintenance' , \PHP_INT_MAX - 1000 ]]
];
}
public function onKernelRequest ( RequestEvent $event ): void
{
$request = $event -> getRequest ();
$locale = $request -> getLocale ();
if($request -> attributes -> get ( '_route' ) != 'app_home' ){
setcookie ( '_locale' , $locale , [
'expires' => time () + 60 * 60 * 24 * 30 ,
'path' => '/' ,
'samesite' => 'Strict' ,
]);
}
$excludedRoutes = array(
'app_home' ,
'app_login' ,
'app_reset_password' ,
'app_check_email' ,
'app_forgot_password_request'
);
if (!$event -> isMainRequest () || $request -> isXmlHttpRequest () || in_array ( $request -> attributes -> get ( '_route' ), $excludedRoutes ) || str_contains ( $request -> getUri (), '/api' )) {
return;
}
/** @var User $user */
if ( $user = $this -> security -> getUser ()) {
$this -> removeTargetPath ( $this -> session , 'user_secured_area' );
$this -> removeTargetPath ( $this -> session , 'admin_secured_area' );
if ((!$user -> getPreferredLocale () || $user -> getPreferredLocale () != $locale ) && in_array ( strtolower ( $locale ), explode ( '|' , $this -> parameterBag -> get ( 'app.supported_locales' )))) {
$user -> setPreferredLocale ( $locale );
$this -> em -> persist ( $user );
$this -> em -> flush ();
}
return;
}
$this -> saveTargetPath ( $this -> session , str_contains ( $request -> getUri (), '/admin' ) ? 'admin' : 'user' . '_secured_area' , $request -> getUri ());
}
public function onMaintenance ( RequestEvent $event ): void
{
/** @var bool $isMaintenance */
$isMaintenance = \filter_var ( $_ENV [ 'MAINTENANCE_MODE' ] ?? '0' , \FILTER_VALIDATE_BOOLEAN );
if ($isMaintenance ) {
$event -> setResponse (new Response (
$this -> twig -> render ( 'admin/maintenance.html.twig' ),
Response :: HTTP_SERVICE_UNAVAILABLE ,
));
$event -> stopPropagation ();
}
}
}