Verzeichnisstruktur phpBB-3.3.15
- Veröffentlicht
- 28.08.2024
So funktioniert es
|
Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück |
Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
Configurator.php
001 <?php
002
003 /**
004 * @package s9e\TextFormatter
005 * @copyright Copyright (c) 2010-2022 The s9e authors
006 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
007 */
008 namespace s9e\TextFormatter;
009
010 use InvalidArgumentException;
011 use RuntimeException;
012 use s9e\TextFormatter\Configurator\BundleGenerator;
013 use s9e\TextFormatter\Configurator\Collections\AttributeFilterCollection;
014 use s9e\TextFormatter\Configurator\Collections\PluginCollection;
015 use s9e\TextFormatter\Configurator\Collections\Ruleset;
016 use s9e\TextFormatter\Configurator\Collections\TagCollection;
017 use s9e\TextFormatter\Configurator\ConfigProvider;
018 use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
019 use s9e\TextFormatter\Configurator\Helpers\RulesHelper;
020 use s9e\TextFormatter\Configurator\JavaScript;
021 use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
022 use s9e\TextFormatter\Configurator\Rendering;
023 use s9e\TextFormatter\Configurator\RulesGenerator;
024 use s9e\TextFormatter\Configurator\TemplateChecker;
025 use s9e\TextFormatter\Configurator\TemplateNormalizer;
026 use s9e\TextFormatter\Configurator\UrlConfig;
027
028 /**
029 * @property Plugins\Autoemail\Configurator $Autoemail Autoemail plugin's configurator
030 * @property Plugins\Autoimage\Configurator $Autolink Autoimage plugin's configurator
031 * @property Plugins\Autolink\Configurator $Autolink Autolink plugin's configurator
032 * @property Plugins\Autovideo\Configurator $Autovideo Autovideo plugin's configurator
033 * @property Plugins\BBCodes\Configurator $BBCodes BBCodes plugin's configurator
034 * @property Plugins\Censor\Configurator $Censor Censor plugin's configurator
035 * @property Plugins\Emoji\Configurator $Emoji Emoji plugin's configurator
036 * @property Plugins\Emoticons\Configurator $Emoticons Emoticons plugin's configurator
037 * @property Plugins\Escaper\Configurator $Escaper Escaper plugin's configurator
038 * @property Plugins\FancyPants\Configurator $FancyPants FancyPants plugin's configurator
039 * @property Plugins\HTMLComments\Configurator $HTMLComments HTMLComments plugin's configurator
040 * @property Plugins\HTMLElements\Configurator $HTMLElements HTMLElements plugin's configurator
041 * @property Plugins\HTMLEntities\Configurator $HTMLEntities HTMLEntities plugin's configurator
042 * @property Plugins\Keywords\Configurator $Keywords Keywords plugin's configurator
043 * @property Plugins\Litedown\Configurator $Litedown Litedown plugin's configurator
044 * @property Plugins\MediaEmbed\Configurator $MediaEmbed MediaEmbed plugin's configurator
045 * @property Plugins\PipeTables\Configurator $PipeTables PipeTables plugin's configurator
046 * @property Plugins\Preg\Configurator $Preg Preg plugin's configurator
047 * @property UrlConfig $urlConfig Default URL config
048 */
049 class Configurator implements ConfigProvider
050 {
051 /**
052 * @var AttributeFilterCollection Dynamically-populated collection of AttributeFilter instances
053 */
054 public $attributeFilters;
055
056 /**
057 * @var BundleGenerator Default bundle generator
058 */
059 public $bundleGenerator;
060
061 /**
062 * @var JavaScript JavaScript manipulation object
063 */
064 public $javascript;
065
066 /**
067 * @var string PHP files header
068 */
069 public $phpHeader = '/**
070 * @package s9e\TextFormatter
071 * @copyright Copyright (c) 2010-2022 The s9e authors
072 * @license http://www.opensource.org/licenses/mit-license.php The MIT License
073 */';
074
075 /**
076 * @var PluginCollection Loaded plugins
077 */
078 public $plugins;
079
080 /**
081 * @var array Array of variables that are available to the filters during parsing
082 */
083 public $registeredVars;
084
085 /**
086 * @var Rendering Rendering configuration
087 */
088 public $rendering;
089
090 /**
091 * @var Ruleset Rules that apply at the root of the text
092 */
093 public $rootRules;
094
095 /**
096 * @var RulesGenerator Generator used by $this->getRenderer()
097 */
098 public $rulesGenerator;
099
100 /**
101 * @var TagCollection Tags repository
102 */
103 public $tags;
104
105 /**
106 * @var TemplateChecker Default template checker
107 */
108 public $templateChecker;
109
110 /**
111 * @var TemplateNormalizer Default template normalizer
112 */
113 public $templateNormalizer;
114
115 /**
116 * Constructor
117 *
118 * Prepares the collections that hold tags and filters, the UrlConfig object as well as the
119 * various helpers required to generate a full config.
120 */
121 public function __construct()
122 {
123 $this->attributeFilters = new AttributeFilterCollection;
124 $this->bundleGenerator = new BundleGenerator($this);
125 $this->plugins = new PluginCollection($this);
126 $this->registeredVars = ['urlConfig' => new UrlConfig];
127 $this->rendering = new Rendering($this);
128 $this->rootRules = new Ruleset;
129 $this->rulesGenerator = new RulesGenerator;
130 $this->tags = new TagCollection;
131 $this->templateChecker = new TemplateChecker;
132 $this->templateNormalizer = new TemplateNormalizer;
133 }
134
135 /**
136 * Magic __get automatically loads plugins, returns registered vars
137 *
138 * @param string $k Property name
139 * @return mixed
140 */
141 public function __get($k)
142 {
143 if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
144 {
145 return (isset($this->plugins[$k]))
146 ? $this->plugins[$k]
147 : $this->plugins->load($k);
148 }
149
150 if (isset($this->registeredVars[$k]))
151 {
152 return $this->registeredVars[$k];
153 }
154
155 throw new RuntimeException("Undefined property '" . __CLASS__ . '::$' . $k . "'");
156 }
157
158 /**
159 * Magic __isset checks existence in the plugins collection and registered vars
160 *
161 * @param string $k Property name
162 * @return bool
163 */
164 public function __isset($k)
165 {
166 if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
167 {
168 return isset($this->plugins[$k]);
169 }
170
171 return isset($this->registeredVars[$k]);
172 }
173
174 /**
175 * Magic __set adds to the plugins collection, registers vars
176 *
177 * @param string $k Property name
178 * @param mixed $v Property value
179 * @return mixed
180 */
181 public function __set($k, $v)
182 {
183 if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
184 {
185 $this->plugins[$k] = $v;
186 }
187 else
188 {
189 $this->registeredVars[$k] = $v;
190 }
191 }
192
193 /**
194 * Magic __set removes plugins from the plugins collection, unregisters vars
195 *
196 * @param string $k Property name
197 * @return mixed
198 */
199 public function __unset($k)
200 {
201 if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
202 {
203 unset($this->plugins[$k]);
204 }
205 else
206 {
207 unset($this->registeredVars[$k]);
208 }
209 }
210
211 /**
212 * Enable the creation of a JavaScript parser
213 *
214 * @return void
215 */
216 public function enableJavaScript()
217 {
218 if (!isset($this->javascript))
219 {
220 $this->javascript = new JavaScript($this);
221 }
222 }
223
224 /**
225 * Finalize this configuration and return all the relevant objects
226 *
227 * @return array One "parser" element and one "renderer" element unless specified otherwise
228 */
229 public function finalize()
230 {
231 $return = [];
232
233 // Finalize the plugins' config
234 $this->plugins->finalize();
235
236 // Normalize the tags' templates
237 foreach ($this->tags as $tag)
238 {
239 $this->templateNormalizer->normalizeTag($tag);
240 }
241
242 // Create a renderer
243 $return['renderer'] = $this->rendering->getRenderer();
244
245 // Add the generated tag rules
246 $this->addTagRules();
247
248 // Prepare the parser config
249 $config = $this->asConfig();
250 if (isset($this->javascript))
251 {
252 $return['js'] = $this->javascript->getParser(ConfigHelper::filterConfig($config, 'JS'));
253 }
254
255 // Remove JS-specific data from the config
256 $config = ConfigHelper::filterConfig($config, 'PHP');
257 ConfigHelper::optimizeArray($config);
258
259 // Create a parser
260 $return['parser'] = new Parser($config);
261
262 return $return;
263 }
264
265 /**
266 * Load a bundle into this configuration
267 *
268 * @param string $bundleName Name of the bundle
269 * @return void
270 */
271 public function loadBundle($bundleName)
272 {
273 if (!preg_match('#^[A-Z][A-Za-z0-9]+$#D', $bundleName))
274 {
275 throw new InvalidArgumentException("Invalid bundle name '" . $bundleName . "'");
276 }
277
278 $className = __CLASS__ . '\\Bundles\\' . $bundleName;
279
280 $bundle = new $className;
281 $bundle->configure($this);
282 }
283
284 /**
285 * Create and save a bundle based on this configuration
286 *
287 * @param string $className Name of the bundle class
288 * @param string $filepath Path where to save the bundle file
289 * @param array $options Options passed to the bundle generator
290 * @return bool Whether the write succeeded
291 */
292 public function saveBundle($className, $filepath, array $options = [])
293 {
294 $file = "<?php\n\n" . $this->bundleGenerator->generate($className, $options);
295
296 return (file_put_contents($filepath, $file) !== false);
297 }
298
299 /**
300 * Generate and return the complete config array
301 *
302 * @return array
303 */
304 public function asConfig()
305 {
306 // Remove properties that shouldn't be turned into config arrays
307 $properties = get_object_vars($this);
308 unset($properties['attributeFilters']);
309 unset($properties['bundleGenerator']);
310 unset($properties['javascript']);
311 unset($properties['rendering']);
312 unset($properties['rulesGenerator']);
313 unset($properties['registeredVars']);
314 unset($properties['templateChecker']);
315 unset($properties['templateNormalizer']);
316 unset($properties['stylesheet']);
317
318 // Create the config array
319 $config = ConfigHelper::toArray($properties);
320 $bitfields = RulesHelper::getBitfields($this->tags, $this->rootRules);
321
322 // Save the root context
323 $config['rootContext'] = $bitfields['root'];
324 $config['rootContext']['flags'] = $config['rootRules']['flags'];
325
326 // Save the registered vars (including the empty ones)
327 $config['registeredVars'] = ConfigHelper::toArray($this->registeredVars, true);
328
329 // Make sure those keys exist even if they're empty
330 $config += [
331 'plugins' => [],
332 'tags' => []
333 ];
334
335 // Remove unused tags
336 $config['tags'] = array_intersect_key($config['tags'], $bitfields['tags']);
337
338 // Add the bitfield information to each tag
339 foreach ($bitfields['tags'] as $tagName => $tagBitfields)
340 {
341 $config['tags'][$tagName] += $tagBitfields;
342 }
343
344 // Remove unused entries
345 unset($config['rootRules']);
346
347 return $config;
348 }
349
350 /**
351 * Add the rules generated by $this->rulesGenerator
352 *
353 * @return void
354 */
355 protected function addTagRules()
356 {
357 // Get the rules
358 $rules = $this->rulesGenerator->getRules($this->tags);
359
360 // Add the rules pertaining to the root
361 $this->rootRules->merge($rules['root'], false);
362
363 // Add the rules pertaining to each tag
364 foreach ($rules['tags'] as $tagName => $tagRules)
365 {
366 $this->tags[$tagName]->rules->merge($tagRules, false);
367 }
368 }
369 }