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