[ Index ]

PHP Cross Reference of phpBB-3.3.14-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/BBCodes/ -> 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\Plugins\BBCodes;
   9  
  10  use ArrayAccess;
  11  use Countable;
  12  use InvalidArgumentException;
  13  use Iterator;
  14  use RuntimeException;
  15  use s9e\TextFormatter\Configurator\Helpers\RegexpBuilder;
  16  use s9e\TextFormatter\Configurator\Helpers\RegexpParser;
  17  use s9e\TextFormatter\Configurator\JavaScript\Dictionary;
  18  use s9e\TextFormatter\Configurator\Traits\CollectionProxy;
  19  use s9e\TextFormatter\Plugins\BBCodes\Configurator\BBCode;
  20  use s9e\TextFormatter\Plugins\BBCodes\Configurator\BBCodeCollection;
  21  use s9e\TextFormatter\Plugins\BBCodes\Configurator\BBCodeMonkey;
  22  use s9e\TextFormatter\Plugins\BBCodes\Configurator\Repository;
  23  use s9e\TextFormatter\Plugins\BBCodes\Configurator\RepositoryCollection;
  24  use s9e\TextFormatter\Plugins\ConfiguratorBase;
  25  
  26  /**
  27  * @method mixed   add(string $key, mixed $value) Add an item to this collection
  28  * @method array   asConfig()
  29  * @method void    clear()                        Empty this collection
  30  * @method bool    contains(mixed $value)         Test whether a given value is present in this collection
  31  * @method integer count()
  32  * @method mixed   current()
  33  * @method void    delete(string $key)            Delete an item from this collection
  34  * @method bool    exists(string $key)            Test whether an item of given key exists
  35  * @method mixed   get(string $key)               Return a value from this collection
  36  * @method mixed   indexOf(mixed $value)          Find the index of a given value
  37  * @method integer|string key()
  38  * @method mixed   next()
  39  * @method string  normalizeKey(string $key)      Normalize an item's key
  40  * @method mixed   normalizeValue(mixed $value)   Normalize a value for storage
  41  * @method bool    offsetExists(string|integer $offset)
  42  * @method mixed   offsetGet(string|integer $offset)
  43  * @method void    offsetSet(string|integer $offset, mixed $value)
  44  * @method void    offsetUnset(string|integer $offset)
  45  * @method string  onDuplicate(string|null $action) Query and set the action to take when add() is called with a key that already exists
  46  * @method void    rewind()
  47  * @method mixed   set(string $key, mixed $value) Set and overwrite a value in this collection
  48  * @method bool    valid()
  49  */
  50  class Configurator extends ConfiguratorBase implements ArrayAccess, Countable, Iterator
  51  {
  52      use CollectionProxy;
  53  
  54      /**
  55      * @var BBCodeMonkey Instance of BBCodeMonkey used to parse definitions
  56      */
  57      public $bbcodeMonkey;
  58  
  59      /**
  60      * @var BBCodeCollection BBCode collection
  61      */
  62      public $collection;
  63  
  64      /**
  65      * {@inheritdoc}
  66      */
  67      protected $quickMatch = '[';
  68  
  69      /**
  70      * {@inheritdoc}
  71      */
  72      protected $regexp = '#\\[/?(\\*|[-\\w]+)(?=[\\]\\s=:/])#';
  73  
  74      /**
  75      * @var RepositoryCollection BBCode repositories
  76      */
  77      public $repositories;
  78  
  79      /**
  80      * Plugin setup
  81      *
  82      * @return void
  83      */
  84  	protected function setUp()
  85      {
  86          $this->bbcodeMonkey = new BBCodeMonkey($this->configurator);
  87          $this->collection   = new BBCodeCollection;
  88          $this->repositories = new RepositoryCollection($this->bbcodeMonkey);
  89          $this->repositories->add('default', __DIR__ . '/Configurator/repository.xml');
  90      }
  91  
  92      /**
  93      * Add a BBCode using their human-readable representation
  94      *
  95      * @see s9e\TextFormatter\Plugins\BBCodes\Configurator\BBCodeMonkey
  96      *
  97      * @param  string $usage    BBCode's usage
  98      * @param  string|\s9e\TextFormatter\Configurator\Items\Template $template BBCode's template
  99      * @param  array  $options  Supported: 'tagName' and 'rules'
 100      * @return BBCode           Newly-created BBCode
 101      */
 102  	public function addCustom($usage, $template, array $options = [])
 103      {
 104          $config = $this->bbcodeMonkey->create($usage, $template);
 105  
 106          if (isset($options['tagName']))
 107          {
 108              $config['bbcode']->tagName = $options['tagName'];
 109          }
 110  
 111          if (isset($options['rules']))
 112          {
 113              $config['tag']->rules->merge($options['rules']);
 114          }
 115  
 116          return $this->addFromConfig($config);
 117      }
 118  
 119      /**
 120      * Add a BBCode from a repository
 121      *
 122      * @param  string $name       Name of the entry in the repository
 123      * @param  mixed  $repository Name of the repository to use as source, or instance of Repository
 124      * @param  array  $vars       Variables that will replace default values in the tag definition
 125      * @return BBCode             Newly-created BBCode
 126      */
 127  	public function addFromRepository($name, $repository = 'default', array $vars = [])
 128      {
 129          // Load the Repository if necessary
 130          if (!($repository instanceof Repository))
 131          {
 132              if (!$this->repositories->exists($repository))
 133              {
 134                  throw new InvalidArgumentException("Repository '" . $repository . "' does not exist");
 135              }
 136  
 137              $repository = $this->repositories->get($repository);
 138          }
 139  
 140          return $this->addFromConfig($repository->get($name, $vars));
 141      }
 142  
 143      /**
 144      * Add a BBCode and its tag based on the return config from BBCodeMonkey
 145      *
 146      * @param  array  $config BBCodeMonkey::create()'s return array
 147      * @return BBCode
 148      */
 149  	protected function addFromConfig(array $config)
 150      {
 151          $bbcodeName = $config['bbcodeName'];
 152          $bbcode     = $config['bbcode'];
 153          $tag        = $config['tag'];
 154  
 155          // If the BBCode doesn't specify a tag name, it's the same as the BBCode
 156          if (!isset($bbcode->tagName))
 157          {
 158              $bbcode->tagName = $bbcodeName;
 159          }
 160  
 161          // Normalize this tag's templates
 162          $this->configurator->templateNormalizer->normalizeTag($tag);
 163  
 164          // Test whether this BBCode/tag is safe before adding it
 165          $this->configurator->templateChecker->checkTag($tag);
 166  
 167          // Add our BBCode then its tag
 168          $this->collection->add($bbcodeName, $bbcode);
 169          $this->configurator->tags->add($bbcode->tagName, $tag);
 170  
 171          return $bbcode;
 172      }
 173  
 174      /**
 175      * {@inheritdoc}
 176      */
 177  	public function asConfig()
 178      {
 179          if (!count($this->collection))
 180          {
 181              return;
 182          }
 183  
 184          return [
 185              'bbcodes'    => $this->collection->asConfig(),
 186              'quickMatch' => $this->quickMatch,
 187              'regexp'     => $this->regexp
 188          ];
 189      }
 190  }


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