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 |
RulesGenerator.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\Configurator;
009
010 use ArrayAccess;
011 use DOMDocument;
012 use Iterator;
013 use s9e\TextFormatter\Configurator\Collections\RulesGeneratorList;
014 use s9e\TextFormatter\Configurator\Collections\TagCollection;
015 use s9e\TextFormatter\Configurator\Helpers\TemplateInspector;
016 use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\BooleanRulesGenerator;
017 use s9e\TextFormatter\Configurator\RulesGenerators\Interfaces\TargetedRulesGenerator;
018 use s9e\TextFormatter\Configurator\Traits\CollectionProxy;
019
020 /**
021 * @method mixed add(mixed $value, null $void) Add (append) a value to this list
022 * @method mixed append(mixed $value) Append a value to this list
023 * @method array asConfig()
024 * @method void clear() Empty this collection
025 * @method bool contains(mixed $value) Test whether a given value is present in this collection
026 * @method integer count()
027 * @method mixed current()
028 * @method void delete(string $key) Delete a value from this list and remove gaps in keys
029 * @method bool exists(string $key) Test whether an item of given key exists
030 * @method mixed get(string $key) Return a value from this collection
031 * @method mixed indexOf(mixed $value) Find the index of a given value
032 * @method mixed insert(integer $offset, mixed $value) Insert a value at an arbitrary 0-based position
033 * @method integer|string key()
034 * @method mixed next()
035 * @method integer normalizeKey(mixed $key) Ensure that the key is a valid offset
036 * @method BooleanRulesGenerator|TargetedRulesGenerator normalizeValue(string|BooleanRulesGenerator|TargetedRulesGenerator $generator) Normalize the value to an object
037 * @method bool offsetExists(string|integer $offset)
038 * @method mixed offsetGet(string|integer $offset)
039 * @method void offsetSet(mixed $offset, mixed $value) Custom offsetSet() implementation to allow assignment with a null offset to append to the
040 * @method void offsetUnset(string|integer $offset)
041 * @method string onDuplicate(string|null $action) Query and set the action to take when add() is called with a key that already exists
042 * @method mixed prepend(mixed $value) Prepend a value to this list
043 * @method integer remove(mixed $value) Remove all items matching given value
044 * @method void rewind()
045 * @method mixed set(string $key, mixed $value) Set and overwrite a value in this collection
046 * @method bool valid()
047 */
048 class RulesGenerator implements ArrayAccess, Iterator
049 {
050 use CollectionProxy;
051
052 /**
053 * @var RulesGeneratorList Collection of objects
054 */
055 protected $collection;
056
057 /**
058 * Constructor
059 *
060 * Will load the default rule generators
061 */
062 public function __construct()
063 {
064 $this->collection = new RulesGeneratorList;
065 $this->collection->append('AutoCloseIfVoid');
066 $this->collection->append('AutoReopenFormattingElements');
067 $this->collection->append('BlockElementsCloseFormattingElements');
068 $this->collection->append('BlockElementsFosterFormattingElements');
069 $this->collection->append('DisableAutoLineBreaksIfNewLinesArePreserved');
070 $this->collection->append('EnforceContentModels');
071 $this->collection->append('EnforceOptionalEndTags');
072 $this->collection->append('IgnoreTagsInCode');
073 $this->collection->append('IgnoreTextIfDisallowed');
074 $this->collection->append('IgnoreWhitespaceAroundBlockElements');
075 $this->collection->append('TrimFirstLineInCodeBlocks');
076 }
077
078 /**
079 * Generate rules for given tag collection
080 *
081 * @param TagCollection $tags Tags collection
082 * @return array
083 */
084 public function getRules(TagCollection $tags)
085 {
086 $tagInspectors = $this->getTagInspectors($tags);
087
088 return [
089 'root' => $this->generateRootRules($tagInspectors),
090 'tags' => $this->generateTagRules($tagInspectors)
091 ];
092 }
093
094 /**
095 * Generate and return rules based on a set of TemplateInspector
096 *
097 * @param array $tagInspectors Array of [tagName => TemplateInspector]
098 * @return array Array of [tagName => [<rules>]]
099 */
100 protected function generateTagRules(array $tagInspectors)
101 {
102 $rules = [];
103 foreach ($tagInspectors as $tagName => $tagInspector)
104 {
105 $rules[$tagName] = $this->generateRuleset($tagInspector, $tagInspectors);
106 }
107
108 return $rules;
109 }
110
111 /**
112 * Generate a set of rules to be applied at the root of a document
113 *
114 * @param array $tagInspectors Array of [tagName => TemplateInspector]
115 * @return array
116 */
117 protected function generateRootRules(array $tagInspectors)
118 {
119 // Create a proxy for the parent markup so that we can determine which tags are allowed at
120 // the root of the text (IOW, with no parent) or even disabled altogether
121 $rootInspector = new TemplateInspector('<div><xsl:apply-templates/></div>');
122 $rules = $this->generateRuleset($rootInspector, $tagInspectors);
123
124 // Remove root rules that wouldn't be applied anyway
125 unset($rules['autoClose']);
126 unset($rules['autoReopen']);
127 unset($rules['breakParagraph']);
128 unset($rules['closeAncestor']);
129 unset($rules['closeParent']);
130 unset($rules['fosterParent']);
131 unset($rules['ignoreSurroundingWhitespace']);
132 unset($rules['isTransparent']);
133 unset($rules['requireAncestor']);
134 unset($rules['requireParent']);
135
136 return $rules;
137 }
138
139 /**
140 * Generate a set of rules for a single TemplateInspector instance
141 *
142 * @param TemplateInspector $srcInspector Source of the rules
143 * @param array $trgInspectors Array of [tagName => TemplateInspector]
144 * @return array
145 */
146 protected function generateRuleset(TemplateInspector $srcInspector, array $trgInspectors)
147 {
148 $rules = [];
149 foreach ($this->collection as $rulesGenerator)
150 {
151 if ($rulesGenerator instanceof BooleanRulesGenerator)
152 {
153 foreach ($rulesGenerator->generateBooleanRules($srcInspector) as $ruleName => $bool)
154 {
155 $rules[$ruleName] = $bool;
156 }
157 }
158
159 if ($rulesGenerator instanceof TargetedRulesGenerator)
160 {
161 foreach ($trgInspectors as $tagName => $trgInspector)
162 {
163 $targetedRules = $rulesGenerator->generateTargetedRules($srcInspector, $trgInspector);
164 foreach ($targetedRules as $ruleName)
165 {
166 $rules[$ruleName][] = $tagName;
167 }
168 }
169 }
170 }
171
172 return $rules;
173 }
174
175 /**
176 * Inspect given list of tags
177 *
178 * @param TagCollection $tags Tags collection
179 * @return array Array of [tagName => TemplateInspector]
180 */
181 protected function getTagInspectors(TagCollection $tags)
182 {
183 $tagInspectors = [];
184 foreach ($tags as $tagName => $tag)
185 {
186 // Use the tag's template if applicable or XSLT's implicit default otherwise
187 $template = $tag->template ?? '<xsl:apply-templates/>';
188 $tagInspectors[$tagName] = new TemplateInspector($template);
189 }
190
191 return $tagInspectors;
192 }
193 }