This repository has been archived by the owner. It is now read-only.
forked from ifacesoft/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIce.php
345 lines (290 loc) · 8.04 KB
/
Ice.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
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
<?php
/**
* @link http://www.iceframework.net/
* @copyright Copyright (c) 2014 Ifacesoft
* @license http://www.iceframework.net/license/
*/
namespace ice;
use ice\core\action\Front;
use ice\core\action\Front_Ajax;
use ice\core\action\Front_Cli;
use ice\core\Config;
use ice\core\Loader;
use ice\core\Logger;
use ice\core\Request;
use ice\core\View;
use ice\helper\File;
/**
* Class of Ice application
*
* @link http://iceframework.net Ice home page @endlink
*
* @package ice
* @author dp <[email protected]>
* @since -0
*/
class Ice
{
const INDEX = 'app.php';
//! string Framework name
const ENGINE = 'Ice';
//! string Framework version
const VERSION = '-0';
// @var Ice Instance of ice application
private static $_ice = null;
/**
* Config of ice application
*
* @var Config
*/
private static $_config = null;
private static $_environment = null;
private static $_modules = null;
/**
* Main module name
*
* @var string
*/
private static $_project = null;
/**
* Main module path
*
* @var string
*/
private static $_projectPath = null;
/**
* Root path of modules (includes Ice and other modules)
*
* @todo check. May be is deprecated (modules can be placed in other pathes)
* @var string
*/
private static $_rootPath = null;
/**
* Ice module path
*
* @var string
*/
private static $_enginePath = null;
/**
* Result view of application
*
* @var View
*/
private $view = null;
/**
* Private constructor of ice application
*
* @param $project
*/
private function __construct($project)
{
self::$_project = $project;
$this->bootstrap();
}
/**
* bootstrap method
*
* - init environment variables
* - includes required files
* - register autoloader of classes
* - init logger
*/
private function bootstrap()
{
setlocale(LC_ALL, 'ru_RU.UTF-8');
setlocale(LC_NUMERIC, 'C');
date_default_timezone_set('UTC');
require_once $this->getEnginePath() . 'Exception.php';
require_once $this->getEnginePath() . 'Helper/Object.php';
require_once $this->getEnginePath() . 'Helper/Dir.php';
require_once $this->getEnginePath() . 'Helper/File.php';
require_once $this->getEnginePath() . 'Core/Config.php';
require_once $this->getEnginePath() . 'Core/Data/Provider.php';
require_once $this->getEnginePath() . 'Data/Provider/Registry.php';
require_once $this->getEnginePath() . 'Core/Loader.php';
require_once $this->getEnginePath() . 'Core/Request.php';
Loader::register('ice\core\Loader::load');
Logger::init(Ice::getEnvironment()->get('debug'));
}
/**
* Return engine path
*
* @return string
*/
public static function getEnginePath()
{
if (self::$_enginePath !== null) {
return self::$_enginePath;
}
self::$_enginePath = __DIR__ . '/';
return self::$_enginePath;
}
/**
* @return Config
* @throws Exception
*/
public static function getEnvironment()
{
if (self::$_environment !== null) {
return self::$_environment;
}
self::$_environment = new Config(self::getConfig()->gets('environment'), 'Environment');
return self::$_environment;
}
/**
* Return main config of ice application
*
* @throws Exception
* @return Config
*/
public static function getConfig()
{
if (self::$_config !== null) {
return self::$_config;
}
$configFileCache = Ice::getProjectPath() . Ice::getProject() . '.conf.cache.php';
//
// if (file_exists($configFileCache)) {
// self::$_config = new Config(include $configFileCache, __CLASS__);
//
// return self::$_config;
// }
$_configFile = Ice::getProjectPath() . Ice::getProject() . '.conf.php';
$config = file_exists($_configFile)
? include $_configFile
: include Ice::getEnginePath() . self::ENGINE . '.conf.php';
$_config = [];
foreach ($config['modules'] as $moduleName => $modulePath) {
$configFile = $modulePath . $moduleName . '.conf.php';
if (!file_exists($configFile)) {
die('Could not found module config by path "' . $configFile . '" in ' . $_configFile);
}
$_config = array_merge_recursive($_config, include $configFile);
}
$host = Request::host();
foreach ($_config['hosts'] as $pattern => $environment) {
$matches = [];
preg_match($pattern, $host, $matches);
if (!empty($matches)) {
$_config['host'] = $environment;
break;
}
}
unset($_config['hosts']);
foreach ($_config['environments'] as $environment => $settings) {
$_config['environment'] = array_merge_recursive($settings, $_config['environment']);
if ($environment == $_config['host']) {
break;
}
}
unset($_config['environments']);
File::createData($configFileCache, $_config);
self::$_config = new Config($_config, __CLASS__);
return self::$_config;
}
/**
* Return path of main module
*
* @return string
*/
public static function getProjectPath()
{
if (self::$_projectPath !== null) {
return self::$_projectPath;
}
self::$_projectPath = self::getRootPath() . self::getProject() . '/';
return self::$_projectPath;
}
/**
* Return root path of all modules
*
* @todo check. May be is deprecated (modules can be placed in other pathes)
* @return string
*/
public static function getRootPath()
{
if (self::$_rootPath !== null) {
return self::$_rootPath;
}
self::$_rootPath = dirname(self::getEnginePath()) . '/';
return self::$_rootPath;
}
/**
* Return main module name
*
* @return string
*/
public static function getProject()
{
return self::$_project;
}
/**
* Return instance of Ice application
*
* @param $project
* @return Ice
*/
public static function get($project)
{
if (self::$_ice !== null) {
return self::$_ice;
}
self::$_ice = new Ice($project);
return self::$_ice;
}
/**
* @return array
* @throws Exception
*/
public static function getModules()
{
if (self::$_modules !== null) {
return self::$_modules;
}
self::$_modules = [];
foreach (self::getConfig()->gets('modules') as $moduleName => $modulePath) {
self::$_modules[$moduleName] = is_array($modulePath)
? reset($modulePath)
: $modulePath;
}
return self::$_modules;
}
/**
* Run executing actions
*
* Hierarhical call of actions
*
* @return Ice
*/
public function run()
{
try {
if (!empty($_SERVER['argv'])) {
$this->view = Front_Cli::call();
return $this;
}
if (isset($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
$this->view = Front_Ajax::call();
return $this;
}
Logger::initFb();
$this->view = Front::call();
} catch (\Exception $e) {
Logger::output(Logger::getMessageView($e));
}
return $this;
}
/**
* Flushing ice application
*
* Display rendered view and close opened descriptors and resources
*
*/
public function flush()
{
if (!($this->view instanceof View)) {
die($this->view);
}
$this->view->display();
}
}