1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace YonaCMS; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* Bootstrap |
7
|
|
|
* @copyright Copyright (c) 2011 - 2014 Aleksandr Torosh (http://wezoom.com.ua) |
8
|
|
|
* @author Aleksandr Torosh <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class Bootstrap |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
public function run() |
14
|
|
|
{ |
15
|
|
|
$di = new \Phalcon\DI\FactoryDefault(); |
16
|
|
|
|
17
|
|
|
// Config |
18
|
|
|
require_once APPLICATION_PATH . '/modules/Cms/Config.php'; |
19
|
|
|
$config = \Cms\Config::get(); |
20
|
|
|
$di->set('config', $config); |
21
|
|
|
|
22
|
|
|
// Registry |
23
|
|
|
$registry = new \Phalcon\Registry(); |
24
|
|
|
$di->set('registry', $registry); |
25
|
|
|
|
26
|
|
|
// Loader |
27
|
|
|
$loader = new \Phalcon\Loader(); |
28
|
|
|
$loader->registerNamespaces($config->loader->namespaces->toArray()); |
|
|
|
|
29
|
|
|
$loader->registerDirs([APPLICATION_PATH . "/plugins/"]); |
30
|
|
|
$loader->register(); |
31
|
|
|
require_once APPLICATION_PATH . '/../vendor/autoload.php'; |
32
|
|
|
|
33
|
|
|
// Database |
34
|
|
|
$db = new \Phalcon\Db\Adapter\Pdo\Mysql([ |
35
|
|
|
"host" => $config->database->host, |
|
|
|
|
36
|
|
|
"username" => $config->database->username, |
37
|
|
|
"password" => $config->database->password, |
38
|
|
|
"dbname" => $config->database->dbname, |
39
|
|
|
"charset" => $config->database->charset, |
40
|
|
|
]); |
41
|
|
|
$di->set('db', $db); |
42
|
|
|
|
43
|
|
|
// View |
44
|
|
|
$this->initView($di); |
45
|
|
|
|
46
|
|
|
// URL |
47
|
|
|
$url = new \Phalcon\Mvc\Url(); |
48
|
|
|
$url->setBasePath($config->base_path); |
|
|
|
|
49
|
|
|
$url->setBaseUri($config->base_path); |
50
|
|
|
$di->set('url', $url); |
51
|
|
|
|
52
|
|
|
// Cache |
53
|
|
|
$this->initCache($di); |
54
|
|
|
|
55
|
|
|
// CMS |
56
|
|
|
$cmsModel = new \Cms\Model\Configuration(); |
57
|
|
|
$registry->cms = $cmsModel->getConfig(); // Отправляем в Registry |
58
|
|
|
|
59
|
|
|
// Application |
60
|
|
|
$application = new \Phalcon\Mvc\Application(); |
61
|
|
|
$application->registerModules($config->modules->toArray()); |
|
|
|
|
62
|
|
|
|
63
|
|
|
// Events Manager, Dispatcher |
64
|
|
|
$this->initEventManager($di); |
65
|
|
|
|
66
|
|
|
// Session |
67
|
|
|
$session = new \Phalcon\Session\Adapter\Files(); |
68
|
|
|
$session->start(); |
69
|
|
|
$di->set('session', $session); |
70
|
|
|
|
71
|
|
|
$acl = new \Application\Acl\DefaultAcl(); |
72
|
|
|
$di->set('acl', $acl); |
73
|
|
|
|
74
|
|
|
// JS Assets |
75
|
|
|
$this->initAssetsManager($di); |
76
|
|
|
|
77
|
|
|
// Flash helper |
78
|
|
|
$flash = new \Phalcon\Flash\Session([ |
79
|
|
|
'error' => 'ui red inverted segment', |
80
|
|
|
'success' => 'ui green inverted segment', |
81
|
|
|
'notice' => 'ui blue inverted segment', |
82
|
|
|
'warning' => 'ui orange inverted segment', |
83
|
|
|
]); |
84
|
|
|
$di->set('flash', $flash); |
85
|
|
|
|
86
|
|
|
$di->set('helper', new \Application\Mvc\Helper()); |
87
|
|
|
|
88
|
|
|
// Routing |
89
|
|
|
$this->initRouting($application, $di); |
90
|
|
|
|
91
|
|
|
$application->setDI($di); |
92
|
|
|
|
93
|
|
|
// Main dispatching process |
94
|
|
|
$this->dispatch($di); |
95
|
|
|
|
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
private function initRouting($application, $di) |
99
|
|
|
{ |
100
|
|
|
$router = new \Application\Mvc\Router\DefaultRouter(); |
101
|
|
|
$router->setDi($di); |
102
|
|
|
foreach ($application->getModules() as $module) { |
103
|
|
|
$routesClassName = str_replace('Module', 'Routes', $module['className']); |
104
|
|
|
if (class_exists($routesClassName)) { |
105
|
|
|
$routesClass = new $routesClassName(); |
106
|
|
|
$router = $routesClass->init($router); |
107
|
|
|
} |
108
|
|
|
$initClassName = str_replace('Module', 'Init', $module['className']); |
109
|
|
|
if (class_exists($initClassName)) { |
110
|
|
|
new $initClassName(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
$di->set('router', $router); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
private function initAssetsManager($di) |
117
|
|
|
{ |
118
|
|
|
$config = $di->get('config'); |
119
|
|
|
$assetsManager = new \Application\Assets\Manager(); |
120
|
|
|
$js_collection = $assetsManager->collection('js') |
121
|
|
|
->setLocal(true) |
122
|
|
|
->addFilter(new \Phalcon\Assets\Filters\Jsmin()) |
123
|
|
|
->setTargetPath(ROOT . '/assets/js.js') |
124
|
|
|
->setTargetUri('assets/js.js') |
125
|
|
|
->join(true); |
126
|
|
|
if ($config->assets->js) { |
127
|
|
|
foreach ($config->assets->js as $js) { |
128
|
|
|
$js_collection->addJs(ROOT . '/' . $js); |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
// Admin JS Assets |
133
|
|
|
$assetsManager->collection('modules-admin-js') |
134
|
|
|
->setLocal(true) |
135
|
|
|
->addFilter(new \Phalcon\Assets\Filters\Jsmin()) |
136
|
|
|
->setTargetPath(ROOT . '/assets/modules-admin.js') |
137
|
|
|
->setTargetUri('assets/modules-admin.js') |
138
|
|
|
->join(true); |
139
|
|
|
|
140
|
|
|
// Admin LESS Assets |
141
|
|
|
$assetsManager->collection('modules-admin-less') |
142
|
|
|
->setLocal(true) |
143
|
|
|
->addFilter(new \Application\Assets\Filter\Less()) |
144
|
|
|
->setTargetPath(ROOT . '/assets/modules-admin.less') |
145
|
|
|
->setTargetUri('assets/modules-admin.less') |
146
|
|
|
->join(true) |
147
|
|
|
->addCss(APPLICATION_PATH . '/modules/Admin/assets/admin.less'); |
148
|
|
|
|
149
|
|
|
$di->set('assets', $assetsManager); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
private function initEventManager($di) |
153
|
|
|
{ |
154
|
|
|
$eventsManager = new \Phalcon\Events\Manager(); |
155
|
|
|
$dispatcher = new \Phalcon\Mvc\Dispatcher(); |
156
|
|
|
|
157
|
|
|
$eventsManager->attach("dispatch:beforeDispatchLoop", function ($event, $dispatcher) use ($di) { |
158
|
|
|
new \YonaCMS\Plugin\CheckPoint($di->get('request')); |
159
|
|
|
new \YonaCMS\Plugin\Localization($dispatcher); |
160
|
|
|
new \YonaCMS\Plugin\AdminLocalization($di->get('config')); |
161
|
|
|
new \YonaCMS\Plugin\Acl($di->get('acl'), $dispatcher, $di->get('view')); |
162
|
|
|
new \YonaCMS\Plugin\MobileDetect($di->get('session'), $di->get('view'), $di->get('request')); |
163
|
|
|
}); |
164
|
|
|
|
165
|
|
|
$eventsManager->attach("dispatch:afterDispatchLoop", function ($event, $dispatcher) use ($di) { |
166
|
|
|
new \Seo\Plugin\SeoManager($dispatcher, $di->get('request'), $di->get('router'), $di->get('view')); |
167
|
|
|
new \YonaCMS\Plugin\Title($di); |
168
|
|
|
}); |
169
|
|
|
|
170
|
|
|
// Profiler |
171
|
|
|
$registry = $di->get('registry'); |
172
|
|
|
if ($registry->cms['PROFILER']) { |
173
|
|
|
$profiler = new \Phalcon\Db\Profiler(); |
174
|
|
|
$di->set('profiler', $profiler); |
175
|
|
|
|
176
|
|
|
$eventsManager->attach('db', function ($event, $db) use ($profiler) { |
177
|
|
|
if ($event->getType() == 'beforeQuery') { |
178
|
|
|
$profiler->startProfile($db->getSQLStatement()); |
179
|
|
|
} |
180
|
|
|
if ($event->getType() == 'afterQuery') { |
181
|
|
|
$profiler->stopProfile(); |
182
|
|
|
} |
183
|
|
|
}); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
$db = $di->get('db'); |
187
|
|
|
$db->setEventsManager($eventsManager); |
188
|
|
|
|
189
|
|
|
$dispatcher->setEventsManager($eventsManager); |
190
|
|
|
$di->set('dispatcher', $dispatcher); |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
private function initView($di) |
194
|
|
|
{ |
195
|
|
|
$view = new \Phalcon\Mvc\View(); |
196
|
|
|
|
197
|
|
|
define('MAIN_VIEW_PATH', '../../../views/'); |
198
|
|
|
$view->setMainView(MAIN_VIEW_PATH . 'main'); |
199
|
|
|
$view->setLayoutsDir(MAIN_VIEW_PATH . '/layouts/'); |
200
|
|
|
$view->setLayout('main'); |
201
|
|
|
$view->setPartialsDir(MAIN_VIEW_PATH . '/partials/'); |
202
|
|
|
|
203
|
|
|
// Volt |
204
|
|
|
$volt = new \Application\Mvc\View\Engine\Volt($view, $di); |
205
|
|
|
$volt->setOptions(['compiledPath' => APPLICATION_PATH . '/../data/cache/volt/']); |
206
|
|
|
$volt->initCompiler(); |
207
|
|
|
|
208
|
|
|
|
209
|
|
|
$phtml = new \Phalcon\Mvc\View\Engine\Php($view, $di); |
210
|
|
|
$viewEngines = [ |
211
|
|
|
".volt" => $volt, |
212
|
|
|
".phtml" => $phtml, |
213
|
|
|
]; |
214
|
|
|
|
215
|
|
|
$view->registerEngines($viewEngines); |
216
|
|
|
|
217
|
|
|
$ajax = $di->get('request')->getQuery('_ajax'); |
218
|
|
|
if ($ajax) { |
219
|
|
|
$view->setRenderLevel(\Phalcon\Mvc\View::LEVEL_LAYOUT); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
$di->set('view', $view); |
223
|
|
|
|
224
|
|
|
return $view; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
private function initCache($di) |
228
|
|
|
{ |
229
|
|
|
$config = $di->get('config'); |
230
|
|
|
|
231
|
|
|
$cacheFrontend = new \Phalcon\Cache\Frontend\Data([ |
232
|
|
|
"lifetime" => 60, |
233
|
|
|
"prefix" => HOST_HASH, |
234
|
|
|
]); |
235
|
|
|
|
236
|
|
|
$cache = null; |
237
|
|
|
switch ($config->cache) { |
238
|
|
|
case 'file': |
239
|
|
|
$cache = new \Phalcon\Cache\Backend\File($cacheFrontend, [ |
240
|
|
|
"cacheDir" => APPLICATION_PATH . "/../data/cache/backend/" |
241
|
|
|
]); |
242
|
|
|
break; |
243
|
|
|
case 'memcache': |
244
|
|
|
$cache = new \Phalcon\Cache\Backend\Memcache( |
245
|
|
|
$cacheFrontend, [ |
246
|
|
|
"host" => $config->memcache->host, |
247
|
|
|
"port" => $config->memcache->port, |
248
|
|
|
]); |
249
|
|
|
break; |
250
|
|
|
} |
251
|
|
|
$di->set('cache', $cache, true); |
252
|
|
|
$di->set('modelsCache', $cache, true); |
253
|
|
|
|
254
|
|
|
\Application\Widget\Proxy::$cache = $cache; // Modules Widget System |
255
|
|
|
|
256
|
|
|
$modelsMetadata = new \Phalcon\Mvc\Model\Metadata\Memory(); |
257
|
|
|
$di->set('modelsMetadata', $modelsMetadata); |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
private function dispatch($di) |
261
|
|
|
{ |
262
|
|
|
$router = $di['router']; |
263
|
|
|
|
264
|
|
|
$router->handle(); |
265
|
|
|
|
266
|
|
|
$view = $di['view']; |
267
|
|
|
|
268
|
|
|
$dispatcher = $di['dispatcher']; |
269
|
|
|
|
270
|
|
|
$response = $di['response']; |
271
|
|
|
|
272
|
|
|
$dispatcher->setModuleName($router->getModuleName()); |
273
|
|
|
$dispatcher->setControllerName($router->getControllerName()); |
274
|
|
|
$dispatcher->setActionName($router->getActionName()); |
275
|
|
|
$dispatcher->setParams($router->getParams()); |
276
|
|
|
|
277
|
|
|
$moduleName = \Application\Utils\ModuleName::camelize($router->getModuleName()); |
278
|
|
|
|
279
|
|
|
$ModuleClassName = $moduleName . '\Module'; |
280
|
|
|
if (class_exists($ModuleClassName)) { |
281
|
|
|
$module = new $ModuleClassName; |
282
|
|
|
$module->registerAutoloaders(); |
283
|
|
|
$module->registerServices($di); |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
$view->start(); |
287
|
|
|
|
288
|
|
|
$registry = $di['registry']; |
289
|
|
|
if ($registry->cms['DEBUG_MODE']) { |
290
|
|
|
$debug = new \Phalcon\Debug(); |
291
|
|
|
$debug->listen(); |
292
|
|
|
|
293
|
|
|
$dispatcher->dispatch(); |
294
|
|
|
} else { |
295
|
|
|
try { |
296
|
|
|
$dispatcher->dispatch(); |
297
|
|
|
} catch (\Phalcon\Exception $e) { |
298
|
|
|
// Errors catching |
299
|
|
|
|
300
|
|
|
$view->setViewsDir(__DIR__ . '/modules/Index/views/'); |
301
|
|
|
$view->setPartialsDir(''); |
302
|
|
|
$view->e = $e; |
303
|
|
|
|
304
|
|
|
if ($e instanceof \Phalcon\Mvc\Dispatcher\Exception) { |
305
|
|
|
$response->setHeader(404, 'Not Found'); |
306
|
|
|
$view->partial('error/error404'); |
307
|
|
|
} else { |
308
|
|
|
$response->setHeader(503, 'Service Unavailable'); |
309
|
|
|
$view->partial('error/error503'); |
310
|
|
|
} |
311
|
|
|
$response->sendHeaders(); |
312
|
|
|
echo $response->getContent(); |
313
|
|
|
return; |
314
|
|
|
|
315
|
|
|
} |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
$view->render( |
319
|
|
|
$dispatcher->getControllerName(), |
320
|
|
|
$dispatcher->getActionName(), |
321
|
|
|
$dispatcher->getParams() |
322
|
|
|
); |
323
|
|
|
|
324
|
|
|
$view->finish(); |
325
|
|
|
|
326
|
|
|
$response = $di['response']; |
327
|
|
|
|
328
|
|
|
// AJAX |
329
|
|
|
$request = $di['request']; |
330
|
|
|
$_ajax = $request->getQuery('_ajax'); |
331
|
|
|
if ($_ajax) { |
332
|
|
|
$contents = $view->getContent(); |
333
|
|
|
|
334
|
|
|
$return = new \stdClass(); |
335
|
|
|
$return->html = $contents; |
336
|
|
|
$return->title = $di->get('helper')->title()->get(); |
337
|
|
|
$return->success = true; |
338
|
|
|
|
339
|
|
|
if ($view->bodyClass) { |
340
|
|
|
$return->bodyClass = $view->bodyClass; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
$headers = $response->getHeaders()->toArray(); |
344
|
|
|
if (isset($headers[404]) || isset($headers[503])) { |
345
|
|
|
$return->success = false; |
346
|
|
|
} |
347
|
|
|
$response->setContentType('application/json', 'UTF-8'); |
348
|
|
|
$response->setContent(json_encode($return)); |
349
|
|
|
} else { |
350
|
|
|
$response->setContent($view->getContent()); |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
$response->sendHeaders(); |
354
|
|
|
|
355
|
|
|
echo $response->getContent(); |
356
|
|
|
} |
357
|
|
|
|
358
|
|
|
} |
359
|
|
|
|
An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.
If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.