[ Index ]

PHP Cross Reference of phpBB-3.2.11-deutsch

title

Body

[close]

/vendor/s9e/text-formatter/src/Plugins/BBCodes/Configurator/ -> Repository.php (source)

   1  <?php
   2  
   3  /*
   4  * @package   s9e\TextFormatter
   5  * @copyright Copyright (c) 2010-2019 The s9e Authors
   6  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
   7  */
   8  namespace s9e\TextFormatter\Plugins\BBCodes\Configurator;
   9  use DOMDocument;
  10  use DOMElement;
  11  use DOMXPath;
  12  use InvalidArgumentException;
  13  use RuntimeException;
  14  use s9e\TextFormatter\Configurator\Items\Tag;
  15  class Repository
  16  {
  17      protected $bbcodeMonkey;
  18      protected $dom;
  19      protected $xpath;
  20  	public function __construct($value, BBCodeMonkey $bbcodeMonkey)
  21      {
  22          $this->bbcodeMonkey = $bbcodeMonkey;
  23          $this->dom          = ($value instanceof DOMDocument) ? $value : $this->loadRepository($value);
  24          $this->xpath        = new DOMXPath($this->dom);
  25      }
  26  	public function get($name, array $vars = [])
  27      {
  28          $name = BBCode::normalizeName($name);
  29          $node = $this->xpath->query('//bbcode[@name="' . $name . '"]')->item(0);
  30          if (!($node instanceof DOMElement))
  31              throw new RuntimeException("Could not find '" . $name . "' in repository");
  32          $node = $node->cloneNode(\true);
  33          $this->replaceVars($node, $vars);
  34          $usage    = $this->xpath->evaluate('string(usage)', $node);
  35          $template = $this->xpath->evaluate('string(template)', $node);
  36          $config   = $this->bbcodeMonkey->create($usage, $template);
  37          if ($node->hasAttribute('tagName'))
  38              $config['bbcode']->tagName = $node->getAttribute('tagName');
  39          $this->addRules($node, $config['tag']);
  40          return $config;
  41      }
  42  	protected function addRules(DOMElement $node, Tag $tag)
  43      {
  44          foreach ($this->xpath->query('rules/*', $node) as $ruleNode)
  45          {
  46              $methodName = $ruleNode->nodeName;
  47              $args       = [];
  48              if ($ruleNode->textContent)
  49                  $args[] = $ruleNode->textContent;
  50              \call_user_func_array([$tag->rules, $methodName], $args);
  51          }
  52      }
  53      protected function createRepositoryException($filepath)
  54      {
  55          return new InvalidArgumentException(\var_export($filepath, \true) . ' is not a valid BBCode repository file');
  56      }
  57      protected function loadRepository($filepath)
  58      {
  59          if (!\file_exists($filepath))
  60              throw $this->createRepositoryException($filepath);
  61          $dom = new DOMDocument;
  62          $dom->preserveWhiteSpace = \false;
  63          if (!$dom->loadXML(\file_get_contents($filepath), \LIBXML_NOERROR))
  64              throw $this->createRepositoryException($filepath);
  65          return $dom;
  66      }
  67      protected function replaceVars(DOMElement $node, array $vars)
  68      {
  69          foreach ($this->xpath->query('.//var', $node) as $varNode)
  70          {
  71              $varName = $varNode->getAttribute('name');
  72              if (isset($vars[$varName]))
  73                  $varNode->parentNode->replaceChild(
  74                      $this->dom->createTextNode($vars[$varName]),
  75                      $varNode
  76                  );
  77          }
  78      }
  79  }


Generated: Wed Nov 11 20:33:01 2020 Cross-referenced by PHPXref 0.7.1