1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* |
5
|
|
|
* (c) Yaroslav Honcharuk <[email protected]> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Yarhon\RouteGuardBundle\Twig\Extension; |
12
|
|
|
|
13
|
|
|
use Twig\Extension\AbstractExtension; |
14
|
|
|
use Twig\TwigFunction; |
15
|
|
|
use Yarhon\RouteGuardBundle\Twig\Node\RouteNode; |
16
|
|
|
use Yarhon\RouteGuardBundle\Twig\TokenParser\RouteTokenParser; |
17
|
|
|
use Yarhon\RouteGuardBundle\Twig\NodeVisitor\DiscoverRoutingFunctionNodeVisitor; |
18
|
|
|
use Yarhon\RouteGuardBundle\Twig\RoutingRuntime; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @author Yaroslav Honcharuk <[email protected]> |
22
|
|
|
*/ |
23
|
|
|
class RoutingExtension extends AbstractExtension |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var array |
27
|
|
|
*/ |
28
|
|
|
private $options; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param array $options |
32
|
|
|
*/ |
33
|
12 |
|
public function __construct(array $options = []) |
34
|
|
|
{ |
35
|
|
|
$defaults = [ |
36
|
12 |
|
'tag_name' => 'route', |
37
|
|
|
'tag_variable_name' => '_route', |
38
|
|
|
'discover_routing_functions' => false, |
39
|
|
|
]; |
40
|
|
|
|
41
|
12 |
|
$this->options = array_merge($defaults, $options); |
42
|
|
|
|
43
|
12 |
|
RouteNode::setVariableName($this->options['tag_variable_name']); |
44
|
12 |
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
3 |
|
public function getTokenParsers() |
50
|
|
|
{ |
51
|
|
|
return [ |
52
|
3 |
|
new RouteTokenParser($this->options['tag_name'], $this->options['discover_routing_functions']), |
53
|
|
|
]; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* {@inheritdoc} |
58
|
|
|
*/ |
59
|
3 |
|
public function getNodeVisitors() |
60
|
|
|
{ |
61
|
3 |
|
$nodeVisitors = []; |
62
|
|
|
|
63
|
3 |
|
if ($this->options['discover_routing_functions']) { |
64
|
2 |
|
$nodeVisitors[] = new DiscoverRoutingFunctionNodeVisitor($this->options['tag_name'], $this->options['tag_variable_name']); |
65
|
|
|
} |
66
|
|
|
|
67
|
3 |
|
return $nodeVisitors; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
2 |
|
public function getFunctions() |
74
|
|
|
{ |
75
|
|
|
return [ |
76
|
2 |
|
new TwigFunction('route_guard_route', [RoutingRuntime::class, 'route']), |
77
|
2 |
|
new TwigFunction('route_guard_path', [RoutingRuntime::class, 'path']), |
78
|
2 |
|
new TwigFunction('route_guard_url', [RoutingRuntime::class, 'url']), |
79
|
|
|
]; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|