|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Chris\Bundle\FrontRenderBundle\Subscriber; |
|
4
|
|
|
|
|
5
|
|
|
use Chris\Bundle\FrontRenderBundle\Twig\LexerManager; |
|
6
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
7
|
|
|
use Symfony\Component\HttpKernel\Event\FilterResponseEvent; |
|
8
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseEvent; |
|
9
|
|
|
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent; |
|
10
|
|
|
|
|
11
|
|
|
class RenderSubscriber implements EventSubscriberInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var LexerManager |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $twigLexerManager; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var bool |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $stopPropagation = false; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* RenderSubscriber constructor. |
|
25
|
|
|
* |
|
26
|
|
|
* @param LexerManager $twigLexerManager |
|
27
|
|
|
*/ |
|
28
|
|
|
public function __construct(LexerManager $twigLexerManager) |
|
29
|
|
|
{ |
|
30
|
|
|
$this->twigLexerManager = $twigLexerManager; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* {@inheritdoc} |
|
35
|
|
|
*/ |
|
36
|
|
|
public static function getSubscribedEvents() |
|
37
|
|
|
{ |
|
38
|
|
|
return [ |
|
39
|
|
|
'kernel.request' => ['updateTwigLexer'], |
|
40
|
|
|
'kernel.response' => ['rollbackTwigLexer'], |
|
41
|
|
|
'kernel.exception' => ['onKernelException'], |
|
42
|
|
|
]; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Set the custom twig lexer to display custom tags on the front |
|
47
|
|
|
* |
|
48
|
|
|
* @param GetResponseEvent $event |
|
49
|
|
|
*/ |
|
50
|
|
|
public function updateTwigLexer(GetResponseEvent $event) |
|
51
|
|
|
{ |
|
52
|
|
|
if (false === $this->stopPropagation && $event->isMasterRequest() && !$event->getRequest()->isXmlHttpRequest()) { |
|
53
|
|
|
$this->twigLexerManager->updateLexer(); |
|
54
|
|
|
$this->stopPropagation = true; |
|
55
|
|
|
} |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Set the custom twig lexer to display custom tags on the front |
|
60
|
|
|
* |
|
61
|
|
|
* @param FilterResponseEvent $event |
|
62
|
|
|
*/ |
|
63
|
|
|
public function rollbackTwigLexer(FilterResponseEvent $event) |
|
|
|
|
|
|
64
|
|
|
{ |
|
65
|
|
|
$this->twigLexerManager->rollbackLexer(); |
|
66
|
|
|
$this->stopPropagation = true; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Set the default twig lexer to display default tags on exceptions |
|
71
|
|
|
* |
|
72
|
|
|
* @param GetResponseForExceptionEvent $event |
|
73
|
|
|
*/ |
|
74
|
|
|
public function onKernelException(GetResponseForExceptionEvent $event) |
|
|
|
|
|
|
75
|
|
|
{ |
|
76
|
|
|
$this->twigLexerManager->rollbackLexer(); |
|
77
|
|
|
$this->stopPropagation = true; |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.