Verzeichnisstruktur phpBB-3.3.15


Veröffentlicht
28.08.2024

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

(Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

Repository.php

Zuletzt modifiziert: 02.04.2025, 15:04 - Dateigröße: 4.01 KiB


001  <?php
002   
003  /**
004  * @package   s9e\TextFormatter
005  * @copyright Copyright (c) 2010-2022 The s9e authors
006  * @license   http://www.opensource.org/licenses/mit-license.php The MIT License
007  */
008  namespace s9e\TextFormatter\Plugins\BBCodes\Configurator;
009   
010  use DOMDocument;
011  use DOMElement;
012  use DOMXPath;
013  use InvalidArgumentException;
014  use RuntimeException;
015  use s9e\TextFormatter\Configurator\Items\Tag;
016   
017  class Repository
018  {
019      /**
020      * @var BBCodeMonkey Instance of BBCodeMonkey used to parse definitions
021      */
022      protected $bbcodeMonkey;
023   
024      /**
025      * @var DOMDocument Repository document
026      */
027      protected $dom;
028   
029      /**
030      * @var DOMXPath
031      */
032      protected $xpath;
033   
034      /**
035      * Constructor
036      *
037      * @param  mixed        $value        Either a DOMDocument or the path to a repository's XML file
038      * @param  BBCodeMonkey $bbcodeMonkey Instance of BBCodeMonkey used to parse definitions
039      */
040      public function __construct($value, BBCodeMonkey $bbcodeMonkey)
041      {
042          $this->bbcodeMonkey = $bbcodeMonkey;
043          $this->dom          = ($value instanceof DOMDocument) ? $value : $this->loadRepository($value);
044          $this->xpath        = new DOMXPath($this->dom);
045      }
046   
047      /**
048      * Get a BBCode and its associated tag from this repository
049      *
050      * @param  string $name Name of the entry in the repository
051      * @param  array  $vars Replacement variables
052      * @return array        Array with three elements: "bbcode", "name" and "tag"
053      */
054      public function get($name, array $vars = [])
055      {
056          $name = BBCode::normalizeName($name);
057          $node = $this->xpath->query('//bbcode[@name="' . $name . '"]')->item(0);
058          if (!($node instanceof DOMElement))
059          {
060              throw new RuntimeException("Could not find '" . $name . "' in repository");
061          }
062   
063          // Clone the node so we don't end up modifying the node in the repository
064          $node = $node->cloneNode(true);
065   
066          // Replace all the <var> descendants if applicable
067          $this->replaceVars($node, $vars);
068   
069          // Now we can parse the BBCode usage and prepare the template.
070          // Grab the content of the <usage> element then use BBCodeMonkey to parse it
071          $usage    = $this->xpath->evaluate('string(usage)', $node);
072          $template = $this->xpath->evaluate('string(template)', $node);
073          $config   = $this->bbcodeMonkey->create($usage, $template);
074   
075          // Set the optional tag name
076          if ($node->hasAttribute('tagName'))
077          {
078              $config['bbcode']->tagName = $node->getAttribute('tagName');
079          }
080   
081          // Set the rules
082          $this->addRules($node, $config['tag']);
083   
084          return $config;
085      }
086   
087      /**
088      * Add rules to given tag based on given definition
089      *
090      * @param  DOMElement $node
091      * @param  Tag        $tag
092      * @return void
093      */
094      protected function addRules(DOMElement $node, Tag $tag)
095      {
096          foreach ($this->xpath->query('rules/*', $node) as $ruleNode)
097          {
098              $methodName = $ruleNode->nodeName;
099              $args       = [];
100              if ($ruleNode->textContent)
101              {
102                  $args[] = $ruleNode->textContent;
103              }
104   
105              call_user_func_array([$tag->rules, $methodName], $args);
106          }
107      }
108   
109      /**
110      * Create an exception for a bad repository file path
111      *
112      * @param  string $filepath
113      * @return InvalidArgumentException
114      */
115      protected function createRepositoryException($filepath)
116      {
117          return new InvalidArgumentException(var_export($filepath, true) . ' is not a valid BBCode repository file');
118      }
119   
120      /**
121      * Load a repository file into a DOMDocument
122      *
123      * @param  string $filepath
124      * @return DOMDocument
125      */
126      protected function loadRepository($filepath)
127      {
128          if (!file_exists($filepath))
129          {
130              throw $this->createRepositoryException($filepath);
131          }
132   
133          $dom = new DOMDocument;
134          $dom->preserveWhiteSpace = false;
135          if (!$dom->loadXML(file_get_contents($filepath), LIBXML_NOERROR))
136          {
137              throw $this->createRepositoryException($filepath);
138          }
139   
140          return $dom;
141      }
142   
143      /**
144      * Replace var elements in given definition
145      *
146      * @param  DOMElement $node
147      * @param  array      $vars
148      * @return void
149      */
150      protected function replaceVars(DOMElement $node, array $vars)
151      {
152          foreach ($this->xpath->query('.//var', $node) as $varNode)
153          {
154              $varName = $varNode->getAttribute('name');
155   
156              if (isset($vars[$varName]))
157              {
158                  $varNode->parentNode->replaceChild(
159                      $this->dom->createTextNode($vars[$varName]),
160                      $varNode
161                  );
162              }
163          }
164      }
165  }