-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKernel.php
More file actions
387 lines (309 loc) · 9.7 KB
/
Kernel.php
File metadata and controls
387 lines (309 loc) · 9.7 KB
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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<?php
namespace Phemo;
use Phalcon\Config;
use Phalcon\Db\Exception as ExceptionDb;
use Phalcon\DI;
use Phalcon\DI\FactoryDefault;
use Phalcon\DiInterface;
use Phalcon\Exception;
use Phalcon\Http\Request;
use Phalcon\Http\Response;
use Phalcon\Loader;
use Phalcon\Mvc\Application;
use Phalcon\Mvc\DispatcherInterface;
use Phalcon\Mvc\Model\Transaction\Manager;
use Phalcon\Mvc\Router;
use Phalcon\Mvc\View;
use Phalcon\Session\Adapter\Files;
class Kernel
{
/**
* @var string
*/
protected $environment;
/**
* @var boolean
*/
protected $debug;
/**
* @var Config
*/
protected $config;
/**
* @var DiInterface
*/
protected $di;
/**
* @var Loader
*/
protected $loader;
/**
* @var array
*/
protected $bundlesModules;
/**
* @param stirng $environment
* @param boolean $debug
*/
public function __construct($environment, $debug = false)
{
$this->environment = $environment;
$this->debug = (boolean) $debug;
if ($this->debug) {
error_reporting(E_ALL);
}
else {
error_reporting(0);
}
$config = new Config($this->registerConfig());
$this->setConfig($config);
$this->setBundles($this->registerBundles());
try {
$this->registerAutolader();
$this->confingureAutolader();
$this->initializeBundles();
$this->configureRouter();
$this->confingureServices();
} catch (Exception $e) {
$this->handleException($e);
}
}
public function getEnvironment()
{
return $this->environment;
}
public function setConfig(Config $config)
{
$this->config = $config;
return $this;
}
public function getConfig()
{
return $this->config;
}
public function setBundles($bundles)
{
$this->bundles = $bundles;
return $this;
}
public function getBundles()
{
return $this->bundles;
}
/**
* Handle app request
*/
public function handle()
{
try {
// Handle the request
$application = new Application($this->getDI());
// Register modules
if (is_array($this->bundlesModules)) {
$application->registerModules($this->bundlesModules);
}
/* @var $response Response */
$response = $application->handle();
echo $response->getContent();
} catch (Exception $e) {
$this->handleException($e);
}
}
/**
* Create a DI
*
* @return DI
*/
protected function getDI()
{
if (!$this->di) {
$this->di = new FactoryDefault;
}
return $this->di;
}
/**
* Configure router
*/
private function configureRouter()
{
$di = $this->getDI();
$di->set('router', function() {
$router = new Router($defaultRoutes = false);
/**
* Example:
* phalcon.local/phemo/demo/product/index/?page=2
* phalcon.local/fr/phemo/demo/product/index/?page=2
* phalcon.local/fr-ca/phemo/demo/product/index/?page=2
*
* language = fr
* locale = ca
* module = phemo/demo
* controller = product
* action = index
* params = page=2
*/
/*
$prefixModules = array_keys($this->bundlesModules);
foreach ($prefixModules as $prefix) {
$router->add('/(?:([a-z]{2})(?:-([a-z]{2}))?/)?' . $prefix . '(?:/:controller(?:/:action(?:/:params)?)?)?', [
'language' => 1,
'locale' => 2,
'module' => $prefix,
'controller' => 3,
'action' => 4,
'params' => 5,
]);
}/* */
return $router;
});
}
/**
* Push components into DI
*
* @return DI
*/
private function confingureServices()
{
$di = $this->getDI();
// Register config as service
$di->setShared('config', function() {
return $this->getConfig();
});
$di->setShared('bundles', function() {
return $this->getBundles();
});
/* @var $assets Manager */
$assets = $this->getDI()->get('assets');
$assets->collection('headerCss');
$assets->collection('headerJs');
// Start the session the first time a component requests the session service
$di->set('session', function() {
$session = new Files();
$session->start();
return $session;
});
// Setup the database service
$di->set('db', function() {
$pdoAdapter = '\\Phalcon\\Db\\Adapter\\Pdo\\' . ucfirst($this->config->database->adapter);
unset($this->config->database->adapter);
if (!class_exists($pdoAdapter)) {
throw new ExceptionDb('DB adapter "' . $pdoAdapter . '" not exists! See param database->adapter in config file.');
}
try {
$db = new $pdoAdapter((array) $this->config->database);
} catch (Exception $e) {
$this->handleException($e);
}
return $db;
});
// Setup the view component
$di->set('view', function() {
$templating = $this->config->framework->templating;
$view = new View();
$view->registerEngines((array) $templating->engines);
$view->setViewsDir(__DIR__ . $templating->baseView);
return $view;
});
// Set i13n
$di->setShared('i13n', function() {
/* @var $dispatcher DispatcherInterface */
$dispatcher = $this->getDI()->getShared('dispatcher');
// Get currnet language from query
$language = $dispatcher->getParam('language', 'string');
$locale = $dispatcher->getParam('locale', 'string');
$locale = $locale ? $locale : $language;
if ($language) {
$i13n = $language . '-' . $locale;
}
else {
// Get currnet browser language setting
/* @var $request Request */
$request = $this->getDI()->getShared('request');
$i13n = $request->getBestLanguage();
}
return strtolower($i13n);
});
static::confingureServices();
return $this;
}
/**
* Register an autoloader
*/
private function registerAutolader()
{
if (!$this->loader) {
$this->loader = new Loader();
}
return $this->loader;
}
private function confingureAutolader()
{
$autoloadDirs = $this->config->framework->autoloadDirs;
$namespaces = [];
// Add phemo vendor to autoload
foreach (['Mvc'] as $dir) {
$namespaces['Phemo\\' . $dir] = realpath(__DIR__ . DIRECTORY_SEPARATOR . $dir);
}
$namespaces['Phemo'] = realpath(__DIR__);
// Add autoload bundles
foreach ($this->getBundles() as $bundleNamespace => $bundlePath) {
foreach ($autoloadDirs as $dir) {
$namespaces[$bundleNamespace . '\\' . $dir] = realpath(__DIR__ . str_repeat(DIRECTORY_SEPARATOR . '..', 3) . DIRECTORY_SEPARATOR . $bundlePath . $dir);
}
$namespaces[$bundleNamespace] = realpath(__DIR__ . str_repeat(DIRECTORY_SEPARATOR . '..', 3) . DIRECTORY_SEPARATOR . $bundlePath);
}
$this->loader->registerNamespaces($namespaces);
$this->loader->register();
return $this;
}
/**
* Initialize all project bundles
*/
private function initializeBundles()
{
foreach ($this->getBundles() as $bundleNamespace => $bundlePath) {
$this->initializeBundle($bundleNamespace, $bundlePath);
}
}
/**
* Initialize bundle
*
* @param string $bundleNamespace
* @param string $bundlePath
*/
private function initializeBundle($bundleNamespace, $bundlePath)
{
$bundleNamespaceParsts = preg_split("/\\\/", $bundleNamespace);
$bundleVendorName = array_shift($bundleNamespaceParsts);
$bundleName = array_shift($bundleNamespaceParsts);
$prefix = strtolower($bundleVendorName . '' . str_replace('Bundle', '', $bundleName));
$this->bundlesModules[$prefix] = [
'className' => $bundleNamespace . '\BundleModule',
'path' => realpath(__DIR__ . str_repeat(DIRECTORY_SEPARATOR . '..', 3) . DIRECTORY_SEPARATOR . $bundlePath . 'BundleModule.php'),
];
// Register independent bundle service
$bundleModuleClass = '\\' . $bundleNamespace . '\\BundleModule';
$registerServicesBundleMethod = 'registerServicesBundle';
if (method_exists($bundleModuleClass, $registerServicesBundleMethod)) {
$bundleModuleClass::$registerServicesBundleMethod($this->getDI());
}
}
/**
* Format and output exception info
*
* @todo use prettify tool https:// github.com/phalcon/pretty-exceptions
*
* @param \Exception $e
*/
private function handleException(\Exception $e)
{
$out = '<pre>';
$out .= get_class($e) . ': ' . $e->getMessage() . PHP_EOL;
$out .= 'File: ' . $e->getFile() . PHP_EOL;
$out .= 'Line: ' . $e->getLine() . PHP_EOL;
$out .= '<br/>';
$out .= $e->getTraceAsString();
$out .= '</pre>';
echo $out;
}
}