Completed
Push — master ( 860392...c518de )
by Alex
02:22
created

FilesystemLoader::template()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 13
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 13
ccs 0
cts 11
cp 0
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 12

1 Method

Rating   Name   Duplication   Size   Complexity  
A FilesystemLoader::getFileExtensions() 0 4 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
     * @return string
77
     */
78 1
    public function getPath()
79
    {
80 1
        return $this->path;
81
    }
82
83
    /**
84
     * @return array
85
     */
86 1
    public function getFileExtensions()
87
    {
88 1
        return $this->fileExtensions;
89
    }
90
91
    /**
92
     * @throws LoaderException When $name is not found
93
     *
94
     * @param string $name
95
     *
96
     * @return string
97
     */
98 5
    private function template($name)
99
    {
100 5
        if (isset($this->cache[$name])) {
101 1
            return $this->cache[$name];
102
        }
103
104 5
        $found = $this->findTemplate($name);
105 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...
106 1
            throw LoaderException::notFound($name, $this->path);
107
        }
108
109 4
        return $this->cache[$name] = $found;
110
    }
111
112
    /**
113
     * @param string $name
114
     *
115
     * @return string|null
116
     */
117 5
    private function findTemplate($name)
118
    {
119 5
        $files = $this->possibleTemplateFiles($name);
120
121 5
        foreach($files as $file) {
122 5
            $filepath = $this->path . DIRECTORY_SEPARATOR . $file;
123 5
            if (is_readable($filepath)) {
124 5
                return realpath($filepath);
125
            }
126
        }
127
128 1
        return null;
129
    }
130
131
    /**
132
     * @param string $name
133
     *
134
     * @return array
135
     */
136 5
    private function possibleTemplateFiles($name)
137
    {
138 5
        $name = $this->normalizeName($name);
139
140 5
        $templates = [$name];
141 5
        foreach($this->fileExtensions as $extension) {
142 5
            $templates[] = "$name.$extension";
143
        }
144
145 5
        return $templates;
146
    }
147
148
    /**
149
     * @param string $name
150
     *
151
     * @return string
152
     */
153 5
    private function normalizeName($name)
154
    {
155 5
        return preg_replace('#/{2,}#', DIRECTORY_SEPARATOR,
156 5
            str_replace('\\', DIRECTORY_SEPARATOR, $name)
157
        );
158
    }
159
}
160