-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathModule.php
More file actions
executable file
·96 lines (84 loc) · 3.49 KB
/
Module.php
File metadata and controls
executable file
·96 lines (84 loc) · 3.49 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
<?php
namespace Instcar\Admin;
class Module
{
public function registerAutoloaders()
{
$loader = new \Phalcon\Loader();
$loader->registerNamespaces(array(
'Instcar\Admin\Controllers' => __DIR__.'/controllers/',
'Instcar\Admin\Models' => __DIR__.'/models/',
'Instcar\Admin\Logics' => __DIR__.'/logics/',
'Instcar\Admin\Plugins' => __DIR__.'/plugins',
))->register();
}
/**
*
* Register the services here to make them module-specific
*
*/
public function registerServices($di)
{
// get bootstrap obj
$bootstrap = $di->get('bootstrap');
// get config class name
$confClass = $bootstrap->getConfClass();
// module config
$mConfPath = __DIR__.'/confs/'.PHALCON_ENV.'.'.PHALCON_CONF_TYPE;
if(!is_file($mConfPath)) {
throw new \Phalcon\Config\Exception("Module config file not exist, file position: {$mConfPath}");
}
if(PHALCON_CONF_TYPE == 'ini') {
$mConfig = new $confClass($mConfPath);
} else if(PHALCON_CONF_TYPE == 'php') {
$mConfig = new $confClass(require_once($mConfPath));
}
// global config
$gConfig = $di->get('config');
// merge module config and global config, module's will override global's
$gConfig->merge($mConfig);
// set config back
$di->set('config', $gConfig);
// registering a dispatcher
$di->set('dispatcher', function () use ($di) {
$evtManager = $di->getShared('eventsManager');
$evtManager->attach("dispatch:beforeException", function ($event, $dispatcher, $exception) {
switch ($exception->getCode()) {
case \Phalcon\Mvc\Dispatcher::EXCEPTION_HANDLER_NOT_FOUND:
case \Phalcon\Mvc\Dispatcher::EXCEPTION_ACTION_NOT_FOUND:
$dispatcher->forward(array(
'module' => 'admin',
'controller' => 'error',
'action' => 'show404'
));
return false;
}
});
$acl = new \Instcar\Admin\Plugins\Acl($di, $evtManager);
$evtManager->attach('dispatch', $acl);
$dispatcher = new \Phalcon\Mvc\Dispatcher();
$dispatcher->setEventsManager($evtManager);
$dispatcher->setDefaultNamespace("Instcar\Admin\Controllers\\");
return $dispatcher;
});
// set view with volt
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir(__DIR__.'/views/');
$view->registerEngines(array(
".volt" => function($view, $di) {
$volt = new \Phalcon\Mvc\View\Engine\Volt($view, $di);
$volt->setOptions(array(
"compiledPath" => $di->get('config')->view->compiledPath,
"compiledExtension" => $di->get('config')->view->compiledExtension,
"compileAlways" => (bool) $di->get('config')->application->debug
));
$compiler = $volt->getCompiler();
$compiler->addExtension(new \BullSoft\Volt\Extension\PhpFunction());
return $volt;
}
));
return $view;
});
}
}