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.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

CallbackGenerator.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 4.66 KiB


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\JavaScript;
009   
010  class CallbackGenerator
011  {
012      /**
013      * @var array Path to callbacks in keys, callback signature in values
014      */
015      public $callbacks = [
016          'tags.*.attributes.*.filterChain.*' => [
017              'attrValue' => '*',
018              'attrName'  => 'string'
019          ],
020          'tags.*.filterChain.*' => [
021              'tag'       => '!Tag',
022              'tagConfig' => '!Object'
023          ]
024      ];
025   
026      /**
027      * @var Encoder
028      */
029      protected $encoder;
030   
031      /**
032      * Constructor
033      */
034      public function __construct()
035      {
036          $this->encoder = new Encoder;
037      }
038   
039      /**
040      * Replace all callbacks in given config
041      *
042      * @param  array $config Original config
043      * @return array         Modified config
044      */
045      public function replaceCallbacks(array $config)
046      {
047          foreach ($this->callbacks as $path => $params)
048          {
049              $config = $this->mapArray($config, explode('.', $path), $params);
050          }
051   
052          return $config;
053      }
054   
055      /**
056      * Build the list of arguments used in a callback invocation
057      *
058      * @param  array  $params    Callback parameters
059      * @param  array  $localVars Known vars from the calling scope
060      * @return string            JavaScript code
061      */
062      protected function buildCallbackArguments(array $params, array $localVars)
063      {
064          // Remove 'parser' as a parameter, since there's no such thing in JavaScript
065          unset($params['parser']);
066   
067          // Rebuild the local vars map to include global vars and computed values
068          $available  = array_combine(array_keys($localVars), array_keys($localVars));
069          $available += [
070              'innerText'      => '(tag.getEndTag() ? text.substr(tag.getPos() + tag.getLen(), tag.getEndTag().getPos() - tag.getPos() - tag.getLen()) : "")',
071              'logger'         => 'logger',
072              'openTags'       => 'openTags',
073              'outerText'      => 'text.substr(tag.getPos(), (tag.getEndTag() ? tag.getEndTag().getPos() + tag.getEndTag().getLen() - tag.getPos() : tag.getLen()))',
074              'registeredVars' => 'registeredVars',
075              'tagText'        => 'text.substr(tag.getPos(), tag.getLen())',
076              'text'           => 'text'
077          ];
078   
079          $args = [];
080          foreach ($params as $k => $v)
081          {
082              if (isset($v))
083              {
084                  // Param by value
085                  $args[] = $this->encoder->encode($v);
086              }
087              elseif (isset($available[$k]))
088              {
089                  // Param by name that matches a local expression
090                  $args[] = $available[$k];
091              }
092              else
093              {
094                  $args[] = 'registeredVars[' . json_encode($k) . ']';
095              }
096          }
097   
098          return implode(',', $args);
099      }
100   
101      /**
102      * Generate a function from a callback config
103      *
104      * @param  array $config Callback config
105      * @param  array $params Param names as keys, param types as values
106      * @return Code
107      */
108      protected function generateFunction(array $config, array $params)
109      {
110          $js = (string) $config['js'];
111   
112          // returnFalse() and returnTrue() can be used as-is
113          if ($js === 'returnFalse' || $js === 'returnTrue')
114          {
115              return new Code($js);
116          }
117   
118          // Add an empty list of params if none is set
119          $config += ['params' => []];
120   
121          $src  = $this->getHeader($params);
122          $src .= 'function(' . implode(',', array_keys($params)) . '){';
123          $src .= 'return ' . $this->parenthesizeCallback($js);
124          $src .= '(' . $this->buildCallbackArguments($config['params'], $params) . ');}';
125   
126          return new Code($src);
127      }
128   
129      /**
130      * Generate a function header for given signature
131      *
132      * @param  array  $params Param names as keys, param types as values
133      * @return string
134      */
135      protected function getHeader(array $params)
136      {
137          // Prepare the function's header
138          $header = "/**\n";
139          foreach ($params as $paramName => $paramType)
140          {
141              $header .= '* @param {' . $paramType . '} ' . $paramName . "\n";
142          }
143          $header .= "* @return {*}\n";
144          $header .= "*/\n";
145   
146          return $header;
147      }
148   
149      /**
150      * Replace callbacks in given config array
151      *
152      * @param  array    $array  Original config
153      * @param  string[] $path   Path to callbacks
154      * @param  array    $params Default params
155      * @return array            Modified config
156      */
157      protected function mapArray(array $array, array $path, array $params)
158      {
159          $key  = array_shift($path);
160          $keys = ($key === '*') ? array_keys($array) : [$key];
161          foreach ($keys as $key)
162          {
163              if (!isset($array[$key]))
164              {
165                  continue;
166              }
167              $array[$key] = (empty($path)) ? $this->generateFunction($array[$key], $params) : $this->mapArray($array[$key], $path, $params);
168          }
169   
170          return $array;
171      }
172   
173      /**
174      * Add parentheses to a function literal, if necessary
175      *
176      * Will return single vars as-is, and will put anything else between parentheses
177      *
178      * @param  string $callback Original callback
179      * @return string           Modified callback
180      */
181      protected function parenthesizeCallback($callback)
182      {
183          return (preg_match('(^[.\\w]+$)D', $callback)) ? $callback : '(' . $callback  . ')';
184      }
185  }