AbstractPathResolver::resolvePath()   B
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 27
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 5

Importance

Changes 0
Metric Value
dl 0
loc 27
ccs 8
cts 8
cp 1
rs 8.439
c 0
b 0
f 0
cc 5
eloc 9
nc 4
nop 2
crap 5
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\NodeVisitor;
13
14
use Puli\Repository\Api\ResourceRepository;
15
use Twig_BaseNodeVisitor;
16
use Twig_Environment;
17
use Twig_Node;
18
use Twig_Node_Module;
19
use Webmozart\PathUtil\Path;
20
21
/**
22
 * @since  1.0
23
 *
24
 * @author Bernhard Schussek <[email protected]>
25
 */
26
abstract class AbstractPathResolver extends Twig_BaseNodeVisitor
27
{
28
    /**
29
     * @var ResourceRepository
30
     */
31
    protected $repo;
32
33
    /**
34
     * @var string
35
     */
36
    protected $currentDir;
37
38 23
    public function __construct(ResourceRepository $repo)
39
    {
40 23
        $this->repo = $repo;
41 23
    }
42
43
    /**
44
     * {@inheritdoc}
45
     */
46 23
    protected function doEnterNode(Twig_Node $node, Twig_Environment $env)
47
    {
48
        // Remember the directory of the current file
49 23
        if ($node instanceof Twig_Node_Module && $node->hasAttribute('puli-dir')) {
50
            // Currently, it doesn't seem like Twig does recursive traversals
51
            // (i.e. starting the traversal of another module while a previous
52
            // one is still in progress). Thus we don't need to track existing
53
            // values here.
54 19
            $this->currentDir = $node->getAttribute('puli-dir');
55
        }
56
57 23
        return $node;
58
    }
59
60
    /**
61
     * {@inheritdoc}
62
     */
63 23
    protected function doLeaveNode(Twig_Node $node, Twig_Environment $env)
64
    {
65
        // Only process if the current directory was set
66 23
        if (null !== $this->currentDir) {
67 19
            if ($result = $this->processNode($node)) {
68 3
                $node = $result;
69
            }
70
        }
71
72 23
        return $node;
73
    }
74
75
    /**
76
     * {@inheritdoc}
77
     */
78 16
    protected function resolvePath($path, $checkPath = false)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
79
    {
80
        // Empty path? WTF I don't want to deal with this.
81 16
        if ('' === $path) {
82
            return $path;
83
        }
84
85
        // Absolute paths are fine
86 16
        if ('/' === $path[0]) {
87 7
            return $path;
88
        }
89
90
        // Resolve relative paths
91 11
        $absolutePath = Path::makeAbsolute($path, $this->currentDir);
92
93
        // With other loaders enabled, it may happen that a path looks like
94
        // a relative path, but is none, for example
95
        // "AcmeBlogBundle::index.html.twig", which doesn't start with a forward
96
        // slash. For this reason, if $checkPath is true, we should only resolve
97
        // paths if they actually exist in the repository.
98 11
        if (!$checkPath || $this->repo->contains($absolutePath)) {
99 11
            return $absolutePath;
100
        }
101
102
        // Return the path unchanged if $checkPath and the path does not exist
103 1
        return $path;
104
    }
105
106
    /**
107
     * @param Twig_Node $node
108
     *
109
     * @return Twig_Node|null
110
     */
111
    abstract protected function processNode(Twig_Node $node);
112
}
113