-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMaintenanceListener.php
More file actions
69 lines (52 loc) · 2.09 KB
/
MaintenanceListener.php
File metadata and controls
69 lines (52 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
/**
* src/Services/Listener/MaintenanceListener.php
*/
namespace App\Services\Listener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Twig\Environment;
class MaintenanceListener
{
// Container
private $container;
// Engine
public $_engine;
public function __construct(ContainerInterface $container, Environment $engine)
{
$this->container = $container;
$this->_engine = $engine;
}
public function onKernelRequest(RequestEvent $event)
{
// What route will be available publicly even in maintenance
$allowRoutesInMaintenance = [
"app_reset_password",
"app_forgot_password_request",
"app_login",
"app_logout"
];
// get maintenance parameter
$maintenance = $this->container->hasParameter('maintenance') ? $this->container->getParameter('maintenance') : null;
// Debug mode
$debug = in_array($this->container->get('kernel')->getEnvironment(), array('test'));
// If maintenance mode is turn on
if ($maintenance && !$debug) {
// Get the route name
$route_name = $event->getRequest()->attributes->get('_route');
// If $route_name isn't in $allowRoutesInMaintenance
if (!in_array($route_name, $allowRoutesInMaintenance)) {
// The user don't have the allowed role (people who can surf even in maintenance)
if (!$this->container->get('security.authorization_checker')->isGranted('ROLE_TEAM')) {
// Be sure maintenance/index.html.twig is your maintenance twig template, fix it correctly
$content = $this->_engine->render('maintenance/index.html.twig', []);
// Send response
$event->setResponse(new Response($content, 503));
// Stop the event propagation
$event->stopPropagation();
}
}
}
}
}