1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Magium\Configuration\View\Controllers; |
4
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
6
|
|
|
use Magium\Configuration\Config\BuilderInterface; |
7
|
|
|
use Magium\Configuration\Config\Repository\ConfigInterface; |
8
|
|
|
use Magium\Configuration\Config\Repository\ConfigurationRepository; |
9
|
|
|
use Magium\Configuration\Config\MergedStructure; |
10
|
|
|
use Magium\Configuration\Config\Storage\StorageInterface; |
11
|
|
|
use Magium\Configuration\Source\SourceInterface; |
12
|
|
|
use Magium\Configuration\View\UnableToCreateInstanceException; |
13
|
|
|
use Magium\Configuration\View\ViewConfiguration; |
14
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
15
|
|
|
use Zend\View\Model\ViewModel; |
16
|
|
|
|
17
|
|
|
class View implements ControllerInterface |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
protected $viewConfiguration; |
21
|
|
|
protected $builder; |
22
|
|
|
protected $mergedConfiguration; |
23
|
|
|
protected $storage; |
24
|
|
|
protected $container; |
25
|
|
|
protected $context; |
26
|
|
|
|
27
|
8 |
|
public function __construct( |
28
|
|
|
ViewConfiguration $viewConfiguration, |
29
|
|
|
BuilderInterface $builder, |
30
|
|
|
MergedStructure $mergedConfiguration, |
31
|
|
|
StorageInterface $storage, |
32
|
|
|
$context, |
33
|
|
|
ContainerInterface $container = null |
34
|
|
|
) |
35
|
|
|
{ |
36
|
8 |
|
$this->viewConfiguration = $viewConfiguration; |
37
|
8 |
|
$this->mergedConfiguration = $mergedConfiguration; |
38
|
8 |
|
$this->builder = $builder; |
39
|
8 |
|
$this->container = $container; |
40
|
8 |
|
$this->context = $context; |
41
|
8 |
|
$this->storage = $storage; |
42
|
8 |
|
} |
43
|
|
|
|
44
|
1 |
|
public function execute(ServerRequestInterface $request) |
45
|
|
|
{ |
46
|
1 |
|
$params = $request->getQueryParams(); |
47
|
1 |
|
if (!isset($params['context'])) { |
48
|
1 |
|
$params['context'] = ConfigurationRepository::CONTEXT_DEFAULT; |
49
|
|
|
} |
50
|
1 |
|
$groups = $this->buildSectionArray($params['section']); |
51
|
1 |
|
$viewModel = new ViewModel([ |
52
|
1 |
|
'groups' => $groups, |
53
|
1 |
|
'section' => $params['section'] |
54
|
|
|
]); |
55
|
1 |
|
$viewModel->setTemplate($this->viewConfiguration->getViewFile()); |
56
|
1 |
|
return $viewModel; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param $class |
61
|
|
|
* @return SourceInterface |
62
|
|
|
* @throws UnableToCreateInstanceException |
63
|
|
|
*/ |
64
|
|
|
|
65
|
4 |
|
public function getSource($class) |
66
|
|
|
{ |
67
|
4 |
|
if ($this->container instanceof ContainerInterface) { |
68
|
1 |
|
if ($this->container->has($class)) { |
69
|
1 |
|
return $this->container->get($class); |
70
|
|
|
} |
71
|
|
|
} |
72
|
3 |
|
$reflectionClass = new \ReflectionClass($class); |
73
|
3 |
|
if (!$reflectionClass->implementsInterface(SourceInterface::class)) { |
74
|
1 |
|
throw new UnableToCreateInstanceException('Source model must implement ' . SourceInterface::class); |
75
|
|
|
} |
76
|
2 |
|
if (!$reflectionClass->getConstructor() || count($reflectionClass->getConstructor()->getParameters()) == 0) { |
77
|
1 |
|
return $reflectionClass->newInstance(); |
78
|
|
|
} |
79
|
1 |
|
throw new UnableToCreateInstanceException('If a source model requires constructor parameters a service manager or DI container must be provided'); |
80
|
|
|
} |
81
|
|
|
|
82
|
6 |
|
public function getStorage() |
83
|
|
|
{ |
84
|
6 |
|
return $this->storage; |
85
|
|
|
} |
86
|
|
|
|
87
|
6 |
|
public function getContext() |
88
|
|
|
{ |
89
|
6 |
|
return $this->context; |
90
|
|
|
} |
91
|
|
|
|
92
|
8 |
|
public function buildSectionArray($section) |
93
|
|
|
{ |
94
|
8 |
|
$xpath = sprintf('//s:section[@identifier="%s" and not(@hidden="yes")]/s:group[not(@hidden="yes")]/s:element[not(@hidden="yes")]', $section); |
95
|
8 |
|
$this->mergedConfiguration->registerXPathNamespace('s', 'http://www.magiumlib.com/Configuration'); |
96
|
8 |
|
$nodes = $this->mergedConfiguration->xpath($xpath); |
97
|
8 |
|
$groups = []; |
98
|
8 |
|
foreach ($nodes as $node) { |
99
|
|
|
/* @var $node \SimpleXMLElement */ |
100
|
8 |
|
$group = $node->xpath('..')[0]; |
101
|
8 |
|
$groupIdentifier = $name = (string)$group['identifier']; |
102
|
8 |
|
if (!isset($groups[$groupIdentifier])) { |
103
|
8 |
|
if (isset($group['label'])) { |
104
|
3 |
|
$name = (string)$group['label']; |
105
|
|
|
} |
106
|
8 |
|
$groups[$groupIdentifier] = [ |
107
|
8 |
|
'label' => $name, |
108
|
|
|
'children' => [] |
109
|
|
|
]; |
110
|
|
|
} |
111
|
8 |
|
$identifier = $label = (string)$node['identifier']; |
112
|
8 |
|
$permittedValues = $source = []; |
113
|
8 |
|
$description = ''; |
114
|
8 |
|
$type = 'text'; |
115
|
8 |
|
if (isset($node['label'])) { |
116
|
3 |
|
$label = (string)$node['label']; |
117
|
|
|
} |
118
|
8 |
|
if (isset($node->description)) { |
119
|
2 |
|
$description = (string)$node->description; |
120
|
|
|
} |
121
|
8 |
|
if (isset($node->permittedValues)) { |
122
|
1 |
|
foreach ($node->permittedValues->value as $value) { |
123
|
1 |
|
$permittedValues[] = (string)$value; |
124
|
|
|
} |
125
|
|
|
} |
126
|
8 |
|
if (isset($node['type'])) { |
127
|
1 |
|
$type = (string)$node['type']; |
128
|
|
|
} |
129
|
8 |
|
if (isset($node['source'])) { |
130
|
4 |
|
$source = $this->getSource((string)$node['source'])->getSourceData(); |
131
|
|
|
} |
132
|
6 |
|
$path = $this->generatePath($node); |
133
|
6 |
|
$value = $this->getStorage()->getValue($path, $this->getContext()); |
134
|
|
|
|
135
|
6 |
|
$groups[$groupIdentifier]['children'][$identifier] = [ |
136
|
6 |
|
'path' => $path, |
137
|
6 |
|
'value' => $value, |
138
|
6 |
|
'source' => $source, |
139
|
6 |
|
'permittedValues' => $permittedValues, |
140
|
6 |
|
'type' => $type, |
141
|
6 |
|
'description' => $description, |
142
|
6 |
|
'label' => $label |
143
|
|
|
]; |
144
|
|
|
} |
145
|
6 |
|
return $groups; |
146
|
|
|
} |
147
|
|
|
|
148
|
6 |
|
protected function generatePath(\SimpleXMLElement $node) |
149
|
|
|
{ |
150
|
6 |
|
$element = (string)$node['identifier']; |
151
|
|
|
|
152
|
6 |
|
$node = $node->xpath('..')[0]; |
153
|
6 |
|
$group = (string)$node['identifier']; |
154
|
|
|
|
155
|
6 |
|
$node = $node->xpath('..')[0]; |
156
|
6 |
|
$section = (string)$node['identifier']; |
157
|
|
|
|
158
|
6 |
|
return sprintf('%s/%s/%s', $section, $group, $element); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|