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 |
FilterProcessing.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\Parser;
009
010 use s9e\TextFormatter\Parser;
011
012 class FilterProcessing
013 {
014 /**
015 * Execute all the attribute preprocessors of given tag
016 *
017 * @private
018 *
019 * @param Tag $tag Source tag
020 * @param array $tagConfig Tag's config
021 * @return void
022 */
023 public static function executeAttributePreprocessors(Tag $tag, array $tagConfig)
024 {
025 if (empty($tagConfig['attributePreprocessors']))
026 {
027 return;
028 }
029
030 foreach ($tagConfig['attributePreprocessors'] as list($attrName, $regexp, $map))
031 {
032 if ($tag->hasAttribute($attrName))
033 {
034 self::executeAttributePreprocessor($tag, $attrName, $regexp, $map);
035 }
036 }
037 }
038
039 /**
040 * Filter the attributes of given tag
041 *
042 * @private
043 *
044 * @param Tag $tag Tag being checked
045 * @param array $tagConfig Tag's config
046 * @param array $registeredVars Array of registered vars for use in attribute filters
047 * @param Logger $logger This parser's Logger instance
048 * @return void
049 */
050 public static function filterAttributes(Tag $tag, array $tagConfig, array $registeredVars, Logger $logger)
051 {
052 $attributes = [];
053 foreach ($tagConfig['attributes'] as $attrName => $attrConfig)
054 {
055 $attrValue = false;
056 if ($tag->hasAttribute($attrName))
057 {
058 $vars = [
059 'attrName' => $attrName,
060 'attrValue' => $tag->getAttribute($attrName),
061 'logger' => $logger,
062 'registeredVars' => $registeredVars
063 ];
064 $attrValue = self::executeAttributeFilterChain($attrConfig['filterChain'], $vars);
065 }
066
067 if ($attrValue !== false)
068 {
069 $attributes[$attrName] = $attrValue;
070 }
071 elseif (isset($attrConfig['defaultValue']))
072 {
073 $attributes[$attrName] = $attrConfig['defaultValue'];
074 }
075 elseif (!empty($attrConfig['required']))
076 {
077 $tag->invalidate();
078 }
079 }
080 $tag->setAttributes($attributes);
081 }
082
083 /**
084 * Execute a tag's filterChain
085 *
086 * @private
087 *
088 * @param Tag $tag Tag to filter
089 * @param Parser $parser Parser
090 * @param array $tagsConfig Tags' config
091 * @param Tag[] $openTags List of open tags
092 * @return void
093 */
094 public static function filterTag(Tag $tag, Parser $parser, array $tagsConfig, array $openTags)
095 {
096 $tagName = $tag->getName();
097 $tagConfig = $tagsConfig[$tagName];
098
099 // Record the tag being processed into the logger it can be added to the context of
100 // messages logged during the execution
101 $logger = $parser->getLogger();
102 $logger->setTag($tag);
103
104 // Prepare the variables that are accessible to filters
105 $text = $parser->getText();
106 $vars = [
107 'innerText' => '',
108 'logger' => $logger,
109 'openTags' => $openTags,
110 'outerText' => substr($text, $tag->getPos(), $tag->getLen()),
111 'parser' => $parser,
112 'registeredVars' => $parser->registeredVars,
113 'tag' => $tag,
114 'tagConfig' => $tagConfig,
115 'tagText' => substr($text, $tag->getPos(), $tag->getLen()),
116 'text' => $text
117 ];
118 $endTag = $tag->getEndTag();
119 if ($endTag)
120 {
121 $vars['innerText'] = substr($text, $tag->getPos() + $tag->getLen(), $endTag->getPos() - $tag->getPos() - $tag->getLen());
122 $vars['outerText'] = substr($text, $tag->getPos(), $endTag->getPos() + $endTag->getLen() - $tag->getPos());
123 }
124 foreach ($tagConfig['filterChain'] as $filter)
125 {
126 if ($tag->isInvalid())
127 {
128 break;
129 }
130 self::executeFilter($filter, $vars);
131 }
132
133 // Remove the tag from the logger
134 $logger->unsetTag();
135 }
136
137 /**
138 * Execute an attribute's filterChain
139 *
140 * @param array $filterChain Attribute's filterChain
141 * @param array $vars Callback vars
142 * @return mixed Filtered value
143 */
144 protected static function executeAttributeFilterChain(array $filterChain, array $vars)
145 {
146 $vars['logger']->setAttribute($vars['attrName']);
147 foreach ($filterChain as $filter)
148 {
149 $vars['attrValue'] = self::executeFilter($filter, $vars);
150 if ($vars['attrValue'] === false)
151 {
152 break;
153 }
154 }
155 $vars['logger']->unsetAttribute();
156
157 return $vars['attrValue'];
158 }
159
160 /**
161 * Execute an attribute preprocessor
162 *
163 * @param Tag $tag
164 * @param string $attrName
165 * @param string $regexp
166 * @param string[] $map
167 * @return void
168 */
169 protected static function executeAttributePreprocessor(Tag $tag, $attrName, $regexp, $map)
170 {
171 $attrValue = $tag->getAttribute($attrName);
172 $captures = self::getNamedCaptures($attrValue, $regexp, $map);
173 foreach ($captures as $k => $v)
174 {
175 // Attribute preprocessors cannot overwrite other attributes but they can
176 // overwrite themselves
177 if ($k === $attrName || !$tag->hasAttribute($k))
178 {
179 $tag->setAttribute($k, $v);
180 }
181 }
182 }
183
184 /**
185 * Execute a filter
186 *
187 * @see s9e\TextFormatter\Configurator\Items\ProgrammableCallback
188 *
189 * @param array $filter Programmed callback
190 * @param array $vars Variables to be used when executing the callback
191 * @return mixed Whatever the callback returns
192 */
193 protected static function executeFilter(array $filter, array $vars)
194 {
195 // Add vars from the registeredVars array to the list of vars
196 $vars += ['registeredVars' => []];
197 $vars += $vars['registeredVars'];
198
199 // Prepare the list of arguments
200 $args = [];
201 if (isset($filter['params']))
202 {
203 foreach ($filter['params'] as $k => $v)
204 {
205 $args[] = $vars[$k] ?? $v;
206 }
207 }
208
209 return call_user_func_array($filter['callback'], $args);
210 }
211
212 /**
213 * Execute a regexp and return the values of the mapped captures
214 *
215 * @param string $str
216 * @param string $regexp
217 * @param string[] $map
218 * @return array
219 */
220 protected static function getNamedCaptures($str, $regexp, $map)
221 {
222 if (!preg_match($regexp, $str, $m))
223 {
224 return [];
225 }
226
227 $values = [];
228 foreach ($map as $i => $k)
229 {
230 if (isset($m[$i]) && $m[$i] !== '')
231 {
232 $values[$k] = $m[$i];
233 }
234 }
235
236 return $values;
237 }
238 }