1 | <?php |
||
49 | class Configurator implements ConfigProvider |
||
50 | { |
||
51 | /** |
||
52 | * @var AttributeFilterCollection Dynamically-populated collection of AttributeFilter instances |
||
53 | */ |
||
54 | public $attributeFilters; |
||
55 | |||
56 | /** |
||
57 | * @var BundleGenerator Default bundle generator |
||
58 | */ |
||
59 | public $bundleGenerator; |
||
60 | |||
61 | /** |
||
62 | * @var JavaScript JavaScript manipulation object |
||
63 | */ |
||
64 | public $javascript; |
||
65 | |||
66 | /** |
||
67 | * @var PluginCollection Loaded plugins |
||
68 | */ |
||
69 | public $plugins; |
||
70 | |||
71 | /** |
||
72 | * @var array Array of variables that are available to the filters during parsing |
||
73 | */ |
||
74 | public $registeredVars; |
||
75 | |||
76 | /** |
||
77 | * @var Rendering Rendering configuration |
||
78 | */ |
||
79 | public $rendering; |
||
80 | |||
81 | /** |
||
82 | * @var Ruleset Rules that apply at the root of the text |
||
83 | */ |
||
84 | public $rootRules; |
||
85 | |||
86 | /** |
||
87 | * @var RulesGenerator Generator used by $this->getRenderer() |
||
88 | */ |
||
89 | public $rulesGenerator; |
||
90 | |||
91 | /** |
||
92 | * @var TagCollection Tags repository |
||
93 | */ |
||
94 | public $tags; |
||
95 | |||
96 | /** |
||
97 | * @var TemplateChecker Default template checker |
||
98 | */ |
||
99 | public $templateChecker; |
||
100 | |||
101 | /** |
||
102 | * @var TemplateNormalizer Default template normalizer |
||
103 | */ |
||
104 | public $templateNormalizer; |
||
105 | |||
106 | /** |
||
107 | * Constructor |
||
108 | * |
||
109 | * Prepares the collections that hold tags and filters, the UrlConfig object as well as the |
||
110 | * various helpers required to generate a full config. |
||
111 | */ |
||
112 | 45 | public function __construct() |
|
125 | |||
126 | /** |
||
127 | * Magic __get automatically loads plugins, returns registered vars |
||
128 | * |
||
129 | * @param string $k Property name |
||
130 | * @return mixed |
||
131 | */ |
||
132 | 6 | public function __get($k) |
|
148 | |||
149 | /** |
||
150 | * Magic __isset checks existence in the plugins collection and registered vars |
||
151 | * |
||
152 | * @param string $k Property name |
||
153 | * @return bool |
||
154 | */ |
||
155 | 5 | public function __isset($k) |
|
164 | |||
165 | /** |
||
166 | * Magic __set adds to the plugins collection, registers vars |
||
167 | * |
||
168 | * @param string $k Property name |
||
169 | * @param mixed $v Property value |
||
170 | * @return mixed |
||
171 | */ |
||
172 | 2 | public function __set($k, $v) |
|
183 | |||
184 | /** |
||
185 | * Magic __set removes plugins from the plugins collection, unregisters vars |
||
186 | * |
||
187 | * @param string $k Property name |
||
188 | * @return mixed |
||
189 | */ |
||
190 | 2 | public function __unset($k) |
|
201 | |||
202 | /** |
||
203 | * Enable the creation of a JavaScript parser |
||
204 | * |
||
205 | * @return void |
||
206 | */ |
||
207 | 3 | public function enableJavaScript() |
|
214 | |||
215 | /** |
||
216 | * Finalize this configuration and return all the relevant objects |
||
217 | * |
||
218 | * @return array One "parser" element and one "renderer" element unless specified otherwise |
||
219 | */ |
||
220 | 11 | public function finalize() |
|
221 | { |
||
222 | 11 | $return = []; |
|
223 | |||
224 | // Finalize the plugins' config |
||
225 | 11 | $this->plugins->finalize(); |
|
226 | |||
227 | // Normalize the tags' templates |
||
228 | 11 | foreach ($this->tags as $tag) |
|
229 | { |
||
230 | 5 | $this->templateNormalizer->normalizeTag($tag); |
|
231 | } |
||
232 | |||
233 | // Create a renderer |
||
234 | 11 | $return['renderer'] = $this->rendering->getRenderer(); |
|
235 | |||
236 | // Add the generated tag rules |
||
237 | 11 | $this->addTagRules(); |
|
238 | |||
239 | // Prepare the parser config |
||
240 | $config = $this->asConfig(); |
||
241 | if (isset($this->javascript)) |
||
242 | { |
||
243 | $return['js'] = $this->javascript->getParser(ConfigHelper::filterConfig($config, 'JS')); |
||
244 | } |
||
245 | |||
246 | // Remove JS-specific data from the config |
||
247 | $config = ConfigHelper::filterConfig($config, 'PHP'); |
||
248 | ConfigHelper::optimizeArray($config); |
||
249 | |||
250 | // Create a parser |
||
251 | $return['parser'] = new Parser($config); |
||
252 | |||
253 | return $return; |
||
254 | } |
||
255 | |||
256 | /** |
||
257 | * Load a bundle into this configuration |
||
258 | * |
||
259 | * @param string $bundleName Name of the bundle |
||
260 | * @return void |
||
261 | */ |
||
262 | 2 | public function loadBundle($bundleName) |
|
274 | |||
275 | /** |
||
276 | * Create and save a bundle based on this configuration |
||
277 | * |
||
278 | * @param string $className Name of the bundle class |
||
279 | * @param string $filepath Path where to save the bundle file |
||
280 | * @param array $options Options passed to the bundle generator |
||
281 | * @return bool Whether the write succeeded |
||
282 | */ |
||
283 | 3 | public function saveBundle($className, $filepath, array $options = []) |
|
284 | { |
||
285 | 3 | $file = "<?php\n\n" . $this->bundleGenerator->generate($className, $options); |
|
286 | |||
287 | return (file_put_contents($filepath, $file) !== false); |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * Generate and return the complete config array |
||
292 | * |
||
293 | * @return array |
||
294 | */ |
||
295 | 9 | public function asConfig() |
|
343 | |||
344 | /** |
||
345 | * Add the rules generated by $this->rulesGenerator |
||
346 | * |
||
347 | * @return void |
||
348 | */ |
||
349 | 11 | protected function addTagRules() |
|
363 | } |