|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @package s9e\TextFormatter |
|
5
|
|
|
* @copyright Copyright (c) 2010-2016 The s9e Authors |
|
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace s9e\TextFormatter\Plugins\MediaEmbed\Configurator\Collections; |
|
9
|
|
|
|
|
10
|
|
|
use DOMDocument; |
|
11
|
|
|
use DOMElement; |
|
12
|
|
|
use InvalidArgumentException; |
|
13
|
|
|
|
|
14
|
|
|
class XmlFileDefinitionCollection extends SiteDefinitionCollection |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* Constructor |
|
18
|
|
|
* |
|
19
|
|
|
* @param string $path Path to site definitions' dir |
|
20
|
|
|
*/ |
|
21
|
9 |
|
public function __construct($path) |
|
22
|
|
|
{ |
|
23
|
9 |
|
if (!file_exists($path) || !is_dir($path)) |
|
24
|
9 |
|
{ |
|
25
|
1 |
|
throw new InvalidArgumentException('Invalid site directory'); |
|
26
|
|
|
} |
|
27
|
8 |
|
foreach (glob($path . '/*.xml') as $filepath) |
|
28
|
|
|
{ |
|
29
|
8 |
|
$siteId = basename($filepath, '.xml'); |
|
30
|
8 |
|
$this->items[$siteId] = $this->getConfigFromXmlFile($filepath); |
|
31
|
8 |
|
} |
|
32
|
8 |
|
} |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Replace arrays that contain a single element with the element itself |
|
36
|
|
|
* |
|
37
|
|
|
* @param array $config |
|
38
|
|
|
* @return array |
|
39
|
|
|
*/ |
|
40
|
8 |
|
protected function flattenConfig(array $config) |
|
41
|
|
|
{ |
|
42
|
8 |
|
foreach ($config as $k => $v) |
|
43
|
|
|
{ |
|
44
|
8 |
|
if (is_array($v) && count($v) === 1) |
|
45
|
8 |
|
{ |
|
46
|
8 |
|
$config[$k] = end($v); |
|
47
|
8 |
|
} |
|
48
|
8 |
|
} |
|
49
|
|
|
|
|
50
|
8 |
|
return $config; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* Extract a site's config from its XML file |
|
55
|
|
|
* |
|
56
|
|
|
* @param string $filepath Path to the XML file |
|
57
|
|
|
* @return mixed |
|
58
|
|
|
*/ |
|
59
|
8 |
|
protected function getConfigFromXmlFile($filepath) |
|
60
|
|
|
{ |
|
61
|
8 |
|
$dom = new DOMDocument; |
|
62
|
8 |
|
$dom->load($filepath); |
|
63
|
|
|
|
|
64
|
8 |
|
return $this->getElementConfig($dom->documentElement); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Extract a site's config from its XML representation |
|
69
|
|
|
* |
|
70
|
|
|
* @param DOMElement $element Current node |
|
71
|
|
|
* @return mixed |
|
72
|
|
|
*/ |
|
73
|
8 |
|
protected function getElementConfig(DOMElement $element) |
|
74
|
|
|
{ |
|
75
|
8 |
|
$config = []; |
|
76
|
8 |
|
foreach ($element->attributes as $attribute) |
|
77
|
|
|
{ |
|
78
|
8 |
|
$config[$attribute->name][] = $attribute->value; |
|
79
|
8 |
|
} |
|
80
|
8 |
|
foreach ($element->childNodes as $childNode) |
|
81
|
|
|
{ |
|
82
|
8 |
|
if ($childNode instanceof DOMElement) |
|
83
|
8 |
|
{ |
|
84
|
8 |
|
$config[$childNode->nodeName][] = $this->getValueFromElement($childNode); |
|
85
|
8 |
|
} |
|
86
|
8 |
|
} |
|
87
|
|
|
|
|
88
|
8 |
|
return $this->flattenConfig($config); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Extract a value from given element |
|
93
|
|
|
* |
|
94
|
|
|
* @param DOMElement $element |
|
95
|
|
|
* @return mixed |
|
96
|
|
|
*/ |
|
97
|
8 |
|
protected function getValueFromElement(DOMElement $element) |
|
98
|
|
|
{ |
|
99
|
8 |
|
return (!$element->attributes->length && $element->childNodes->length === 1) |
|
100
|
8 |
|
? $element->nodeValue |
|
101
|
8 |
|
: $this->getElementConfig($element); |
|
102
|
|
|
} |
|
103
|
|
|
} |