1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is a part of Sculpin. |
5
|
|
|
* |
6
|
|
|
* (c) Dragonfly Development Inc. |
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 Symplify\PHP7_Sculpin\Bundle\TwigBundle\Twig; |
13
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Bundle\TwigBundle\FilesystemLoader; |
15
|
|
|
use Twig_LoaderInterface; |
16
|
|
|
|
17
|
|
|
final class FlexibleExtensionFilesystemLoader implements Twig_LoaderInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var FilesystemLoader |
21
|
|
|
*/ |
22
|
|
|
private $filesystemLoader; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private $cachedCacheKey = []; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private $cachedCacheKeyExtension = []; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $cachedCacheKeyException = []; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string[] |
41
|
|
|
*/ |
42
|
|
|
private $extensions; |
43
|
|
|
|
44
|
|
|
public function __construct(string $sourceDir, array $sourcePaths, array $paths, array $extensions) |
45
|
|
|
{ |
46
|
|
|
$mappedSourcePaths = array_map(function ($path) use ($sourceDir) { |
47
|
|
|
return $sourceDir.'/'.$path; |
48
|
|
|
}, $sourcePaths); |
49
|
|
|
|
50
|
|
|
$allPaths = array_merge( |
51
|
|
|
array_filter($mappedSourcePaths, function ($path) { |
52
|
|
|
return file_exists($path); |
53
|
|
|
}), |
54
|
|
|
array_filter($paths, function ($path) { |
55
|
|
|
return file_exists($path); |
56
|
|
|
}) |
57
|
|
|
); |
58
|
|
|
|
59
|
|
|
$this->filesystemLoader = new FilesystemLoader($allPaths); |
60
|
|
|
|
61
|
|
|
$this->extensions = array_map(function ($ext) { |
62
|
|
|
return $ext ? '.'.$ext : $ext; |
63
|
|
|
}, $extensions); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* {@inheritdoc} |
68
|
|
|
*/ |
69
|
|
View Code Duplication |
public function getSource($name) |
|
|
|
|
70
|
|
|
{ |
71
|
|
|
$this->getCacheKey($name); |
72
|
|
|
|
73
|
|
|
$extension = $this->cachedCacheKeyExtension[$name]; |
74
|
|
|
|
75
|
|
|
return $this->filesystemLoader->getSource($name.$extension); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function getCacheKey($name) |
82
|
|
|
{ |
83
|
|
|
if (isset($this->cachedCacheKey[$name])) { |
84
|
|
|
$extension = $this->cachedCacheKeyExtension[$name]; |
85
|
|
|
|
86
|
|
|
return $this->cachedCacheKey[$name] = $this->filesystemLoader->getCacheKey($name.$extension); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (isset($this->cachedCacheKeyException[$name])) { |
90
|
|
|
throw $this->cachedCacheKeyException[$name]; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
foreach ($this->extensions as $extension) { |
94
|
|
|
try { |
95
|
|
|
$this->cachedCacheKey[$name] = $this->filesystemLoader->getCacheKey($name.$extension); |
96
|
|
|
$this->cachedCacheKeyExtension[$name] = $extension; |
97
|
|
|
|
98
|
|
|
return $this->cachedCacheKey[$name]; |
99
|
|
|
} catch (\Twig_Error_Loader $e) { |
|
|
|
|
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
throw $this->cachedCacheKeyException[$name] = new \Twig_Error_Loader( |
104
|
|
|
sprintf('Template "%s" is not defined.', $name) |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* {@inheritdoc} |
110
|
|
|
*/ |
111
|
|
View Code Duplication |
public function isFresh($name, $time) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$this->getCacheKey($name); |
114
|
|
|
|
115
|
|
|
$extension = $this->cachedCacheKeyExtension[$name]; |
116
|
|
|
|
117
|
|
|
return $this->filesystemLoader->isFresh($name.$extension, $time); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
public function clean() |
121
|
|
|
{ |
122
|
|
|
$this->cachedCacheKey = []; |
123
|
|
|
$this->cachedCacheKeyExtension = []; |
124
|
|
|
$this->cachedCacheKeyException = []; |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.