1 | <?php |
||
17 | class FrontController |
||
18 | { |
||
19 | |||
20 | protected $viewConfiguration; |
||
21 | protected $magiumConfigurationFactory; |
||
22 | protected $diContainer; |
||
23 | protected $view; |
||
24 | |||
25 | 1 | public function __construct( |
|
35 | |||
36 | /** |
||
37 | * @return \Psr\Http\Message\ResponseInterface |
||
38 | */ |
||
39 | |||
40 | 10 | public function render() |
|
41 | { |
||
42 | 10 | $request = $this->getRequest(); |
|
43 | 10 | $params = $request->getQueryParams(); |
|
44 | 10 | $viewModel = null; |
|
45 | |||
46 | // This pseudo router section is because I don't want to create yet-another-dependency in the project. |
||
47 | 10 | if ($request->getMethod() == 'POST' && isset($params['rebuild'])) { |
|
48 | $rebuild = new Rebuild($this->getBuilder(), $this->getContextFile()); |
||
49 | $viewModel = $rebuild->execute($request); |
||
50 | 10 | } else if ($request->getMethod() == 'GET' && !isset($params['section'])) { |
|
51 | 9 | $layout = new Layout( |
|
52 | 9 | $this->getViewConfiguration(), |
|
53 | 9 | $this->getContextFile(), |
|
54 | 9 | $this->getConfigurationFactory(), |
|
55 | 9 | $this->getMergedStructure() |
|
56 | ); |
||
57 | 9 | $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 | 1 | $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 | 10 | $content = ''; |
|
82 | |||
83 | 10 | if ($viewModel instanceof JsonModel) { |
|
84 | $content = $viewModel->serialize(); |
||
85 | 10 | } else if ($viewModel instanceof ViewModel) { |
|
86 | 10 | $renderer = new PhpRenderer(); |
|
87 | 10 | $mRenderer = new MagiumRenderer(); |
|
88 | 10 | $mRenderer->setView($renderer); |
|
89 | 10 | $renderer->getHelperPluginManager()->setService('magiumRenderer', $mRenderer); |
|
90 | |||
91 | 10 | $mRenderer = new MagiumRecursiveContextRenderer(); |
|
92 | 10 | $mRenderer->setView($renderer); |
|
93 | 10 | $renderer->getHelperPluginManager()->setService('magiumRecursiveContextRenderer', $mRenderer); |
|
94 | |||
95 | 10 | $renderer->setResolver(new TemplatePathStack(['script_paths' => [$this->getViewConfiguration()->getViewDirectory()]])); |
|
96 | 10 | $content = $renderer->render($viewModel); |
|
97 | } |
||
98 | |||
99 | 10 | $response = $this->getResponse(); |
|
100 | 10 | $response->getBody()->write($content); |
|
101 | 10 | return $response; |
|
102 | } |
||
103 | |||
104 | 1 | public function getResponse() |
|
108 | |||
109 | 1 | public function getRequest() |
|
113 | |||
114 | public function getConfigurationFactory() |
||
118 | |||
119 | 1 | public function getDiContainer() |
|
123 | |||
124 | 1 | public function getViewConfiguration() |
|
128 | |||
129 | public function getBuilder() |
||
133 | |||
134 | public function getConfiguration($context = ConfigurationRepository::CONTEXT_DEFAULT) |
||
138 | |||
139 | public function getMergedStructure() |
||
143 | |||
144 | public function getContextFile() |
||
148 | |||
149 | public function getStorage() |
||
153 | |||
154 | } |
||
155 |