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