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. |
|
(Beispiel Datei-Icons)
|
Auf das Icon klicken um den Quellcode anzuzeigen |
XmlFileDefinitionCollection.php
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\MediaEmbed\Configurator\Collections;
009
010 use DOMDocument;
011 use DOMElement;
012 use InvalidArgumentException;
013
014 class XmlFileDefinitionCollection extends SiteDefinitionCollection
015 {
016 /**
017 * @var array Known config types [<name regexp>, <value regexp>, <type>]
018 */
019 protected $configTypes = [
020 ['(^defaultValue$)', '(^(?:0|[1-9][0-9]+)$)D', 'castToInt'],
021 ['(height$|width$)', '(^(?:0|[1-9][0-9]+)$)D', 'castToInt'],
022 ['(^required$)', '(^(?:fals|tru)e$)Di', 'castToBool']
023 ];
024
025 /**
026 * Constructor
027 *
028 * @param string $path Path to site definitions' dir
029 */
030 public function __construct($path)
031 {
032 if (!file_exists($path) || !is_dir($path))
033 {
034 throw new InvalidArgumentException('Invalid site directory');
035 }
036 foreach (glob($path . '/*.xml') as $filepath)
037 {
038 $siteId = basename($filepath, '.xml');
039 $this->add($siteId, $this->getConfigFromXmlFile($filepath));
040 }
041 }
042
043 /**
044 * Cast given config value to the appropriate type
045 *
046 * @param string $name Name of the config value
047 * @param string $value Config value in string form
048 * @return mixed Config value in appropriate type
049 */
050 protected function castConfigValue($name, $value)
051 {
052 foreach ($this->configTypes as list($nameRegexp, $valueRegexp, $methodName))
053 {
054 if (preg_match($nameRegexp, $name) && preg_match($valueRegexp, $value))
055 {
056 return $this->$methodName($value);
057 }
058 }
059
060 return $value;
061 }
062
063 /**
064 * Cast given config value to a boolean
065 *
066 * @param string $value
067 * @return bool
068 */
069 protected function castToBool($value)
070 {
071 return (strtolower($value) === 'true');
072 }
073
074 /**
075 * Cast given config value to an integer
076 *
077 * @param string $value
078 * @return integer
079 */
080 protected function castToInt($value)
081 {
082 return (int) $value;
083 }
084
085 /**
086 * Convert known config values to the appropriate type
087 *
088 * Will cast properties whose name is "defaultValue" or ends in "height" or "width" to integers
089 *
090 * @param array $config Original config
091 * @return array Converted config
092 */
093 protected function convertValueTypes(array $config)
094 {
095 foreach ($config as $k => $v)
096 {
097 if (is_array($v))
098 {
099 $config[$k] = $this->convertValueTypes($v);
100 }
101 elseif (is_string($v))
102 {
103 $config[$k] = $this->castConfigValue($k, $v);
104 }
105 }
106
107 return $config;
108 }
109
110 /**
111 * Replace arrays that contain a single element with the element itself
112 *
113 * @param array $config
114 * @return array
115 */
116 protected function flattenConfig(array $config)
117 {
118 foreach ($config as $k => $v)
119 {
120 if (is_array($v) && count($v) === 1)
121 {
122 $config[$k] = end($v);
123 }
124 }
125
126 return $config;
127 }
128
129 /**
130 * Extract a site's config from its XML file
131 *
132 * @param string $filepath Path to the XML file
133 * @return mixed
134 */
135 protected function getConfigFromXmlFile($filepath)
136 {
137 $dom = new DOMDocument;
138 $dom->loadXML(file_get_contents($filepath), LIBXML_NOCDATA);
139
140 return $this->getElementConfig($dom->documentElement);
141 }
142
143 /**
144 * Extract a site's config from its XML representation
145 *
146 * @param DOMElement $element Current node
147 * @return mixed
148 */
149 protected function getElementConfig(DOMElement $element)
150 {
151 $config = [];
152 foreach ($element->attributes as $attribute)
153 {
154 $config[$attribute->name][] = $attribute->value;
155 }
156 foreach ($element->childNodes as $childNode)
157 {
158 if ($childNode instanceof DOMElement)
159 {
160 $config[$childNode->nodeName][] = $this->getValueFromElement($childNode);
161 }
162 }
163
164 return $this->flattenConfig($this->convertValueTypes($config));
165 }
166
167 /**
168 * Extract a value from given element
169 *
170 * @param DOMElement $element
171 * @return mixed
172 */
173 protected function getValueFromElement(DOMElement $element)
174 {
175 return (!$element->attributes->length && $element->childNodes->length === 1 && $element->firstChild->nodeType === XML_TEXT_NODE)
176 ? $element->nodeValue
177 : $this->getElementConfig($element);
178 }
179 }