[ Index ]

PHP Cross Reference of phpBB-3.3.12-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/ -> Configurator.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;
   9  
  10  use InvalidArgumentException;
  11  use RuntimeException;
  12  use s9e\TextFormatter\Configurator\BundleGenerator;
  13  use s9e\TextFormatter\Configurator\Collections\AttributeFilterCollection;
  14  use s9e\TextFormatter\Configurator\Collections\PluginCollection;
  15  use s9e\TextFormatter\Configurator\Collections\Ruleset;
  16  use s9e\TextFormatter\Configurator\Collections\TagCollection;
  17  use s9e\TextFormatter\Configurator\ConfigProvider;
  18  use s9e\TextFormatter\Configurator\Helpers\ConfigHelper;
  19  use s9e\TextFormatter\Configurator\Helpers\RulesHelper;
  20  use s9e\TextFormatter\Configurator\JavaScript;
  21  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  22  use s9e\TextFormatter\Configurator\Rendering;
  23  use s9e\TextFormatter\Configurator\RulesGenerator;
  24  use s9e\TextFormatter\Configurator\TemplateChecker;
  25  use s9e\TextFormatter\Configurator\TemplateNormalizer;
  26  use s9e\TextFormatter\Configurator\UrlConfig;
  27  
  28  /**
  29  * @property Plugins\Autoemail\Configurator $Autoemail Autoemail plugin's configurator
  30  * @property Plugins\Autoimage\Configurator $Autolink Autoimage plugin's configurator
  31  * @property Plugins\Autolink\Configurator $Autolink Autolink plugin's configurator
  32  * @property Plugins\Autovideo\Configurator $Autovideo Autovideo plugin's configurator
  33  * @property Plugins\BBCodes\Configurator $BBCodes BBCodes plugin's configurator
  34  * @property Plugins\Censor\Configurator $Censor Censor plugin's configurator
  35  * @property Plugins\Emoji\Configurator $Emoji Emoji plugin's configurator
  36  * @property Plugins\Emoticons\Configurator $Emoticons Emoticons plugin's configurator
  37  * @property Plugins\Escaper\Configurator $Escaper Escaper plugin's configurator
  38  * @property Plugins\FancyPants\Configurator $FancyPants FancyPants plugin's configurator
  39  * @property Plugins\HTMLComments\Configurator $HTMLComments HTMLComments plugin's configurator
  40  * @property Plugins\HTMLElements\Configurator $HTMLElements HTMLElements plugin's configurator
  41  * @property Plugins\HTMLEntities\Configurator $HTMLEntities HTMLEntities plugin's configurator
  42  * @property Plugins\Keywords\Configurator $Keywords Keywords plugin's configurator
  43  * @property Plugins\Litedown\Configurator $Litedown Litedown plugin's configurator
  44  * @property Plugins\MediaEmbed\Configurator $MediaEmbed MediaEmbed plugin's configurator
  45  * @property Plugins\PipeTables\Configurator $PipeTables PipeTables plugin's configurator
  46  * @property Plugins\Preg\Configurator $Preg Preg plugin's configurator
  47  * @property UrlConfig $urlConfig Default URL config
  48  */
  49  class Configurator implements ConfigProvider
  50  {
  51      /**
  52      * @var AttributeFilterCollection Dynamically-populated collection of AttributeFilter instances
  53      */
  54      public $attributeFilters;
  55  
  56      /**
  57      * @var BundleGenerator Default bundle generator
  58      */
  59      public $bundleGenerator;
  60  
  61      /**
  62      * @var JavaScript JavaScript manipulation object
  63      */
  64      public $javascript;
  65  
  66      /**
  67      * @var string PHP files header
  68      */
  69      public $phpHeader = '/**
  70  * @package   s9e\TextFormatter
  71  * @copyright Copyright (c) 2010-2022 The s9e authors
  72  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
  73  */';
  74  
  75      /**
  76      * @var PluginCollection Loaded plugins
  77      */
  78      public $plugins;
  79  
  80      /**
  81      * @var array Array of variables that are available to the filters during parsing
  82      */
  83      public $registeredVars;
  84  
  85      /**
  86      * @var Rendering Rendering configuration
  87      */
  88      public $rendering;
  89  
  90      /**
  91      * @var Ruleset Rules that apply at the root of the text
  92      */
  93      public $rootRules;
  94  
  95      /**
  96      * @var RulesGenerator Generator used by $this->getRenderer()
  97      */
  98      public $rulesGenerator;
  99  
 100      /**
 101      * @var TagCollection Tags repository
 102      */
 103      public $tags;
 104  
 105      /**
 106      * @var TemplateChecker Default template checker
 107      */
 108      public $templateChecker;
 109  
 110      /**
 111      * @var TemplateNormalizer Default template normalizer
 112      */
 113      public $templateNormalizer;
 114  
 115      /**
 116      * Constructor
 117      *
 118      * Prepares the collections that hold tags and filters, the UrlConfig object as well as the
 119      * various helpers required to generate a full config.
 120      */
 121  	public function __construct()
 122      {
 123          $this->attributeFilters   = new AttributeFilterCollection;
 124          $this->bundleGenerator    = new BundleGenerator($this);
 125          $this->plugins            = new PluginCollection($this);
 126          $this->registeredVars     = ['urlConfig' => new UrlConfig];
 127          $this->rendering          = new Rendering($this);
 128          $this->rootRules          = new Ruleset;
 129          $this->rulesGenerator     = new RulesGenerator;
 130          $this->tags               = new TagCollection;
 131          $this->templateChecker    = new TemplateChecker;
 132          $this->templateNormalizer = new TemplateNormalizer;
 133      }
 134  
 135      /**
 136      * Magic __get automatically loads plugins, returns registered vars
 137      *
 138      * @param  string $k Property name
 139      * @return mixed
 140      */
 141  	public function __get($k)
 142      {
 143          if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
 144          {
 145              return (isset($this->plugins[$k]))
 146                   ? $this->plugins[$k]
 147                   : $this->plugins->load($k);
 148          }
 149  
 150          if (isset($this->registeredVars[$k]))
 151          {
 152              return $this->registeredVars[$k];
 153          }
 154  
 155          throw new RuntimeException("Undefined property '" . __CLASS__ . '::$' . $k . "'");
 156      }
 157  
 158      /**
 159      * Magic __isset checks existence in the plugins collection and registered vars
 160      *
 161      * @param  string $k Property name
 162      * @return bool
 163      */
 164  	public function __isset($k)
 165      {
 166          if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
 167          {
 168              return isset($this->plugins[$k]);
 169          }
 170  
 171          return isset($this->registeredVars[$k]);
 172      }
 173  
 174      /**
 175      * Magic __set adds to the plugins collection, registers vars
 176      *
 177      * @param  string $k Property name
 178      * @param  mixed  $v Property value
 179      * @return mixed
 180      */
 181  	public function __set($k, $v)
 182      {
 183          if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
 184          {
 185              $this->plugins[$k] = $v;
 186          }
 187          else
 188          {
 189              $this->registeredVars[$k] = $v;
 190          }
 191      }
 192  
 193      /**
 194      * Magic __set removes plugins from the plugins collection, unregisters vars
 195      *
 196      * @param  string $k Property name
 197      * @return mixed
 198      */
 199  	public function __unset($k)
 200      {
 201          if (preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $k))
 202          {
 203              unset($this->plugins[$k]);
 204          }
 205          else
 206          {
 207              unset($this->registeredVars[$k]);
 208          }
 209      }
 210  
 211      /**
 212      * Enable the creation of a JavaScript parser
 213      *
 214      * @return void
 215      */
 216  	public function enableJavaScript()
 217      {
 218          if (!isset($this->javascript))
 219          {
 220              $this->javascript = new JavaScript($this);
 221          }
 222      }
 223  
 224      /**
 225      * Finalize this configuration and return all the relevant objects
 226      *
 227      * @return array One "parser" element and one "renderer" element unless specified otherwise
 228      */
 229  	public function finalize()
 230      {
 231          $return = [];
 232  
 233          // Finalize the plugins' config
 234          $this->plugins->finalize();
 235  
 236          // Normalize the tags' templates
 237          foreach ($this->tags as $tag)
 238          {
 239              $this->templateNormalizer->normalizeTag($tag);
 240          }
 241  
 242          // Create a renderer
 243          $return['renderer'] = $this->rendering->getRenderer();
 244  
 245          // Add the generated tag rules
 246          $this->addTagRules();
 247  
 248          // Prepare the parser config
 249          $config = $this->asConfig();
 250          if (isset($this->javascript))
 251          {
 252              $return['js'] = $this->javascript->getParser(ConfigHelper::filterConfig($config, 'JS'));
 253          }
 254  
 255          // Remove JS-specific data from the config
 256          $config = ConfigHelper::filterConfig($config, 'PHP');
 257          ConfigHelper::optimizeArray($config);
 258  
 259          // Create a parser
 260          $return['parser'] = new Parser($config);
 261  
 262          return $return;
 263      }
 264  
 265      /**
 266      * Load a bundle into this configuration
 267      *
 268      * @param  string $bundleName Name of the bundle
 269      * @return void
 270      */
 271  	public function loadBundle($bundleName)
 272      {
 273          if (!preg_match('#^[A-Z][A-Za-z0-9]+$#D', $bundleName))
 274          {
 275              throw new InvalidArgumentException("Invalid bundle name '" . $bundleName . "'");
 276          }
 277  
 278          $className = __CLASS__ . '\\Bundles\\' . $bundleName;
 279  
 280          $bundle = new $className;
 281          $bundle->configure($this);
 282      }
 283  
 284      /**
 285      * Create and save a bundle based on this configuration
 286      *
 287      * @param  string $className Name of the bundle class
 288      * @param  string $filepath  Path where to save the bundle file
 289      * @param  array  $options   Options passed to the bundle generator
 290      * @return bool              Whether the write succeeded
 291      */
 292  	public function saveBundle($className, $filepath, array $options = [])
 293      {
 294          $file = "<?php\n\n" . $this->bundleGenerator->generate($className, $options);
 295  
 296          return (file_put_contents($filepath, $file) !== false);
 297      }
 298  
 299      /**
 300      * Generate and return the complete config array
 301      *
 302      * @return array
 303      */
 304  	public function asConfig()
 305      {
 306          // Remove properties that shouldn't be turned into config arrays
 307          $properties = get_object_vars($this);
 308          unset($properties['attributeFilters']);
 309          unset($properties['bundleGenerator']);
 310          unset($properties['javascript']);
 311          unset($properties['rendering']);
 312          unset($properties['rulesGenerator']);
 313          unset($properties['registeredVars']);
 314          unset($properties['templateChecker']);
 315          unset($properties['templateNormalizer']);
 316          unset($properties['stylesheet']);
 317  
 318          // Create the config array
 319          $config    = ConfigHelper::toArray($properties);
 320          $bitfields = RulesHelper::getBitfields($this->tags, $this->rootRules);
 321  
 322          // Save the root context
 323          $config['rootContext'] = $bitfields['root'];
 324          $config['rootContext']['flags'] = $config['rootRules']['flags'];
 325  
 326          // Save the registered vars (including the empty ones)
 327          $config['registeredVars'] = ConfigHelper::toArray($this->registeredVars, true);
 328  
 329          // Make sure those keys exist even if they're empty
 330          $config += [
 331              'plugins' => [],
 332              'tags'    => []
 333          ];
 334  
 335          // Remove unused tags
 336          $config['tags'] = array_intersect_key($config['tags'], $bitfields['tags']);
 337  
 338          // Add the bitfield information to each tag
 339          foreach ($bitfields['tags'] as $tagName => $tagBitfields)
 340          {
 341              $config['tags'][$tagName] += $tagBitfields;
 342          }
 343  
 344          // Remove unused entries
 345          unset($config['rootRules']);
 346  
 347          return $config;
 348      }
 349  
 350      /**
 351      * Add the rules generated by $this->rulesGenerator
 352      *
 353      * @return void
 354      */
 355  	protected function addTagRules()
 356      {
 357          // Get the rules
 358          $rules = $this->rulesGenerator->getRules($this->tags);
 359  
 360          // Add the rules pertaining to the root
 361          $this->rootRules->merge($rules['root'], false);
 362  
 363          // Add the rules pertaining to each tag
 364          foreach ($rules['tags'] as $tagName => $tagRules)
 365          {
 366              $this->tags[$tagName]->rules->merge($tagRules, false);
 367          }
 368      }
 369  }


Generated: Sun Jun 23 12:25:44 2024 Cross-referenced by PHPXref 0.7.1