Completed
Push — master ( 018295...52e520 )
by Alex
03:55
created

FilesystemLoader::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
crap 1
1
<?php
2
3
namespace AlexMasterov\EquipTwig\Loader;
4
5
use AlexMasterov\EquipTwig\Exception\LoaderException;
6
use Twig_LoaderInterface;
7
use Twig_Source;
8
9
final class FilesystemLoader implements Twig_LoaderInterface
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var array
18
     */
19
    private $fileExtensions = [];
20
21
    /**
22
     * @var array
23
     */
24
    private $cache = [];
25
26
    /**
27
     * @param string $path           The template directory path
28
     * @param array  $fileExtensions The template file extensions
29
     */
30 6
    public function __construct(
31
        $path,
32
        array $fileExtensions = ['html.twig', 'twig']
33
    ) {
34 6
        $this->path = $path;
35 6
        $this->fileExtensions = $fileExtensions;
36 6
    }
37
38
    public function getSource($name)
39
    {
40
        return file_get_contents($this->template($name));
41 4
    }
42
43 4
    public function getSourceContext($name)
44
    {
45
        $code = file_get_contents($this->template($name));
46
47
        return new Twig_Source($code, $name);
48
    }
49 1
50
    /**
51 1
     * @inheritDoc
52
     */
53
    public function getCacheKey($name)
54
    {
55
        return $this->template($name);
56
    }
57 1
58
    /**
59 1
     * @inheritDoc
60
     */
61
    public function isFresh($name, $time)
62
    {
63
        return filemtime($this->template($name)) <= $time;
64
    }
65 1
66
    /**
67 1
     * @inheritDoc
68 1
     */
69
    public function exists($name)
70
    {
71 1
        if (isset($this->cache[$name])) {
72
            return true;
73
        }
74
75
        return (bool) $this->findTemplate($name);
76
    }
77
78
    /**
79
     * @param string $name
80
     *
81
     * @return string
82 5
     *
83
     * @throws LoaderException
84 5
     *  When $name is not found.
85 1
     */
86
    private function template($name)
87
    {
88 5
        if (isset($this->cache[$name])) {
89 5
            return $this->cache[$name];
90 1
        }
91
92
        $found = $this->findTemplate($name);
93 4
        if (null === $found) {
94
            throw LoaderException::notFound($name, $this->path);
95
        }
96
97
        return $this->cache[$name] = $found;
98
    }
99
100
    /**
101 5
     * @param string $name
102
     *
103 5
     * @return string|null
104
     */
105 5
    private function findTemplate($name)
106 5
    {
107 5
        $files = $this->possibleTemplateFiles($name);
108 5
109
        foreach($files as $file) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
110
            $filepath = $this->path . DIRECTORY_SEPARATOR . $file;
111
            if (is_file($filepath) && is_readable($filepath)) {
112 1
                return realpath($filepath);
113
            }
114
        }
115
116
        return null;
117
    }
118
119
    /**
120 5
     * @param string $name
121
     *
122 5
     * @return array
123
     */
124 5
    private function possibleTemplateFiles($name)
125 5
    {
126 5
        $name = $this->normalizeName($name);
127
128
        $templates = [$name];
129 5
        foreach($this->fileExtensions as $extension) {
0 ignored issues
show
Coding Style introduced by
Expected 1 space after FOREACH keyword; 0 found
Loading history...
130
            $templates[] = "{$name}.{$extension}";
131
        }
132
133
        return $templates;
134
    }
135
136
    /**
137 5
     * @param string $name
138
     *
139 5
     * @return string
140 5
     */
141
    private function normalizeName($name)
142
    {
143
        return preg_replace('#/{2,}#',
144
            DIRECTORY_SEPARATOR,
145
            str_replace('\\', DIRECTORY_SEPARATOR, $name)
146
        );
147
    }
148
}
149