1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16:
17:
18: namespace Akufen\Orchestra;
19:
20: 21: 22: 23: 24: 25: 26:
27: class Application extends \Phalcon\Mvc\Application
28: {
29: 30: 31:
32: public function __construct()
33: {
34:
35: $di = new \Phalcon\DI\FactoryDefault();
36:
37:
38: $di->setShared('config', function () {
39:
40:
41: $file = get_template_directory() . '/config.php';
42:
43:
44: if (!file_exists($file)) {
45: throw new \Exception('Unable to locate configuration file.');
46: }
47:
48: return include $file;
49: });
50:
51:
52: $config = $di->getConfig();
53:
54:
55: if (!$config->application->production) {
56:
57: error_reporting(E_ALL);
58:
59:
60: $debug = new \Phalcon\Debug();
61: $debug->listen();
62: }
63:
64:
65: $di->setShared('url', function () use ($config) {
66:
67:
68: $url = new \Phalcon\Mvc\Url();
69:
70:
71: $baseUri = isset($config->application->baseUri)?
72: $config->application->baseUri : '/';
73:
74:
75: $url->setBaseUri($baseUri);
76:
77: return $url;
78: });
79:
80:
81: $di->setShared('session', function () {
82:
83:
84: $session = new \Phalcon\Session\Adapter\Files();
85: $session->start();
86:
87: return $session;
88: });
89:
90:
91: $di->setShared('router', function () use ($config) {
92:
93:
94: $router = new \Phalcon\Mvc\Router();
95:
96:
97: if (isset($config->application->defaultModule)) {
98: $router->setDefaultModule(
99: $config->application->defaultModule
100: );
101: }
102:
103:
104: $router->removeExtraSlashes(true);
105:
106:
107: if (isset($config->application->routers)) {
108: foreach ($config->application->routers as $info) {
109: include_once get_template_directory() . '/' . $info->path;
110: $group = new $info->className;
111: $router->mount($group);
112: }
113: }
114:
115: return $router;
116: });
117:
118:
119: $di->setShared('db', function () {
120: return new \Phalcon\Db\Adapter\Pdo\Mysql(
121: array(
122: 'host' => DB_HOST,
123: 'username' => DB_USER,
124: 'password' => DB_PASSWORD,
125: 'dbname' => DB_NAME
126: )
127: );
128: });
129:
130:
131: $di->setShared('dispatcher', function () use ($di, $config) {
132:
133:
134: $customDispatcher = new \Akufen\Orchestra\Services\Dispatcher();
135: $eventsManager = $di->getEventsManager();
136: $eventsManager->attach('dispatch', $customDispatcher);
137:
138:
139: $dispatcher = new \Phalcon\Mvc\Dispatcher();
140: $dispatcher->setDefaultNamespace($config->application->defaultNamespace);
141: $dispatcher->setEventsManager($eventsManager);
142:
143: return $dispatcher;
144: });
145:
146:
147: $di->set('modules', function () use ($config) {
148:
149:
150: $modules = array();
151:
152: foreach ($config->modules->toArray() as $module) {
153:
154: $modulePath = substr($module['path'], 0, 1) == '/'?
155: get_template_directory() : get_template_directory() . '/vendor/';
156:
157:
158: $modulePath .= $module['path'];
159:
160:
161: $modules[$module['name']] = array(
162: 'className' => ucfirst($module['name']). '\\Module',
163: 'path' => $modulePath . '/Module.php'
164: );
165: }
166:
167: return $modules;
168: });
169:
170:
171: $this->setDI($di);
172: $this->registerModules($di->getModules());
173: }
174:
175: 176: 177: 178: 179: 180:
181: public function handle($uri = null)
182: {
183:
184: $_GET['_url'] = ($uri)? $uri : $this->di->getRequest()->getUri();
185:
186:
187: echo parent::handle()->getContent();
188: }
189: }
190: