1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* App |
5
|
|
|
* |
6
|
|
|
* @author Alexey Krupskiy <[email protected]> |
7
|
|
|
* @link http://inji.ru/ |
8
|
|
|
* @copyright 2015 Alexey Krupskiy |
9
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
10
|
|
|
*/ |
11
|
|
|
class App { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* static instance |
15
|
|
|
* |
16
|
|
|
* @var App |
17
|
|
|
*/ |
18
|
|
|
public static $cur = null; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var App |
22
|
|
|
*/ |
23
|
|
|
public static $primary = null; |
24
|
|
|
private $_objects = []; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* App params |
28
|
|
|
*/ |
29
|
|
|
public $name = ''; |
30
|
|
|
public $dir = ''; |
31
|
|
|
public $type = 'app'; |
32
|
|
|
public $system = false; |
33
|
|
|
public $default = false; |
34
|
|
|
public $route = ''; |
35
|
|
|
public $installed = false; |
36
|
|
|
public $staticPath = '/static'; |
37
|
|
|
public $templatesPath = '/static/templates'; |
38
|
|
|
public $path = ''; |
39
|
|
|
public $params = []; |
40
|
|
|
public $config = []; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Constructor App |
44
|
|
|
* |
45
|
|
|
* @param array $preSet |
46
|
|
|
*/ |
47
|
|
|
public function __construct($preSet = []) { |
48
|
|
|
foreach ($preSet as $key => $value) { |
49
|
|
|
$this->{$key} = $value; |
50
|
|
|
} |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Return module object by name or alias |
55
|
|
|
* |
56
|
|
|
* @param string $className |
57
|
|
|
* @return Module |
58
|
|
|
*/ |
59
|
|
|
public function getObject($className, $params = []) { |
60
|
|
|
$paramsStr = serialize($params); |
61
|
|
|
$className = ucfirst($className); |
62
|
|
|
if (isset($this->_objects[$className][$paramsStr])) { |
63
|
|
|
return $this->_objects[$className][$paramsStr]; |
64
|
|
|
} |
65
|
|
|
return $this->loadObject($className, $params); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Find module class from each paths |
70
|
|
|
* |
71
|
|
|
* @param string $moduleName |
72
|
|
|
* @return mixed |
73
|
|
|
*/ |
74
|
|
|
public function findModuleClass($moduleName) { |
75
|
|
|
$paths = Module::getModulePaths($moduleName); |
76
|
|
|
foreach ($paths as $path) { |
77
|
|
|
if (file_exists($path . '/' . $moduleName . '.php')) { |
78
|
|
|
include_once $path . '/' . $moduleName . '.php'; |
79
|
|
|
return $moduleName; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
if (!empty($this->config['moduleRouter'])) { |
83
|
|
|
foreach ($this->config['moduleRouter'] as $route => $module) { |
84
|
|
|
if (preg_match("!{$route}!i", $moduleName)) { |
85
|
|
|
return $module; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
if (!empty(Inji::$config['moduleRouter'])) { |
90
|
|
|
foreach (Inji::$config['moduleRouter'] as $route => $module) { |
91
|
|
|
if (preg_match("!{$route}!i", $moduleName)) { |
92
|
|
|
return $module; |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
return false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function isLoaded($moduleName) { |
100
|
|
|
return !empty($this->_objects[$moduleName]); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
public function getDomain($decode = false) { |
104
|
|
|
return !empty($this->config['site']['domain']) ? $this->config['site']['domain'] : ($decode ? idn_to_utf8(INJI_DOMAIN_NAME) : INJI_DOMAIN_NAME); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Load module by name or alias |
109
|
|
|
* |
110
|
|
|
* @param string $className |
111
|
|
|
* @return mixed |
112
|
|
|
*/ |
113
|
|
|
public function loadObject($className, $params = []) { |
114
|
|
|
$paramsStr = serialize($params); |
115
|
|
|
$moduleClassName = $this->findModuleClass($className); |
116
|
|
|
if (!is_bool($moduleClassName) && $moduleClassName != $className) { |
117
|
|
|
return $this->_objects[$moduleClassName][$paramsStr] = $this->_objects[$className][$paramsStr] = $this->getObject($moduleClassName); |
118
|
|
|
} elseif (Module::installed($className, $this) && class_exists($className)) { |
119
|
|
|
$this->_objects[$className][$paramsStr] = new $className($this); |
120
|
|
|
} |
121
|
|
|
if (isset($this->_objects[$className][$paramsStr])) { |
122
|
|
|
$this->_objects[$className][$paramsStr]->checkDbMigration(); |
123
|
|
|
if (method_exists($this->_objects[$className][$paramsStr], 'init')) { |
124
|
|
|
call_user_func_array([$this->_objects[$className][$paramsStr], 'init'], $params); |
125
|
|
|
} |
126
|
|
|
return $this->_objects[$className][$paramsStr]; |
127
|
|
|
} |
128
|
|
|
return null; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Reference to module getter |
133
|
|
|
* |
134
|
|
|
* @param string $className |
135
|
|
|
* @return Module|null |
136
|
|
|
*/ |
137
|
|
|
public function __get($className) { |
138
|
|
|
return $this->getObject($className); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Reference to module getter with params |
143
|
|
|
* |
144
|
|
|
* @param string $className |
145
|
|
|
* @param array $params |
146
|
|
|
* @return Module|null |
147
|
|
|
*/ |
148
|
|
|
public function __call($className, $params) { |
149
|
|
|
return $this->getObject($className, $params); |
150
|
|
|
} |
151
|
|
|
} |