1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Goetas\Twital; |
4
|
|
|
|
5
|
|
|
use Goetas\Twital\SourceAdapter\HTML5Adapter; |
6
|
|
|
use Goetas\Twital\SourceAdapter\XHTMLAdapter; |
7
|
|
|
use Goetas\Twital\SourceAdapter\XMLAdapter; |
8
|
|
|
use Twig\Environment; |
9
|
|
|
use Twig\Error\LoaderError; |
10
|
|
|
use Twig\Loader\ExistsLoaderInterface; |
11
|
|
|
use Twig\Loader\LoaderInterface; |
12
|
|
|
use Twig\Loader\SourceContextLoaderInterface; |
13
|
|
|
use Twig\Source; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @author Martin Hasoň <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
trait TwitalLoaderTrait |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* Array of patterns used to decide if a template is twital-compilable or not. |
22
|
|
|
* Items are strings or callbacks |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $sourceAdapters = array(); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* The internal Twital compiler |
30
|
|
|
* |
31
|
|
|
* @var Twital |
32
|
|
|
*/ |
33
|
|
|
protected $twital; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* The wrapped Twig loader |
37
|
|
|
* |
38
|
|
|
* @var LoaderInterface|\Twig_LoaderInterface |
39
|
|
|
*/ |
40
|
|
|
protected $loader; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Creates a new Twital loader. |
44
|
|
|
* |
45
|
|
|
* @param LoaderInterface $loader |
46
|
|
|
* @param Twital $twital |
47
|
|
|
* @param bool $addDefaults If NULL, some standard rules will be used (`*.twital.*` and `*.twital`). |
48
|
|
|
*/ |
49
|
|
|
public function __construct(LoaderInterface $loader = null, Twital $twital = null, $addDefaults = true) |
50
|
|
|
{ |
51
|
|
|
$this->loader = $loader; |
52
|
|
|
$this->twital = $twital; |
53
|
|
|
|
54
|
|
|
if ($addDefaults === true || (is_array($addDefaults) && in_array('html', $addDefaults))) { |
55
|
|
|
$this->addSourceAdapter('/\.twital\.html$/i', new HTML5Adapter()); |
56
|
|
|
} |
57
|
|
|
if ($addDefaults === true || (is_array($addDefaults) && in_array('xml', $addDefaults))) { |
58
|
|
|
$this->addSourceAdapter('/\.twital\.xml$/i', new XMLAdapter()); |
59
|
|
|
} |
60
|
|
|
if ($addDefaults === true || (is_array($addDefaults) && in_array('xhtml', $addDefaults))) { |
61
|
|
|
$this->addSourceAdapter('/\.twital\.xhtml$/i', new XHTMLAdapter()); |
62
|
|
|
} |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Add a new pattern that can decide if a template is twital-compilable or not. |
67
|
|
|
* If $pattern is a string, then must be a valid regex that matches the template filename. |
68
|
|
|
* If $pattern is a callback, then must return true if the template is compilable, false otherwise. |
69
|
|
|
* |
70
|
|
|
* @param string|callback $pattern |
71
|
|
|
* @param SourceAdapter $adapter |
72
|
|
|
* @return TwitalLoader |
73
|
|
|
*/ |
74
|
|
|
public function addSourceAdapter($pattern, SourceAdapter $adapter) |
75
|
|
|
{ |
76
|
|
|
$this->sourceAdapters[$pattern] = $adapter; |
77
|
|
|
|
78
|
|
|
return $this; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Get all patterns used to choose if a template is twital-compilable or not |
83
|
|
|
* |
84
|
|
|
* @return array: |
85
|
|
|
*/ |
86
|
|
|
public function getSourceAdapters() |
87
|
|
|
{ |
88
|
|
|
return $this->sourceAdapters; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Decide if a template is twital-compilable or not. |
93
|
|
|
* |
94
|
|
|
* @param string $name |
95
|
|
|
* @return SourceAdapter |
96
|
|
|
*/ |
97
|
|
|
public function getSourceAdapter($name) |
98
|
|
|
{ |
99
|
|
|
foreach (array_reverse($this->sourceAdapters) as $pattern => $adapter) { |
100
|
|
|
if (preg_match($pattern, $name)) { |
101
|
|
|
return $adapter; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Get the wrapped Twig loader |
110
|
|
|
* |
111
|
|
|
* @return LoaderInterface|\Twig_LoaderInterface |
112
|
|
|
*/ |
113
|
|
|
public function getLoader() |
114
|
|
|
{ |
115
|
|
|
return $this->loader; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Set the wrapped Twig loader |
120
|
|
|
* |
121
|
|
|
* @param LoaderInterface|\Twig_LoaderInterface $loader |
122
|
|
|
* @return TwitalLoader |
123
|
|
|
*/ |
124
|
|
|
public function setLoader($loader) |
125
|
|
|
{ |
126
|
|
|
$this->loader = $loader; |
127
|
|
|
|
128
|
|
|
return $this; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @return Twital |
133
|
|
|
*/ |
134
|
|
|
public function getTwital() |
135
|
|
|
{ |
136
|
|
|
if ($this->twital === null) { |
137
|
|
|
$this->twital = new Twital(); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
return $this->twital; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
private function doGetSourceContext($name) |
144
|
|
|
{ |
145
|
|
|
if (Environment::MAJOR_VERSION >= 2 || $this->loader instanceof SourceContextLoaderInterface) { |
146
|
|
|
$originalContext = $this->loader->getSourceContext($name); |
147
|
|
|
$code = $originalContext->getCode(); |
148
|
|
|
$path = $originalContext->getPath(); |
149
|
|
|
} else { |
150
|
|
|
$code = $this->loader->getSource($name); |
151
|
|
|
$path = null; |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
if ($adapter = $this->getSourceAdapter($name)) { |
155
|
|
|
$code = $this->getTwital()->compile($adapter, $code); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
return new Source($code, $name, $path); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private function doExists($name) |
162
|
|
|
{ |
163
|
|
|
if (Environment::MAJOR_VERSION >= 2 || $this->loader instanceof ExistsLoaderInterface) { |
164
|
|
|
return $this->loader->exists($name); |
165
|
|
|
} else { |
166
|
|
|
try { |
167
|
|
|
$this->getSourceContext($name); |
|
|
|
|
168
|
|
|
|
169
|
|
|
return true; |
170
|
|
|
} catch (LoaderError $e) { |
171
|
|
|
return false; |
172
|
|
|
} |
173
|
|
|
} |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.