1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inji; |
4
|
|
|
/** |
5
|
|
|
* App |
6
|
|
|
* |
7
|
|
|
* @author Alexey Krupskiy <[email protected]> |
8
|
|
|
* @link http://inji.ru/ |
9
|
|
|
* @copyright 2015 Alexey Krupskiy |
10
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
11
|
|
|
*/ |
12
|
|
|
class App { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* static instance |
16
|
|
|
* |
17
|
|
|
* @var App |
18
|
|
|
*/ |
19
|
|
|
public static $cur = null; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var App |
23
|
|
|
*/ |
24
|
|
|
public static $primary = null; |
25
|
|
|
private $_objects = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* App params |
29
|
|
|
*/ |
30
|
|
|
public $name = ''; |
31
|
|
|
public $dir = ''; |
32
|
|
|
public $namespace = ''; |
33
|
|
|
public $type = 'app'; |
34
|
|
|
public $system = false; |
35
|
|
|
public $default = false; |
36
|
|
|
public $route = ''; |
37
|
|
|
public $installed = false; |
38
|
|
|
public $staticPath = '/static'; |
39
|
|
|
public $templatesPath = '/static/templates'; |
40
|
|
|
public $path = ''; |
41
|
|
|
public $params = []; |
42
|
|
|
public $config = []; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Constructor App |
46
|
|
|
* |
47
|
|
|
* @param array $preSet |
48
|
|
|
*/ |
49
|
|
|
public function __construct($preSet = []) { |
50
|
|
|
foreach ($preSet as $key => $value) { |
51
|
|
|
$this->{$key} = $value; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Return module object by name or alias |
57
|
|
|
* |
58
|
|
|
* @param string $className |
59
|
|
|
* @return Module |
60
|
|
|
*/ |
61
|
|
|
public function getObject($className, $params = []) { |
62
|
|
|
$paramsStr = serialize($params); |
63
|
|
|
$className = ucfirst($className); |
64
|
|
|
if (isset($this->_objects[$className][$paramsStr])) { |
65
|
|
|
return $this->_objects[$className][$paramsStr]; |
66
|
|
|
} |
67
|
|
|
return $this->loadObject($className, $params); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Find module class from each paths |
72
|
|
|
* |
73
|
|
|
* @param string $moduleName |
74
|
|
|
* @return mixed |
75
|
|
|
*/ |
76
|
|
|
public function findModuleClass($moduleName) { |
77
|
|
|
$possibleModules = $this->possibleModuleClasses($moduleName); |
78
|
|
|
foreach ($possibleModules as $possibleModule) { |
79
|
|
|
if (class_exists($possibleModule)) { |
80
|
|
|
return $possibleModule; |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
if (!empty($this->config['moduleRouter'])) { |
84
|
|
View Code Duplication |
foreach ($this->config['moduleRouter'] as $route => $module) { |
|
|
|
|
85
|
|
|
if (preg_match("!{$route}!i", $moduleName)) { |
86
|
|
|
$possibleModules = $this->possibleModuleClasses($module); |
87
|
|
|
foreach ($possibleModules as $possibleModule) { |
88
|
|
|
if (class_exists($possibleModule)) { |
89
|
|
|
return $possibleModule; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
if (!empty(\Inji::$config['moduleRouter'])) { |
96
|
|
View Code Duplication |
foreach (\Inji::$config['moduleRouter'] as $route => $module) { |
|
|
|
|
97
|
|
|
if (preg_match("!{$route}!i", $moduleName)) { |
98
|
|
|
$possibleModules = $this->possibleModuleClasses($module); |
99
|
|
|
foreach ($possibleModules as $possibleModule) { |
100
|
|
|
if (class_exists($possibleModule)) { |
101
|
|
|
return $possibleModule; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
return false; |
108
|
|
|
} |
109
|
|
|
public function possibleModuleClasses($moduleName){ |
110
|
|
|
$possibleModules = []; |
111
|
|
|
if ($this->namespace) { |
112
|
|
|
$possibleModules[] = $this->namespace . '\\' . $moduleName; |
113
|
|
|
} |
114
|
|
|
$possibleModules[] = "Inji\\{$moduleName}"; |
115
|
|
|
return $possibleModules; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
public function isLoaded($moduleName) { |
119
|
|
|
return !empty($this->_objects[$moduleName]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function getDomain($decode = false) { |
123
|
|
|
return !empty($this->config['site']['domain']) ? $this->config['site']['domain'] : ($decode ? idn_to_utf8(INJI_DOMAIN_NAME) : INJI_DOMAIN_NAME); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Load module by name or alias |
128
|
|
|
* |
129
|
|
|
* @param string $className |
130
|
|
|
* @return mixed |
131
|
|
|
*/ |
132
|
|
|
public function loadObject($className, $params = []) { |
133
|
|
|
$paramsStr = serialize($params); |
134
|
|
|
$moduleClassName = $this->findModuleClass($className); |
135
|
|
|
if ($moduleClassName === false) { |
136
|
|
|
return false; |
137
|
|
|
} |
138
|
|
|
$this->_objects[$className][$paramsStr] = new $moduleClassName($this); |
139
|
|
|
if (isset($this->_objects[$className][$paramsStr])) { |
140
|
|
|
$this->_objects[$className][$paramsStr]->checkDbMigration(); |
141
|
|
|
if (method_exists($this->_objects[$className][$paramsStr], 'init')) { |
142
|
|
|
call_user_func_array([$this->_objects[$className][$paramsStr], 'init'], $params); |
143
|
|
|
} |
144
|
|
|
return $this->_objects[$className][$paramsStr]; |
145
|
|
|
} |
146
|
|
|
return false; |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* Reference to module getter |
151
|
|
|
* |
152
|
|
|
* @param string $className |
153
|
|
|
* @return Module|null |
154
|
|
|
*/ |
155
|
|
|
public function __get($className) { |
156
|
|
|
return $this->getObject($className); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* Reference to module getter with params |
161
|
|
|
* |
162
|
|
|
* @param string $className |
163
|
|
|
* @param array $params |
164
|
|
|
* @return Module|null |
165
|
|
|
*/ |
166
|
|
|
public function __call($className, $params) { |
167
|
|
|
return $this->getObject($className, $params); |
168
|
|
|
} |
169
|
|
|
} |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.