forked from Swader/flyingpiranhas-mvc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.php
executable file
·255 lines (212 loc) · 6.09 KB
/
App.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<?php
namespace flyingpiranhas\mvc;
use flyingpiranhas\mvc\interfaces\AppInterface;
use flyingpiranhas\common\dependencyInjection\interfaces\DIContainerInterface;
use flyingpiranhas\mvc\exceptions\MvcException;
use flyingpiranhas\mvc\router\interfaces\AppRouterInterface;
use flyingpiranhas\mvc\interfaces\ModuleBootstrapperInterface;
use Exception;
/**
* @category mvc
* @package flyingpiranhas.mvc
* @license Apache-2.0
* @version 0.01
* @since 2012-09-07
* @author Ivan Pintar
*/
class App implements AppInterface
{
/** @var string */
protected $sProjectDir = '';
/** @var string */
protected $sAppEnv = '';
/** @var string */
protected $sModulesDir = '';
/** @var string */
protected $sViewsDir = '';
/** @var string */
protected $sLayoutsDir = '';
/** @var string */
protected $sViewFragmentsDir = '';
/** @var string */
protected $sDefaultModule = '';
/** @var array */
protected $aModuleNamespaces = array();
/** @var array */
protected $aModuleConfigPaths = array();
/** @var AppRouterInterface */
protected $oRouter;
/** @var DIContainerInterface */
protected $oDIContainer;
/**
* @return string
*/
public function getAppEnv()
{
return $this->sAppEnv;
}
/**
* @return string
*/
public function getProjectDir()
{
return $this->sProjectDir;
}
/**
* @return string
*/
public function getViewsDir()
{
return $this->sViewsDir;
}
/**
* @return string
*/
public function getLayoutsDir()
{
return $this->sLayoutsDir;
}
/**
* @return string
*/
public function getViewFragmentsDir()
{
return $this->sViewFragmentsDir;
}
/**
* @return AppRouterInterface
*/
public final function getRouter()
{
return $this->oRouter;
}
/**
* @return DIContainerInterface
*/
public final function getDIContainer()
{
return $this->oDIContainer;
}
/**
* @param array $aAppSettings
*
* @return App
*/
public function setAppSettings(array $aAppSettings)
{
if (!$this->sModulesDir) $this->sModulesDir = $aAppSettings['modulesDir'];
if (!$this->sViewsDir) $this->sViewsDir = $aAppSettings['viewsDir'];
if (!$this->sLayoutsDir) $this->sLayoutsDir = $aAppSettings['layoutsDir'];
if (!$this->sViewFragmentsDir) $this->sViewFragmentsDir = $aAppSettings['viewFragmentsDir'];
if (!$this->sDefaultModule) $this->sDefaultModule = $aAppSettings['defaultModule'];
if (!$this->aModuleNamespaces) $this->aModuleNamespaces = $aAppSettings['moduleNamespaces'];
if (!$this->aModuleConfigPaths) $this->aModuleConfigPaths = $aAppSettings['moduleConfigPaths'];
return $this;
}
/**
* @param string $sAppEnv
*
* @return App
*/
public function setAppEnv($sAppEnv)
{
$this->sAppEnv = $sAppEnv;
return $this;
}
/**
* @param string $sProjectDir
*
* @return App
*/
public function setProjectDir($sProjectDir)
{
$this->sProjectDir = $sProjectDir;
return $this;
}
/**
* @dependency
*
* @param AppRouterInterface $oRouter
*
* @return App
*/
public final function setRouter(AppRouterInterface $oRouter)
{
$this->oRouter = $oRouter;
return $this;
}
/**
* @dependency
*
* @param DIContainerInterface $oDIContainer
*
* @return App
*/
public final function setDIContainer(DIContainerInterface $oDIContainer)
{
$this->oDIContainer = $oDIContainer;
$this->oDIContainer->registerInstance($this, __CLASS__);
return $this;
}
/**
* Creates and runs a Module for the given module name,
*
* @param string $sModuleName
*
* @throws MvcException
*/
protected final function runModule($sModuleName)
{
$sModuleNamespace =
(isset($this->aModuleNamespaces[$sModuleName]))
? $this->aModuleNamespaces[$sModuleName]
: $sModuleName;
$sModuleConfigPath =
(isset($this->aModuleConfigPaths[$sModuleName]))
? $this->aModuleConfigPaths[$sModuleName]
: 'config/Config.ini';
$sModuleDir = $this->sProjectDir . '/' . $this->sModulesDir . '/' . str_replace('\\', '/', $sModuleNamespace);
$aBootstrapperParams = array(
'sModuleName' => $sModuleName,
'sModuleNamespace' => $sModuleNamespace,
'sModuleDir' => $sModuleDir,
'sAppEnv' => $this->sAppEnv,
'sModuleConfigPath' => $sModuleConfigPath
);
/** @var $oModuleBootstrapper ModuleBootstrapperInterface */
$oModuleBootstrapper = null;
try {
$sModuleClass = $sModuleNamespace . '\\Bootstrapper';
$oModuleBootstrapper = $this->oDIContainer->resolve($sModuleClass, $aBootstrapperParams);
} catch (Exception $oException) {
$oModuleBootstrapper = $this->oDIContainer->resolve('\\flyingpiranhas\\mvc\\ModuleBootstrapper', $aBootstrapperParams);
}
if (!($oModuleBootstrapper instanceof ModuleBootstrapperInterface)) {
throw new MvcException('The module bootstrapper has to implement \\flyingpiranhas\\mvc\\interfaces\\ModuleBootstrapperInterface');
}
$oModuleBootstrapper->run();
}
/**
* Override this method to execute operations before the routing starts
*/
protected function preDispatch()
{
}
/**
* Override this method to execute operations on post dispatch
*/
protected function postDispatch()
{
}
/**
* Parses the request to find out the module.
* It then creates a Module and calls its work() method
*/
public final function work()
{
$this->preDispatch();
$this->oRouter->parseRequest();
$this->runModule($this->oRouter->getModule());
$this->postDispatch();
}
}