[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/ -> BundleGenerator.php (source)

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2022 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator;
   9  
  10  use s9e\TextFormatter\Configurator;
  11  use s9e\TextFormatter\Configurator\RendererGenerators\PHP;
  12  
  13  class BundleGenerator
  14  {
  15      /**
  16      * @var Configurator Configurator this instance belongs to
  17      */
  18      protected $configurator;
  19  
  20      /**
  21      * @var callable Callback used to serialize the objects
  22      */
  23      public $serializer = 'serialize';
  24  
  25      /**
  26      * @var string Callback used to unserialize the serialized objects (must be a string)
  27      */
  28      public $unserializer = 'unserialize';
  29  
  30      /**
  31      * Constructor
  32      *
  33      * @param  Configurator $configurator Configurator
  34      */
  35  	public function __construct(Configurator $configurator)
  36      {
  37          $this->configurator = $configurator;
  38      }
  39  
  40      /**
  41      * Create and return the source of a bundle based on given Configurator instance
  42      *
  43      * Options:
  44      *
  45      *  - autoInclude: automatically load the source of the PHP renderer (default: true)
  46      *
  47      * @param  string $className Name of the bundle class
  48      * @param  array  $options   Associative array of optional settings
  49      * @return string            PHP source for the bundle
  50      */
  51  	public function generate($className, array $options = [])
  52      {
  53          // Add default options
  54          $options += ['autoInclude' => true];
  55  
  56          // Copy the PHP files header if applicable
  57          if ($this->configurator->rendering->engine instanceof PHP)
  58          {
  59              $this->configurator->rendering->engine->phpHeader = $this->configurator->phpHeader;
  60          }
  61  
  62          // Get the parser and renderer
  63          $objects  = $this->configurator->finalize();
  64          $parser   = $objects['parser'];
  65          $renderer = $objects['renderer'];
  66  
  67          // Split the bundle's class name and its namespace
  68          $namespace = '';
  69          if (preg_match('#(.*)\\\\([^\\\\]+)$#', $className, $m))
  70          {
  71              $namespace = $m[1];
  72              $className = $m[2];
  73          }
  74  
  75          // Start with the standard header
  76          $php   = [];
  77          $php[] = $this->configurator->phpHeader;
  78  
  79          if ($namespace)
  80          {
  81              $php[] = 'namespace ' . $namespace . ';';
  82              $php[] = '';
  83          }
  84  
  85          // Generate and append the bundle class
  86          $php[] = 'abstract class ' . $className . ' extends \\s9e\\TextFormatter\\Bundle';
  87          $php[] = '{';
  88          $php[] = '    /**';
  89          $php[] = '    * @var s9e\\TextFormatter\\Parser Singleton instance used by parse()';
  90          $php[] = '    */';
  91          $php[] = '    protected static $parser;';
  92          $php[] = '';
  93          $php[] = '    /**';
  94          $php[] = '    * @var s9e\\TextFormatter\\Renderer Singleton instance used by render()';
  95          $php[] = '    */';
  96          $php[] = '    protected static $renderer;';
  97          $php[] = '';
  98  
  99          // 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  }


Generated: Mon Nov 25 19:05:08 2024 Cross-referenced by PHPXref 0.7.1