[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Configurator/Collections/ -> PluginCollection.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\Collections;
   9  
  10  use InvalidArgumentException;
  11  use RuntimeException;
  12  use s9e\TextFormatter\Configurator;
  13  use s9e\TextFormatter\Plugins\ConfiguratorBase;
  14  
  15  class PluginCollection extends NormalizedCollection
  16  {
  17      /**
  18      * @var Configurator
  19      */
  20      protected $configurator;
  21  
  22      /**
  23      * Constructor
  24      *
  25      * @param Configurator $configurator
  26      */
  27  	public function __construct(Configurator $configurator)
  28      {
  29          $this->configurator = $configurator;
  30      }
  31  
  32      /**
  33      * Finalize all of this collection's plugins
  34      *
  35      * @return void
  36      */
  37  	public function finalize()
  38      {
  39          foreach ($this->items as $plugin)
  40          {
  41              $plugin->finalize();
  42          }
  43      }
  44  
  45      /**
  46      * Validate a plugin name
  47      *
  48      * @param  string $pluginName
  49      * @return string
  50      */
  51  	public function normalizeKey($pluginName)
  52      {
  53          if (!preg_match('#^[A-Z][A-Za-z_0-9]+$#D', $pluginName))
  54          {
  55              throw new InvalidArgumentException("Invalid plugin name '" . $pluginName . "'");
  56          }
  57  
  58          return $pluginName;
  59      }
  60  
  61      /**
  62      * Create a plugin instance/ensure it implements the correct interface
  63      *
  64      * @param  mixed $value Either a class name or an object that implements ConfiguratorBase
  65      * @return ConfiguratorBase
  66      */
  67  	public function normalizeValue($value)
  68      {
  69          if (is_string($value) && class_exists($value))
  70          {
  71              $value = new $value($this->configurator);
  72          }
  73  
  74          if ($value instanceof ConfiguratorBase)
  75          {
  76              return $value;
  77          }
  78  
  79          throw new InvalidArgumentException('PluginCollection::normalizeValue() expects a class name or an object that implements s9e\\TextFormatter\\Plugins\\ConfiguratorBase');
  80      }
  81  
  82      /**
  83      * Load a default plugin
  84      *
  85      * @param  string $pluginName    Name of the plugin
  86      * @param  array  $overrideProps Properties of the plugin will be overwritten with those
  87      * @return ConfiguratorBase
  88      */
  89  	public function load($pluginName, array $overrideProps = [])
  90      {
  91          // Validate the plugin name / class
  92          $pluginName = $this->normalizeKey($pluginName);
  93          $className  = 's9e\\TextFormatter\\Plugins\\' . $pluginName . '\\Configurator';
  94  
  95          if (!class_exists($className))
  96          {
  97              throw new RuntimeException("Class '" . $className . "' does not exist");
  98          }
  99  
 100          // Create the plugin
 101          $plugin = new $className($this->configurator, $overrideProps);
 102  
 103          // Save it
 104          $this->set($pluginName, $plugin);
 105  
 106          // Return it
 107          return $plugin;
 108      }
 109  
 110      /**
 111      * {@inheritdoc}
 112      */
 113  	public function asConfig()
 114      {
 115          $plugins = parent::asConfig();
 116  
 117          // Adjust plugins' default properties
 118          foreach ($plugins as $pluginName => &$pluginConfig)
 119          {
 120              $plugin = $this->get($pluginName);
 121  
 122              // Add base properties
 123              $pluginConfig += $plugin->getBaseProperties();
 124  
 125              // Remove quickMatch if it's false
 126              if ($pluginConfig['quickMatch'] === false)
 127              {
 128                  unset($pluginConfig['quickMatch']);
 129              }
 130  
 131              // Remove regexpLimit if there's no regexp
 132              if (!isset($pluginConfig['regexp']))
 133              {
 134                  unset($pluginConfig['regexpLimit']);
 135              }
 136  
 137              // Remove className if it's a default plugin using its default name. Its class name will
 138              // be generated by the parser automatically
 139              $className = 's9e\\TextFormatter\\Plugins\\' . $pluginName . '\\Parser';
 140              if ($pluginConfig['className'] === $className)
 141              {
 142                  unset($pluginConfig['className']);
 143              }
 144          }
 145          unset($pluginConfig);
 146  
 147          return $plugins;
 148      }
 149  }


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