[ Index ]

PHP Cross Reference of phpBB-3.3.7-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2021 The s9e authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Configurator\RendererGenerators;
   9  
  10  use DOMElement;
  11  use s9e\TextFormatter\Configurator\Helpers\TemplateHelper;
  12  use s9e\TextFormatter\Configurator\Helpers\TemplateParser;
  13  use s9e\TextFormatter\Configurator\RendererGenerator;
  14  use s9e\TextFormatter\Configurator\RendererGenerators\PHP\ControlStructuresOptimizer;
  15  use s9e\TextFormatter\Configurator\RendererGenerators\PHP\Optimizer;
  16  use s9e\TextFormatter\Configurator\RendererGenerators\PHP\Quick;
  17  use s9e\TextFormatter\Configurator\RendererGenerators\PHP\Serializer;
  18  use s9e\TextFormatter\Configurator\RendererGenerators\PHP\SwitchStatement;
  19  use s9e\TextFormatter\Configurator\Rendering;
  20  use s9e\TextFormatter\Configurator\TemplateNormalizer;
  21  
  22  class PHP implements RendererGenerator
  23  {
  24      /**
  25      * XSL namespace
  26      */
  27      const XMLNS_XSL = 'http://www.w3.org/1999/XSL/Transform';
  28  
  29      /**
  30      * @var string Directory where the renderer's source is automatically saved if set, and if filepath is not set
  31      */
  32      public $cacheDir;
  33  
  34      /**
  35      * @var string Name of the class to be created. If null, a random name will be generated
  36      */
  37      public $className;
  38  
  39      /**
  40      * @var ControlStructuresOptimizer Control structures optimizer
  41      */
  42      public $controlStructuresOptimizer;
  43  
  44      /**
  45      * @var string Prefix used when generating a default class name
  46      */
  47      public $defaultClassPrefix = 'Renderer_';
  48  
  49      /**
  50      * @var bool Whether to enable the Quick renderer
  51      */
  52      public $enableQuickRenderer = true;
  53  
  54      /**
  55      * @var string If set, path to the file where the renderer will be saved
  56      */
  57      public $filepath;
  58  
  59      /**
  60      * @var string Name of the last class generated
  61      */
  62      public $lastClassName;
  63  
  64      /**
  65      * @var string Path to the last file saved
  66      */
  67      public $lastFilepath;
  68  
  69      /**
  70      * @var TemplateNormalizer
  71      */
  72      protected $normalizer;
  73  
  74      /**
  75      * @var Optimizer Optimizer
  76      */
  77      public $optimizer;
  78  
  79      /**
  80      * @var string File header
  81      */
  82      public $phpHeader = '/**
  83  * @package   s9e\TextFormatter
  84  * @copyright Copyright (c) 2010-2021 The s9e authors
  85  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
  86  */';
  87  
  88      /**
  89      * @var Serializer Serializer
  90      */
  91      public $serializer;
  92  
  93      /**
  94      * Constructor
  95      *
  96      * @param string $cacheDir If set, path to the directory where the renderer will be saved
  97      */
  98  	public function __construct($cacheDir = null)
  99      {
 100          $this->cacheDir = $cacheDir ?? sys_get_temp_dir();
 101          if (extension_loaded('tokenizer'))
 102          {
 103              $this->controlStructuresOptimizer = new ControlStructuresOptimizer;
 104              $this->optimizer = new Optimizer;
 105          }
 106          $this->serializer = new Serializer;
 107          $this->normalizer = new TemplateNormalizer(['RemoveLivePreviewAttributes']);
 108      }
 109  
 110      /**
 111      * {@inheritdoc}
 112      */
 113  	public function getRenderer(Rendering $rendering)
 114      {
 115          // Generate the source file
 116          $php = $this->generate($rendering);
 117  
 118          // Save the file if applicable
 119          if (isset($this->filepath))
 120          {
 121              $filepath = $this->filepath;
 122          }
 123          else
 124          {
 125              $filepath = $this->cacheDir . '/' . str_replace('\\', '_', $this->lastClassName) . '.php';
 126          }
 127  
 128          file_put_contents($filepath, "<?php\n" . $php);
 129          $this->lastFilepath = realpath($filepath);
 130  
 131          // Execute the source to create the class if it doesn't exist
 132          if (!class_exists($this->lastClassName, false))
 133          {
 134              include $filepath;
 135          }
 136  
 137          return new $this->lastClassName;
 138      }
 139  
 140      /**
 141      * Generate the source for a PHP class that renders an intermediate representation according to
 142      * given rendering configuration
 143      *
 144      * @param  Rendering $rendering
 145      * @return string
 146      */
 147  	public function generate(Rendering $rendering)
 148      {
 149          // Compile the templates to PHP
 150          $compiledTemplates = array_map([$this, 'compileTemplate'], $rendering->getTemplates());
 151  
 152          // Start the code right after the class name, we'll prepend the header when we're done
 153          $php = [];
 154          $php[] = ' extends \\s9e\\TextFormatter\\Renderers\\PHP';
 155          $php[] = '{';
 156          $php[] = '    protected $params=' . self::export($rendering->getAllParameters()) . ';';
 157          $php[] = '    protected function renderNode(\\DOMNode $node)';
 158          $php[] = '    {';
 159          $php[] = '        ' . SwitchStatement::generate('$node->nodeName', $compiledTemplates, '$this->at($node);');
 160          $php[] = '    }';
 161  
 162          // Append the Quick renderer if applicable
 163          if ($this->enableQuickRenderer)
 164          {
 165              $php[] = Quick::getSource($compiledTemplates);
 166          }
 167  
 168          // Close the class definition
 169          $php[] = '}';
 170  
 171          // Assemble the source
 172          $php = implode("\n", $php);
 173  
 174          // Finally, optimize the control structures
 175          if (isset($this->controlStructuresOptimizer))
 176          {
 177              $php = $this->controlStructuresOptimizer->optimize($php);
 178          }
 179  
 180          // Generate a name for that class if necessary, and save it
 181          $className = (isset($this->className))
 182                     ? $this->className
 183                     : $this->defaultClassPrefix . sha1($php);
 184          $this->lastClassName = $className;
 185  
 186          // Prepare the header
 187          $header = "\n" . $this->phpHeader . "\n";
 188  
 189          // Declare the namespace and class name
 190          $pos = strrpos($className, '\\');
 191          if ($pos !== false)
 192          {
 193              $header .= 'namespace ' . substr($className, 0, $pos) . ";\n\n";
 194              $className = substr($className, 1 + $pos);
 195          }
 196  
 197          // Prepend the header and the class name
 198          $php = $header . 'class ' . $className . $php;
 199  
 200          return $php;
 201      }
 202  
 203      /**
 204      * Export given array as PHP code
 205      *
 206      * @param  array  $value Original value
 207      * @return string        PHP code
 208      */
 209  	protected static function export(array $value)
 210      {
 211          $pairs = [];
 212          foreach ($value as $k => $v)
 213          {
 214              $pairs[] = var_export($k, true) . '=>' . var_export($v, true);
 215          }
 216  
 217          return '[' . implode(',', $pairs) . ']';
 218      }
 219  
 220      /**
 221      * Compile a template to PHP
 222      *
 223      * @param  string $template Original template
 224      * @return string           Compiled template
 225      */
 226  	protected function compileTemplate($template)
 227      {
 228          $template = $this->normalizer->normalizeTemplate($template);
 229  
 230          // Parse the template
 231          $ir = TemplateParser::parse($template);
 232  
 233          // Serialize the representation to PHP
 234          $php = $this->serializer->serialize($ir->documentElement);
 235          if (isset($this->optimizer))
 236          {
 237              $php = $this->optimizer->optimize($php);
 238          }
 239  
 240          return $php;
 241      }
 242  }


Generated: Thu Mar 24 21:31:15 2022 Cross-referenced by PHPXref 0.7.1