1 | <?php |
||
17 | trait TwitalLoaderTrait |
||
18 | { |
||
19 | /** |
||
20 | * Array of patterns used to decide if a template is twital-compilable or not. |
||
21 | * Items are strings or callbacks |
||
22 | * |
||
23 | * @var array |
||
24 | */ |
||
25 | protected $sourceAdapters = array(); |
||
26 | |||
27 | /** |
||
28 | * The internal Twital compiler |
||
29 | * |
||
30 | * @var Twital |
||
31 | */ |
||
32 | protected $twital; |
||
33 | |||
34 | /** |
||
35 | * The wrapped Twig loader |
||
36 | * |
||
37 | * @var LoaderInterface|\Twig_LoaderInterface |
||
38 | */ |
||
39 | protected $loader; |
||
40 | |||
41 | private $twigMajorVersion; |
||
42 | |||
43 | /** |
||
44 | * Add a new pattern that can decide if a template is twital-compilable or not. |
||
45 | * If $pattern is a string, then must be a valid regex that matches the template filename. |
||
46 | * If $pattern is a callback, then must return true if the template is compilable, false otherwise. |
||
47 | * |
||
48 | * @param string|callback $pattern |
||
49 | * @param SourceAdapter $adapter |
||
50 | * @return TwitalLoader |
||
51 | */ |
||
52 | public function addSourceAdapter($pattern, SourceAdapter $adapter) |
||
58 | |||
59 | /** |
||
60 | * Get all patterns used to choose if a template is twital-compilable or not |
||
61 | * |
||
62 | * @return array: |
||
63 | */ |
||
64 | public function getSourceAdapters() |
||
68 | |||
69 | /** |
||
70 | * Decide if a template is twital-compilable or not. |
||
71 | * |
||
72 | * @param string $name |
||
73 | * @return SourceAdapter |
||
74 | */ |
||
75 | public function getSourceAdapter($name) |
||
85 | |||
86 | /** |
||
87 | * Get the wrapped Twig loader |
||
88 | * |
||
89 | * @return LoaderInterface|\Twig_LoaderInterface |
||
90 | */ |
||
91 | public function getLoader() |
||
95 | |||
96 | /** |
||
97 | * Set the wrapped Twig loader |
||
98 | * |
||
99 | * @param LoaderInterface|\Twig_LoaderInterface $loader |
||
100 | * @return TwitalLoader |
||
101 | */ |
||
102 | public function setLoader($loader) |
||
108 | |||
109 | /** |
||
110 | * @return Twital |
||
111 | */ |
||
112 | public function getTwital() |
||
120 | |||
121 | protected function doGetSourceContext($name) |
||
122 | { |
||
123 | if ($this->getTwigMajorVersion() >= 2 || $this->loader instanceof SourceContextLoaderInterface) { |
||
124 | $originalContext = $this->loader->getSourceContext($name); |
||
125 | $code = $originalContext->getCode(); |
||
126 | $path = $originalContext->getPath(); |
||
127 | } else { |
||
128 | $code = $this->loader->getSource($name); |
||
129 | $path = null; |
||
130 | } |
||
131 | |||
132 | if ($adapter = $this->getSourceAdapter($name)) { |
||
133 | $code = $this->getTwital()->compile($adapter, $code); |
||
134 | } |
||
135 | |||
136 | return array($code, $name, $path); |
||
137 | } |
||
138 | |||
139 | /** |
||
140 | * Creates a new Twital loader. |
||
141 | * |
||
142 | * @param LoaderInterface|\Twig_LoaderInterface $loader |
||
143 | * @param Twital $twital |
||
144 | * @param bool $addDefaults If NULL, some standard rules will be used (`*.twital.*` and `*.twital`). |
||
145 | */ |
||
146 | private function doConstruct($loader = null, Twital $twital = null, $addDefaults = true) |
||
147 | { |
||
148 | $this->loader = $loader; |
||
149 | $this->twital = $twital; |
||
150 | |||
151 | if ($addDefaults === true || (is_array($addDefaults) && in_array('html', $addDefaults))) { |
||
152 | $this->addSourceAdapter('/\.twital\.html$/i', new HTML5Adapter()); |
||
153 | } |
||
154 | if ($addDefaults === true || (is_array($addDefaults) && in_array('xml', $addDefaults))) { |
||
155 | $this->addSourceAdapter('/\.twital\.xml$/i', new XMLAdapter()); |
||
156 | } |
||
157 | if ($addDefaults === true || (is_array($addDefaults) && in_array('xhtml', $addDefaults))) { |
||
158 | $this->addSourceAdapter('/\.twital\.xhtml$/i', new XHTMLAdapter()); |
||
159 | } |
||
160 | } |
||
161 | |||
162 | private function doExists($name) |
||
178 | |||
179 | private function getTwigMajorVersion() |
||
187 | } |
||
188 |
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.