|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Magium\Configuration\View; |
|
4
|
|
|
|
|
5
|
|
|
use Interop\Container\ContainerInterface; |
|
6
|
|
|
use Magium\Configuration\Config\Repository\ConfigurationRepository; |
|
7
|
|
|
use Magium\Configuration\MagiumConfigurationFactoryInterface; |
|
8
|
|
|
use Magium\Configuration\View\Controllers\Layout; |
|
9
|
|
|
use Magium\Configuration\View\Controllers\Rebuild; |
|
10
|
|
|
use Magium\Configuration\View\Controllers\Save; |
|
11
|
|
|
use Magium\Configuration\View\Controllers\View; |
|
12
|
|
|
use Zend\View\Model\JsonModel; |
|
13
|
|
|
use Zend\View\Model\ViewModel; |
|
14
|
|
|
use Zend\View\Renderer\PhpRenderer; |
|
15
|
|
|
use Zend\View\Resolver\TemplatePathStack; |
|
16
|
|
|
|
|
17
|
|
|
class FrontController |
|
18
|
|
|
{ |
|
19
|
|
|
|
|
20
|
|
|
protected $viewConfiguration; |
|
21
|
|
|
protected $magiumConfigurationFactory; |
|
22
|
|
|
protected $diContainer; |
|
23
|
|
|
protected $view; |
|
24
|
|
|
|
|
25
|
1 |
|
public function __construct( |
|
26
|
|
|
ViewConfiguration $viewConfiguration, |
|
27
|
|
|
MagiumConfigurationFactoryInterface $magiumConfigurationFactory, |
|
28
|
|
|
ContainerInterface $diContainer |
|
29
|
|
|
) |
|
30
|
|
|
{ |
|
31
|
1 |
|
$this->viewConfiguration = $viewConfiguration; |
|
32
|
1 |
|
$this->magiumConfigurationFactory = $magiumConfigurationFactory; |
|
33
|
1 |
|
$this->diContainer = $diContainer; |
|
34
|
1 |
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @return \Psr\Http\Message\ResponseInterface |
|
38
|
|
|
*/ |
|
39
|
|
|
|
|
40
|
9 |
|
public function render() |
|
41
|
|
|
{ |
|
42
|
9 |
|
$request = $this->getRequest(); |
|
43
|
9 |
|
$params = $request->getQueryParams(); |
|
44
|
9 |
|
$viewModel = null; |
|
45
|
|
|
|
|
46
|
|
|
// This pseudo router section is because I don't want to create yet-another-dependency in the project. |
|
47
|
9 |
|
if ($request->getMethod() == 'POST' && isset($params['rebuild'])) { |
|
48
|
|
|
$rebuild = new Rebuild($this->getBuilder(), $this->getContextFile()); |
|
49
|
|
|
$viewModel = $rebuild->execute($request); |
|
50
|
9 |
|
} else if ($request->getMethod() == 'GET' && !isset($params['section'])) { |
|
51
|
8 |
|
$layout = new Layout( |
|
52
|
8 |
|
$this->getViewConfiguration(), |
|
53
|
8 |
|
$this->getContextFile(), |
|
54
|
8 |
|
$this->getConfigurationFactory(), |
|
55
|
8 |
|
$this->getMergedStructure() |
|
56
|
|
|
); |
|
57
|
8 |
|
$viewModel = $layout->execute($request); |
|
58
|
1 |
|
} else if ($request->getMethod() == 'GET' && isset($params['section'])) { |
|
59
|
1 |
|
$context = ConfigurationRepository::CONTEXT_DEFAULT; |
|
60
|
1 |
|
if (isset($params['context'])) { |
|
61
|
|
|
$context = $params['context']; |
|
62
|
|
|
} |
|
63
|
1 |
|
$view = new View( |
|
64
|
1 |
|
$this->getViewConfiguration(), |
|
65
|
1 |
|
$this->getBuilder(), |
|
66
|
1 |
|
$this->getMergedStructure(), |
|
67
|
1 |
|
$this->getStorage(), |
|
68
|
|
|
$context, |
|
69
|
1 |
|
$this->getDiContainer() |
|
70
|
|
|
); |
|
71
|
1 |
|
$viewModel = $view->execute($request); |
|
72
|
|
|
} else if ($request->getMethod() == 'POST') { |
|
73
|
|
|
$save = new Save( |
|
74
|
|
|
$this->getConfigurationFactory()->getBuilder() |
|
75
|
|
|
); |
|
76
|
|
|
$viewModel = $save->execute($request); |
|
77
|
|
|
} else { |
|
78
|
|
|
$this->getResponse()->withStatus(404); |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
9 |
|
$content = ''; |
|
82
|
|
|
|
|
83
|
9 |
|
if ($viewModel instanceof JsonModel) { |
|
84
|
|
|
$content = $viewModel->serialize(); |
|
85
|
9 |
|
} else if ($viewModel instanceof ViewModel) { |
|
86
|
9 |
|
$renderer = new PhpRenderer(); |
|
87
|
9 |
|
$mRenderer = new MagiumRenderer(); |
|
88
|
9 |
|
$mRenderer->setView($renderer); |
|
89
|
9 |
|
$renderer->getHelperPluginManager()->setService('magiumRenderer', $mRenderer); |
|
90
|
|
|
|
|
91
|
9 |
|
$mRenderer = new MagiumRecursiveContextRenderer(); |
|
92
|
9 |
|
$mRenderer->setView($renderer); |
|
93
|
9 |
|
$renderer->getHelperPluginManager()->setService('magiumRecursiveContextRenderer', $mRenderer); |
|
94
|
|
|
|
|
95
|
9 |
|
$renderer->setResolver(new TemplatePathStack(['script_paths' => [$this->getViewConfiguration()->getViewDirectory()]])); |
|
96
|
9 |
|
$content = $renderer->render($viewModel); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
9 |
|
$response = $this->getResponse(); |
|
100
|
9 |
|
$response->getBody()->write($content); |
|
101
|
9 |
|
return $response; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
1 |
|
public function getResponse() |
|
105
|
|
|
{ |
|
106
|
1 |
|
return $this->viewConfiguration->getResponse(); |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
public function getRequest() |
|
110
|
|
|
{ |
|
111
|
1 |
|
return $this->viewConfiguration->getRequest(); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
public function getConfigurationFactory() |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->magiumConfigurationFactory; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
1 |
|
public function getDiContainer() |
|
120
|
|
|
{ |
|
121
|
1 |
|
return $this->diContainer; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
public function getViewConfiguration() |
|
125
|
|
|
{ |
|
126
|
1 |
|
return $this->viewConfiguration; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
public function getBuilder() |
|
130
|
|
|
{ |
|
131
|
|
|
return $this->magiumConfigurationFactory->getBuilder(); |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
|
|
public function getConfiguration($context = ConfigurationRepository::CONTEXT_DEFAULT) |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->magiumConfigurationFactory->getManager()->getConfiguration($context); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
public function getMergedStructure() |
|
140
|
|
|
{ |
|
141
|
|
|
return $this->magiumConfigurationFactory->getBuilder()->getMergedStructure(); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
public function getContextFile() |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->magiumConfigurationFactory->getContextFile(); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
public function getStorage() |
|
150
|
|
|
{ |
|
151
|
|
|
return $this->magiumConfigurationFactory->getBuilder()->getStorage(); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
|
|
} |
|
155
|
|
|
|