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

BundleGenerator.php

Zuletzt modifiziert: 02.04.2025, 15:03 - Dateigröße: 6.75 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;
009   
010  use s9e\TextFormatter\Configurator;
011  use s9e\TextFormatter\Configurator\RendererGenerators\PHP;
012   
013  class BundleGenerator
014  {
015      /**
016      * @var Configurator Configurator this instance belongs to
017      */
018      protected $configurator;
019   
020      /**
021      * @var callable Callback used to serialize the objects
022      */
023      public $serializer = 'serialize';
024   
025      /**
026      * @var string Callback used to unserialize the serialized objects (must be a string)
027      */
028      public $unserializer = 'unserialize';
029   
030      /**
031      * Constructor
032      *
033      * @param  Configurator $configurator Configurator
034      */
035      public function __construct(Configurator $configurator)
036      {
037          $this->configurator = $configurator;
038      }
039   
040      /**
041      * Create and return the source of a bundle based on given Configurator instance
042      *
043      * Options:
044      *
045      *  - autoInclude: automatically load the source of the PHP renderer (default: true)
046      *
047      * @param  string $className Name of the bundle class
048      * @param  array  $options   Associative array of optional settings
049      * @return string            PHP source for the bundle
050      */
051      public function generate($className, array $options = [])
052      {
053          // Add default options
054          $options += ['autoInclude' => true];
055   
056          // Copy the PHP files header if applicable
057          if ($this->configurator->rendering->engine instanceof PHP)
058          {
059              $this->configurator->rendering->engine->phpHeader = $this->configurator->phpHeader;
060          }
061   
062          // Get the parser and renderer
063          $objects  = $this->configurator->finalize();
064          $parser   = $objects['parser'];
065          $renderer = $objects['renderer'];
066   
067          // Split the bundle's class name and its namespace
068          $namespace = '';
069          if (preg_match('#(.*)\\\\([^\\\\]+)$#', $className, $m))
070          {
071              $namespace = $m[1];
072              $className = $m[2];
073          }
074   
075          // Start with the standard header
076          $php   = [];
077          $php[] = $this->configurator->phpHeader;
078   
079          if ($namespace)
080          {
081              $php[] = 'namespace ' . $namespace . ';';
082              $php[] = '';
083          }
084   
085          // Generate and append the bundle class
086          $php[] = 'abstract class ' . $className . ' extends \\s9e\\TextFormatter\\Bundle';
087          $php[] = '{';
088          $php[] = '    /**';
089          $php[] = '    * @var s9e\\TextFormatter\\Parser Singleton instance used by parse()';
090          $php[] = '    */';
091          $php[] = '    protected static $parser;';
092          $php[] = '';
093          $php[] = '    /**';
094          $php[] = '    * @var s9e\\TextFormatter\\Renderer Singleton instance used by render()';
095          $php[] = '    */';
096          $php[] = '    protected static $renderer;';
097          $php[] = '';
098   
099          // Add the event callbacks if applicable
100          $events = [
101              'beforeParse'
102                  => 'Callback executed before parse(), receives the original text as argument',
103              'afterParse'
104                  => 'Callback executed after parse(), receives the parsed text as argument',
105              'beforeRender'
106                  => 'Callback executed before render(), receives the parsed text as argument',
107              'afterRender'
108                  => 'Callback executed after render(), receives the output as argument',
109              'beforeUnparse'
110                  => 'Callback executed before unparse(), receives the parsed text as argument',
111              'afterUnparse'
112                  => 'Callback executed after unparse(), receives the original text as argument'
113          ];
114          foreach ($events as $eventName => $eventDesc)
115          {
116              if (isset($options[$eventName]))
117              {
118                  $php[] = '    /**';
119                  $php[] = '    * @var ' . $eventDesc;
120                  $php[] = '    */';
121                  $php[] = '    public static $' . $eventName . ' = ' . var_export($options[$eventName], true) . ';';
122                  $php[] = '';
123              }
124          }
125   
126          if (isset($objects['js']))
127          {
128              $php[] = '    /**';
129              $php[] = '    * {@inheritdoc}';
130              $php[] = '    */';
131              $php[] = '    public static function getJS()';
132              $php[] = '    {';
133              $php[] = '        return ' . var_export($objects['js'], true) . ';';
134              $php[] = '    }';
135              $php[] = '';
136          }
137   
138          $php[] = '    /**';
139          $php[] = '    * {@inheritdoc}';
140          $php[] = '    */';
141          $php[] = '    public static function getParser()';
142          $php[] = '    {';
143   
144          if (isset($options['parserSetup']))
145          {
146              $php[] = '        $parser = ' . $this->exportObject($parser) . ';';
147              $php[] = '        ' . $this->exportCallback($namespace, $options['parserSetup'], '$parser') . ';';
148              $php[] = '';
149              $php[] = '        return $parser;';
150          }
151          else
152          {
153              $php[] = '        return ' . $this->exportObject($parser) . ';';
154          }
155   
156          $php[] = '    }';
157          $php[] = '';
158          $php[] = '    /**';
159          $php[] = '    * {@inheritdoc}';
160          $php[] = '    */';
161          $php[] = '    public static function getRenderer()';
162          $php[] = '    {';
163   
164          // If this is a PHP renderer and we know where it's saved, automatically load it as needed
165          if (!empty($options['autoInclude'])
166           && $this->configurator->rendering->engine instanceof PHP
167           && isset($this->configurator->rendering->engine->lastFilepath))
168          {
169              $className = get_class($renderer);
170              $filepath  = realpath($this->configurator->rendering->engine->lastFilepath);
171   
172              $php[] = '        if (!class_exists(' . var_export($className, true) . ', false)';
173              $php[] = '         && file_exists(' . var_export($filepath, true) . '))';
174              $php[] = '        {';
175              $php[] = '            include ' . var_export($filepath, true) . ';';
176              $php[] = '        }';
177              $php[] = '';
178          }
179   
180          if (isset($options['rendererSetup']))
181          {
182              $php[] = '        $renderer = ' . $this->exportObject($renderer) . ';';
183              $php[] = '        ' . $this->exportCallback($namespace, $options['rendererSetup'], '$renderer') . ';';
184              $php[] = '';
185              $php[] = '        return $renderer;';
186          }
187          else
188          {
189              $php[] = '        return ' . $this->exportObject($renderer) . ';';
190          }
191   
192          $php[] = '    }';
193          $php[] = '}';
194   
195          return implode("\n", $php);
196      }
197   
198      /**
199      * Export a given callback as PHP code
200      *
201      * @param  string   $namespace Namespace in which the callback is execute
202      * @param  callable $callback  Original callback
203      * @param  string   $argument  Callback's argument (as PHP code)
204      * @return string              PHP code
205      */
206      protected function exportCallback($namespace, callable $callback, $argument)
207      {
208          if (is_array($callback) && is_string($callback[0]))
209          {
210              // Replace ['foo', 'bar'] with 'foo::bar'
211              $callback = $callback[0] . '::' . $callback[1];
212          }
213   
214          if (!is_string($callback))
215          {
216              return 'call_user_func(' . var_export($callback, true) . ', ' . $argument . ')';
217          }
218   
219          // Ensure that the callback starts with a \
220          if ($callback[0] !== '\\')
221          {
222              $callback = '\\' . $callback;
223          }
224   
225          // Replace \foo\bar::baz() with bar::baz() if we're in namespace foo
226          if (substr($callback, 0, 2 + strlen($namespace)) === '\\' . $namespace . '\\')
227          {
228              $callback = substr($callback, 2 + strlen($namespace));
229          }
230   
231          return $callback . '(' . $argument . ')';
232      }
233   
234      /**
235      * Serialize and export a given object as PHP code
236      *
237      * @param  object $obj Original object
238      * @return string      PHP code
239      */
240      protected function exportObject($obj)
241      {
242          // Serialize the object
243          $str = call_user_func($this->serializer, $obj);
244   
245          // Export the object's source
246          $str = var_export($str, true);
247   
248          return $this->unserializer . '(' . $str . ')';
249      }
250  }