RoutingExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 0
crap 1
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 24
    public function __construct(array $options = [])
34
    {
35
        $defaults = [
36 24
            'tag_name' => 'route',
37
            'tag_variable_name' => '_route',
38
            'discover_routing_functions' => false,
39
        ];
40
41 24
        $this->options = array_merge($defaults, $options);
42
43 24
        RouteNode::setVariableName($this->options['tag_variable_name']);
44 24
    }
45
46
    /**
47
     * {@inheritdoc}
48
     */
49 20
    public function getTokenParsers()
50
    {
51
        return [
52 20
            new RouteTokenParser($this->options['tag_name'], $this->options['discover_routing_functions']),
53
        ];
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59 20
    public function getNodeVisitors()
60
    {
61 20
        $nodeVisitors = [];
62
63 20
        if ($this->options['discover_routing_functions']) {
64 19
            $nodeVisitors[] = new DiscoverRoutingFunctionNodeVisitor($this->options['tag_name'], $this->options['tag_variable_name']);
65
        }
66
67 20
        return $nodeVisitors;
68
    }
69
70
    /**
71
     * {@inheritdoc}
72
     */
73 19
    public function getFunctions()
74
    {
75
        return [
76 19
            new TwigFunction('route_guard_route', [RoutingRuntime::class, 'route']),
77 19
            new TwigFunction('route_guard_path', [RoutingRuntime::class, 'path']),
78 19
            new TwigFunction('route_guard_url', [RoutingRuntime::class, 'url']),
79
        ];
80
    }
81
}
82