1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration\View\Controllers\Helpers; |
4
|
|
|
|
5
|
|
|
use Magium\Configuration\Config\Repository\ConfigInterface; |
6
|
|
|
use Magium\Configuration\File\Context\AbstractContextConfigurationFile; |
7
|
|
|
|
8
|
|
|
class ContextRepository |
9
|
|
|
{ |
10
|
|
|
|
11
|
|
|
protected $contextFile; |
12
|
|
|
|
13
|
10 |
|
public function __construct( |
14
|
|
|
AbstractContextConfigurationFile $contextFile |
15
|
|
|
) |
16
|
|
|
{ |
17
|
10 |
|
$this->contextFile = $contextFile; |
18
|
10 |
|
} |
19
|
|
|
|
20
|
9 |
|
public function getContextHierarchy() |
21
|
|
|
{ |
22
|
9 |
|
$contextsFile = $this->getContextFile(); |
23
|
9 |
|
$xml = $contextsFile->toXml(); |
24
|
9 |
|
$children = []; |
25
|
9 |
|
foreach ($xml->children() as $child) { |
26
|
1 |
|
$children[] = $this->buildContextArray($child); |
27
|
|
|
} |
28
|
9 |
|
$contexts[] = [ |
|
|
|
|
29
|
9 |
|
'id' => 'default', |
30
|
9 |
|
'label' => 'Default', |
31
|
9 |
|
'children' => $children |
32
|
|
|
]; |
33
|
|
|
|
34
|
9 |
|
return $contexts; |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
public function getContextList() |
38
|
|
|
{ |
39
|
|
|
$contexts = [ |
40
|
1 |
|
ConfigInterface::CONTEXT_DEFAULT => 'Default' |
41
|
|
|
]; |
42
|
1 |
|
$xml = $this->contextFile->toXml(); |
43
|
1 |
|
$xml->registerXPathNamespace('s', 'http://www.magiumlib.com/ConfigurationContext'); |
44
|
1 |
|
$nodes = $this->contextFile->toXml()->xpath('//s:context'); |
45
|
1 |
|
foreach ($nodes as $node) { |
46
|
1 |
|
$id = $label = (string)$node['id']; |
47
|
1 |
|
if (isset($node['label'])) { |
48
|
1 |
|
$label = (string)$node['label']; |
49
|
|
|
} |
50
|
1 |
|
$contexts[$id] = $label; |
51
|
|
|
} |
52
|
|
|
|
53
|
1 |
|
return $contexts; |
54
|
|
|
} |
55
|
|
|
|
56
|
9 |
|
public function getContextFile() |
57
|
|
|
{ |
58
|
9 |
|
return $this->contextFile; |
59
|
|
|
} |
60
|
|
|
|
61
|
1 |
|
private function buildContextArray(\SimpleXMLElement $element) |
62
|
|
|
{ |
63
|
1 |
|
$children = []; |
64
|
1 |
|
foreach ($element->children() as $child) { |
65
|
1 |
|
$children[] = $this->buildContextArray($child); |
66
|
|
|
} |
67
|
1 |
|
$label = (string)$element['id']; |
68
|
1 |
|
if (isset($element['label'])) { |
69
|
1 |
|
$label = (string)$element['label']; |
70
|
|
|
} |
71
|
|
|
$contexts = [ |
72
|
1 |
|
'id' => (string)$element['id'], |
73
|
1 |
|
'label' => $label, |
74
|
1 |
|
'children' => $children |
75
|
|
|
]; |
76
|
1 |
|
return $contexts; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
} |
80
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
key is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.