Completed
Push — master ( 7329b6...651e35 )
by Alex
02:45
created

FilesystemLoader   A

Complexity

Total Complexity 16

Size/Duplication

Total Lines 135
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 90%

Importance

Changes 5
Bugs 0 Features 2
Metric Value
wmc 16
c 5
b 0
f 2
lcom 1
cbo 1
dl 0
loc 135
ccs 36
cts 40
cp 0.9
rs 10

9 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A getSource() 0 4 1
A isFresh() 0 4 1
A findTemplate() 0 13 4
A possibleTemplateFiles() 0 11 2
A normalizeName() 0 6 1
A getCacheKey() 0 4 1
A exists() 0 8 2
A template() 0 13 3
1
<?php
2
3
namespace Asmaster\EquipTwig\Loader;
4
5
use Asmaster\EquipTwig\Exception\LoaderException;
6
use Twig_ExistsLoaderInterface as TwigExistsLoader;
7
use Twig_LoaderInterface as TwigLoader;
8
9
final class FilesystemLoader implements TwigLoader, TwigExistsLoader
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
    /**
39
     * @inheritDoc
40
     */
41 4
    public function getSource($name)
42
    {
43 4
        return file_get_contents($this->template($name));
44
    }
45
46
    /**
47
     * @inheritDoc
48
     */
49
    public function getCacheKey($name)
50
    {
51
        return $this->template($name);
52
    }
53
54
    /**
55
     * @inheritDoc
56
     */
57 1
    public function isFresh($name, $time)
58
    {
59 1
        return filemtime($this->template($name)) <= $time;
60
    }
61
62
    /**
63
     * @inheritDoc
64
     */
65 1
    public function exists($name)
66
    {
67 1
        if (isset($this->cache[$name])) {
68
            return true;
69
        }
70
71 1
        return (bool) $this->findTemplate($name);
72
    }
73
74
    /**
75
     * @param string $name
76
     *
77
     * @return string
78
     *
79
     * @throws LoaderException
80
     *  When $name is not found.
81
     */
82 5
    private function template($name)
83
    {
84 5
        if (isset($this->cache[$name])) {
85
            return $this->cache[$name];
86
        }
87
88 5
        $found = $this->findTemplate($name);
89 5
        if (null !== $found) {
90 4
            throw LoaderException::notFound($name, $this->path);
91
        }
92
93 1
        return $this->cache[$name] = $found;
94
    }
95
96
    /**
97
     * @param string $name
98
     *
99
     * @return string|null
100
     */
101 5
    private function findTemplate($name)
102
    {
103 5
        $files = $this->possibleTemplateFiles($name);
104
105 5
        foreach($files as $file) {
106 5
            $filepath = $this->path . DIRECTORY_SEPARATOR . $file;
107 5
            if (is_file($filepath) && is_readable($filepath)) {
108 4
                return realpath($filepath);
109
            }
110 1
        }
111
112 1
        return null;
113
    }
114
115
    /**
116
     * @param string $name
117
     *
118
     * @return array
119
     */
120 5
    private function possibleTemplateFiles($name)
121
    {
122 5
        $name = $this->normalizeName($name);
123
124 5
        $templates = [$name];
125 5
        foreach($this->fileExtensions as $extension) {
126 5
            $templates[] = "{$name}.{$extension}";
127 5
        }
128
129 5
        return $templates;
130
    }
131
132
    /**
133
     * @param string $name
134
     *
135
     * @return string
136
     */
137 5
    private function normalizeName($name)
138
    {
139 5
        return preg_replace('#/{2,}#', DIRECTORY_SEPARATOR,
140 5
            str_replace('\\', DIRECTORY_SEPARATOR, $name)
141 5
        );
142
    }
143
}
144