Completed
Branch master (c0d8ef)
by Yaroslav
06:36
created

RoutingExtension::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 6
dl 0
loc 11
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
cc 1
nc 1
nop 1
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 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