Completed
Push — master ( 357847...e38da2 )
by Alex
02:40
created

FilesystemLoader::getSource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 1
crap 1
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
1 ignored issue
show
Deprecated Code introduced by
The interface Twig_ExistsLoaderInterface has been deprecated with message: since 1.12 (to be removed in 3.0)

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
10
{
11
    /**
12
     * @var string
13
     */
14
    private $path;
15
16
    /**
17
     * @var array
18
     */
19
    private $fileExtensions = ['html.twig', 'twig'];
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($path, array $fileExtensions = null)
31
    {
32 6
        $this->path = $path;
33
34 6
        if ($fileExtensions) {
35 1
            $this->fileExtensions = $fileExtensions;
36
        }
37 6
    }
38
39
    /**
40
     * @inheritDoc
41
     */
42 4
    public function getSource($name)
43
    {
44 4
        return file_get_contents($this->template($name));
45
    }
46
47
    /**
48
     * @inheritDoc
49
     */
50 1
    public function getCacheKey($name)
51
    {
52 1
        return $this->template($name);
53
    }
54
55
    /**
56
     * @inheritDoc
57
     */
58 1
    public function isFresh($name, $time)
59
    {
60 1
        return filemtime($this->template($name)) <= $time;
61
    }
62
63
    /**
64
     * @inheritDoc
65
     */
66 1
    public function exists($name)
67
    {
68 1
        if (isset($this->cache[$name])) {
69 1
            return true;
70
        }
71
72 1
        return (bool) $this->findTemplate($name);
73
    }
74
75
    /**
76
     * @param string $name
77
     *
78
     * @return string
79
     *
80
     * @throws LoaderException
81
     * When $name is not found
82
     */
83 5
    private function template($name)
84
    {
85 5
        if (isset($this->cache[$name])) {
86 1
            return $this->cache[$name];
87
        }
88
89 5
        $found = $this->findTemplate($name);
90 5
        if (!$found) {
1 ignored issue
show
Bug Best Practice introduced by
The expression $found of type string|null is loosely compared to false; this is ambiguous if the string can be empty. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For string values, the empty string '' is a special case, in particular the following results might be unexpected:

''   == false // true
''   == null  // true
'ab' == false // false
'ab' == null  // false

// It is often better to use strict comparison
'' === false // false
'' === null  // false
Loading history...
91 1
            throw LoaderException::notFound($name, $this->path);
92
        }
93
94 4
        return $this->cache[$name] = $found;
95
    }
96
97
    /**
98
     * @param string $name
99
     *
100
     * @return string|null
101
     */
102 5
    private function findTemplate($name)
103
    {
104 5
        $files = $this->possibleTemplateFiles($name);
105
106 5
        foreach($files as $file) {
107 5
            $filepath = $this->path . DIRECTORY_SEPARATOR . $file;
108 5
            if (is_readable($filepath)) {
109 5
                return realpath($filepath);
110
            }
111
        }
112
113 1
        return null;
114
    }
115
116
    /**
117
     * @param string $name
118
     *
119
     * @return array
120
     */
121 5
    private function possibleTemplateFiles($name)
122
    {
123 5
        $name = $this->normalizeName($name);
124
125 5
        $templates = [$name];
126 5
        foreach($this->fileExtensions as $extension) {
127 5
            $templates[] = "$name.$extension";
128
        }
129
130 5
        return $templates;
131
    }
132
133
    /**
134
     * @param string $name
135
     *
136
     * @return string
137
     */
138 5
    private function normalizeName($name)
139
    {
140 5
        return preg_replace('#/{2,}#', DIRECTORY_SEPARATOR,
141 5
            str_replace('\\', DIRECTORY_SEPARATOR, $name)
142
        );
143
    }
144
}
145