|
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; |
|
13
|
|
|
|
|
14
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SculpinEvents; |
|
15
|
|
|
use Symplify\PHP7_Sculpin\Core\Event\SourceSetEvent; |
|
16
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
|
17
|
|
|
|
|
18
|
|
|
final class FlexibleExtensionFilesystemLoader implements \Twig_LoaderInterface, EventSubscriberInterface |
|
19
|
|
|
{ |
|
20
|
|
|
/** |
|
21
|
|
|
* @var FilesystemLoader |
|
22
|
|
|
*/ |
|
23
|
|
|
private $filesystemLoader; |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* @var array |
|
27
|
|
|
*/ |
|
28
|
|
|
private $cachedCacheKey = []; |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @var array |
|
32
|
|
|
*/ |
|
33
|
|
|
private $cachedCacheKeyExtension = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @var array |
|
37
|
|
|
*/ |
|
38
|
|
|
private $cachedCacheKeyException = []; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Constructor. |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $sourceDir |
|
44
|
|
|
* @param string[] $sourcePaths |
|
45
|
|
|
* @param string[] $paths |
|
46
|
|
|
* @param string[] $extensions |
|
47
|
|
|
*/ |
|
48
|
|
|
public function __construct($sourceDir, array $sourcePaths, array $paths, array $extensions) |
|
49
|
|
|
{ |
|
50
|
|
|
$mappedSourcePaths = array_map(function ($path) use ($sourceDir) { |
|
51
|
|
|
return $sourceDir.'/'.$path; |
|
52
|
|
|
}, $sourcePaths); |
|
53
|
|
|
|
|
54
|
|
|
$allPaths = array_merge( |
|
55
|
|
|
array_filter($mappedSourcePaths, function ($path) { |
|
56
|
|
|
return file_exists($path); |
|
57
|
|
|
}), |
|
58
|
|
|
array_filter($paths, function ($path) { |
|
59
|
|
|
return file_exists($path); |
|
60
|
|
|
}) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->filesystemLoader = new FilesystemLoader($allPaths); |
|
64
|
|
|
$this->extensions = array_map(function ($ext) { |
|
|
|
|
|
|
65
|
|
|
return $ext ? '.'.$ext : $ext; |
|
66
|
|
|
}, $extensions); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* {@inheritdoc} |
|
71
|
|
|
*/ |
|
72
|
|
View Code Duplication |
public function getSource($name) |
|
|
|
|
|
|
73
|
|
|
{ |
|
74
|
|
|
$this->getCacheKey($name); |
|
75
|
|
|
|
|
76
|
|
|
$extension = $this->cachedCacheKeyExtension[$name]; |
|
77
|
|
|
|
|
78
|
|
|
return $this->filesystemLoader->getSource($name.$extension); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* {@inheritdoc} |
|
83
|
|
|
*/ |
|
84
|
|
|
public function getCacheKey($name) |
|
85
|
|
|
{ |
|
86
|
|
|
if (isset($this->cachedCacheKey[$name])) { |
|
87
|
|
|
$extension = $this->cachedCacheKeyExtension[$name]; |
|
88
|
|
|
|
|
89
|
|
|
return $this->cachedCacheKey[$name] = $this->filesystemLoader->getCacheKey($name.$extension); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
if (isset($this->cachedCacheKeyException[$name])) { |
|
93
|
|
|
throw $this->cachedCacheKeyException[$name]; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
foreach ($this->extensions as $extension) { |
|
97
|
|
|
try { |
|
98
|
|
|
$this->cachedCacheKey[$name] = $this->filesystemLoader->getCacheKey($name.$extension); |
|
99
|
|
|
$this->cachedCacheKeyExtension[$name] = $extension; |
|
100
|
|
|
|
|
101
|
|
|
return $this->cachedCacheKey[$name]; |
|
102
|
|
|
} catch (\Twig_Error_Loader $e) { |
|
|
|
|
|
|
103
|
|
|
} |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
throw $this->cachedCacheKeyException[$name] = new \Twig_Error_Loader( |
|
107
|
|
|
sprintf('Template "%s" is not defined.', $name) |
|
108
|
|
|
); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
|
View Code Duplication |
public function isFresh($name, $time) |
|
|
|
|
|
|
115
|
|
|
{ |
|
116
|
|
|
$this->getCacheKey($name); |
|
117
|
|
|
|
|
118
|
|
|
$extension = $this->cachedCacheKeyExtension[$name]; |
|
119
|
|
|
|
|
120
|
|
|
return $this->filesystemLoader->isFresh($name.$extension, $time); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* {@inheritdoc} |
|
125
|
|
|
*/ |
|
126
|
|
|
public static function getSubscribedEvents() : array |
|
127
|
|
|
{ |
|
128
|
|
|
return [ |
|
129
|
|
|
SculpinEvents::EVENT_BEFORE_RUN => 'beforeRun', |
|
130
|
|
|
]; |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
public function beforeRun(SourceSetEvent $sourceSetEvent) |
|
134
|
|
|
{ |
|
135
|
|
|
if ($sourceSetEvent->sourceSet()->newSources()) { |
|
136
|
|
|
$this->cachedCacheKey = []; |
|
137
|
|
|
$this->cachedCacheKeyExtension = []; |
|
138
|
|
|
$this->cachedCacheKeyException = []; |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: