This repository has been archived by the owner on Jul 12, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Application.php
176 lines (147 loc) · 4.63 KB
/
Application.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
<?php
/**
* @author: Patsura Dmitry http://github.com/ovr <talk@dmtry.me>
*/
namespace Phalcony;
use Phalcon\Events\Manager as EventsManager;
use Phalcony\Stdlib\Hydrator\ClassMethods;
class Application extends \Phalcon\Mvc\Application
{
const ENV_PRODUCTION = 'production';
const ENV_STAGING = 'staging';
const ENV_TESTING = 'testing';
const ENV_DEVELOPMENT = 'development';
/**
* @var array
*/
protected $configuration;
/**
* @var string
*/
protected $env;
/**
* @param string $env
* @param array $configuration
* @param \Phalcon\DiInterface $di
*/
public function __construct($env, array $configuration, \Phalcon\DiInterface $di = null)
{
$this->env = strtolower($env);
$this->configuration = $configuration;
switch ($this->env) {
case self::ENV_PRODUCTION:
case self::ENV_STAGING:
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);
break;
case self::ENV_TESTING:
case self::ENV_DEVELOPMENT:
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(-1);
break;
default:
ini_set('display_errors', 0);
ini_set('display_startup_errors', 0);
error_reporting(0);
}
if (is_null($di)) {
$di = new \Phalcon\DI\FactoryDefault();
}
parent::__construct($di);
}
/**
* Register loader
*/
protected function registerLoader()
{
$config = &$this->configuration;
$loader = new \Phalcon\Loader();
if (isset($config['application']['registerNamespaces'])) {
$loadNamespaces = $config['application']['registerNamespaces'];
} else {
$loadNamespaces = array();
}
foreach ($config['application']['modules'] as $module => $enabled) {
$moduleName = ucfirst($module);
$loadNamespaces[$moduleName.'\Model'] = APPLICATION_PATH . '/modules/'.$module.'/models/';
$loadNamespaces[$moduleName.'\Service'] = APPLICATION_PATH . '/modules/'.$module.'/services/';
}
if (isset($config['application']['registerDirs'])) {
$loader->registerDirs($config['application']['registerDirs']);
}
$loader->registerNamespaces($loadNamespaces)
->register();
}
/**
* Register di services
*
* @throws \Exception
*/
public function registerServices()
{
$di = $this->getDI();
if (isset($this->configuration['services'])) {
if (!is_array($this->configuration['services'])) {
throw new \Exception('Config[services] must be an array');
}
if (count($this->configuration['services']) > 0) {
foreach ($this->configuration['services'] as $diServiceName => $serviceParameters) {
$class = $serviceParameters['class'];
if (is_callable($class)) {
$service = $class($this);
} else if (gettype($class) != 'object') {
if (isset($serviceParameters['__construct'])) {
$service = new $class($serviceParameters['__construct']);
} else {
$service = new $class();
}
} else {
$service = $class;
}
if (isset($serviceParameters['parameters'])) {
$service = ClassMethods::hydrate($serviceParameters['parameters'], $service);
}
$di->set($diServiceName, $service);
}
}
}
}
/**
* @return $this
* @throws \Exception
*/
public function bootstrap()
{
$this->registerLoader();
$this->registerModules($this->configuration['application']['modules']);
$eventsManager = new EventsManager();
$this->setEventsManager($eventsManager);
$this->registerServices();
return $this;
}
/**
* Run app
*
* @param null $uri
*/
public function run($uri = null)
{
$this->handle($uri)->send();
}
/**
* @return string
*/
public function getEnv()
{
return $this->env;
}
/**
* @return array
*/
public function getConfiguration()
{
return $this->configuration;
}
}