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