PuliExtension::getFunctions()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 8
ccs 2
cts 2
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the puli/twig-puli-extension package.
5
 *
6
 * (c) Bernhard Schussek <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Puli\TwigExtension;
13
14
use Puli\Repository\Api\ResourceRepository;
15
use Puli\TwigExtension\NodeVisitor\PuliDirTagger;
16
use Puli\TwigExtension\NodeVisitor\TemplatePathResolver;
17
use Puli\TwigExtension\TokenParser\LoadedByPuliTokenParser;
18
use Puli\UrlGenerator\Api\UrlGenerator;
19
use Twig_Extension;
20
use Twig_SimpleFunction;
21
22
/**
23
 * @since  1.0
24
 *
25
 * @author Bernhard Schussek <[email protected]>
26
 */
27
class PuliExtension extends Twig_Extension
28
{
29
    /**
30
     * Priority for node visitors that want to work with relative path before
31
     * they are turned into absolute paths.
32
     */
33
    const PRE_RESOLVE_PATHS = 4;
34
35
    /**
36
     * Priority for node visitors that turn relative paths into absolute paths.
37
     */
38
    const RESOLVE_PATHS = 5;
39
40
    /**
41
     * Priority for node visitors that want to work with absolute paths.
42
     */
43
    const POST_RESOLVE_PATHS = 6;
44
45
    /**
46
     * @var ResourceRepository
47
     */
48
    private $repo;
49
50
    /**
51
     * @var UrlGenerator
52
     */
53
    private $urlGenerator;
54
55
    /**
56
     * @var bool
57
     */
58
    private $supportFallbackLoader;
59
60 23
    public function __construct(ResourceRepository $repo, UrlGenerator $urlGenerator = null, $supportFallbackLoader = false)
61
    {
62 23
        $this->repo = $repo;
63 23
        $this->urlGenerator = $urlGenerator;
64 23
        $this->supportFallbackLoader = $supportFallbackLoader;
65 23
    }
66
67
    /**
68
     * Returns the name of the extension.
69
     *
70
     * @return string The extension name
71
     */
72 23
    public function getName()
73
    {
74 23
        return 'puli';
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80 23
    public function getNodeVisitors()
81
    {
82
        return array(
83 23
            new PuliDirTagger(),
84 23
            new TemplatePathResolver($this->repo, $this->urlGenerator, $this->supportFallbackLoader),
85
        );
86
    }
87
88
    /**
89
     * {@inheritdoc}
90
     */
91 23
    public function getTokenParsers()
92
    {
93 23
        return array(new LoadedByPuliTokenParser());
94
    }
95
96
    /**
97
     * {@inheritdoc}
98
     */
99 23
    public function getFunctions()
100
    {
101
        // A RuntimeException is thrown by TemplatePathResolver if urlGenerator is missing.
102
103
        return array(
104 23
            new Twig_SimpleFunction('resource_url', array($this->urlGenerator, 'generateUrl')),
105
        );
106
    }
107
}
108