[ Index ]

PHP Cross Reference of phpBB-3.3.2-deutsch

title

Body

[close]

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

   1  <?php
   2  
   3  /**
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2020 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 ReflectionClass;
  11  use RuntimeException;
  12  use s9e\TextFormatter\Configurator;
  13  use s9e\TextFormatter\Configurator\Collections\TemplateParameterCollection;
  14  use s9e\TextFormatter\Configurator\Traits\Configurable;
  15  
  16  /**
  17  * @property RendererGenerator $engine
  18  * @property TemplateParameterCollection $parameters Parameters used by the renderer
  19  */
  20  class Rendering
  21  {
  22      use Configurable;
  23  
  24      /**
  25      * @var Configurator
  26      */
  27      protected $configurator;
  28  
  29      /**
  30      * @var RendererGenerator
  31      */
  32      protected $engine;
  33  
  34      /**
  35      * @var TemplateParameterCollection Parameters used by the renderer
  36      */
  37      protected $parameters;
  38  
  39      /**
  40      * Constructor
  41      *
  42      * @param  Configurator $configurator
  43      */
  44  	public function __construct(Configurator $configurator)
  45      {
  46          $this->configurator = $configurator;
  47          $this->parameters   = new TemplateParameterCollection;
  48      }
  49  
  50      /**
  51      * Get all the parameters defined and/or used in all the templates
  52      *
  53      * @return array Associative array of parameters names and their default value
  54      */
  55  	public function getAllParameters()
  56      {
  57          // Collect parameters used in template
  58          $params = [];
  59          foreach ($this->configurator->tags as $tag)
  60          {
  61              if (isset($tag->template))
  62              {
  63                  foreach ($tag->template->getParameters() as $paramName)
  64                  {
  65                      $params[$paramName] = '';
  66                  }
  67              }
  68          }
  69  
  70          // Merge defined parameters and those collected from templates. Defined parameters take
  71          // precedence
  72          $params = iterator_to_array($this->parameters) + $params;
  73  
  74          // Sort parameters by name for consistency
  75          ksort($params);
  76  
  77          return $params;
  78      }
  79  
  80      /**
  81      * Return the renderer generator used by this instance
  82      *
  83      * @return RendererGenerator
  84      */
  85  	public function getEngine()
  86      {
  87          if (!isset($this->engine))
  88          {
  89              $this->setEngine('XSLT');
  90          }
  91  
  92          return $this->engine;
  93      }
  94  
  95      /**
  96      * Return an instance of Renderer based on the current config
  97      *
  98      * @return Renderer
  99      */
 100  	public function getRenderer()
 101      {
 102          return $this->getEngine()->getRenderer($this);
 103      }
 104  
 105      /**
 106      * Get the templates defined in all the targs
 107      *
 108      * @return array Associative array of template names and content
 109      */
 110  	public function getTemplates()
 111      {
 112          $templates = [
 113              'br' => '<br/>',
 114              'e'  => '',
 115              'i'  => '',
 116              'p'  => '<p><xsl:apply-templates/></p>',
 117              's'  => ''
 118          ];
 119  
 120          foreach ($this->configurator->tags as $tagName => $tag)
 121          {
 122              if (isset($tag->template))
 123              {
 124                  $templates[$tagName] = (string) $tag->template;
 125              }
 126          }
 127  
 128          ksort($templates);
 129  
 130          return $templates;
 131      }
 132  
 133      /**
 134      * Set the RendererGenerator instance used
 135      *
 136      * NOTE: extra parameters are passed to the RendererGenerator's constructor
 137      *
 138      * @param  string|RendererGenerator $engine Engine name or instance of RendererGenerator
 139      * @return RendererGenerator                Instance of RendererGenerator
 140      */
 141  	public function setEngine($engine)
 142      {
 143          if (!($engine instanceof RendererGenerator))
 144          {
 145              $className  = 's9e\\TextFormatter\\Configurator\\RendererGenerators\\' . $engine;
 146              $reflection = new ReflectionClass($className);
 147  
 148              $engine = $reflection->newInstanceArgs(array_slice(func_get_args(), 1));
 149          }
 150  
 151          $this->engine = $engine;
 152  
 153          return $engine;
 154      }
 155  }


Generated: Wed Nov 11 20:28:18 2020 Cross-referenced by PHPXref 0.7.1