1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Router |
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 Router { |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Find class by name |
15
|
|
|
* |
16
|
|
|
* @param string $className |
17
|
|
|
* @return boolean |
18
|
|
|
*/ |
19
|
|
|
public static function findClass($className) { |
20
|
|
|
if (strpos($className, '\\')) { |
21
|
|
|
$classPath = explode('\\', $className); |
22
|
|
|
$moduleName = $classPath[0]; |
23
|
|
|
$result = Router::loadClass($className); |
24
|
|
|
if ($result && App::$cur) { |
25
|
|
|
if (!App::$cur->isLoaded($moduleName)) { |
26
|
|
|
App::$cur->loadObject($moduleName); |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
return $result; |
30
|
|
|
} else { |
31
|
|
|
return Router::loadClass($className); |
32
|
|
|
} |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Include class by name |
37
|
|
|
* |
38
|
|
|
* @param string $className |
39
|
|
|
* @return boolean |
40
|
|
|
*/ |
41
|
|
|
public static function loadClass($className) { |
42
|
|
|
$folders = []; |
43
|
|
|
if (strpos($className, '\\')) { |
44
|
|
|
$classPath = explode('\\', $className); |
45
|
|
|
$moduleName = $classPath[0]; |
46
|
|
|
if (Module::installed($moduleName, App::$cur)) { |
47
|
|
|
$classPath = implode('/', array_slice($classPath, 1)); |
48
|
|
|
if (App::$cur) { |
49
|
|
|
if (App::$cur !== App::$primary) { |
50
|
|
|
$folders['appModule'] = ['folder' => App::$primary->path . '/modules/' . $moduleName, 'classPath' => $classPath]; |
51
|
|
|
} |
52
|
|
|
$folders['primaryAppModule'] = ['folder' => App::$cur->path . '/modules/' . $moduleName, 'classPath' => $classPath]; |
53
|
|
|
} |
54
|
|
|
$folders['systemModule'] = ['folder' => INJI_SYSTEM_DIR . '/modules/' . $moduleName, 'classPath' => $classPath]; |
55
|
|
|
} |
56
|
|
|
} |
57
|
|
|
$classPath = str_replace('\\', '/', $className); |
58
|
|
|
|
59
|
|
|
if (App::$cur) { |
60
|
|
|
if (App::$cur !== App::$primary) { |
61
|
|
|
$folders['primaryApp'] = ['folder' => App::$primary->path, 'classPath' => $classPath]; |
62
|
|
|
} |
63
|
|
|
$folders['app'] = ['folder' => App::$cur->path, 'classPath' => $classPath]; |
64
|
|
|
} |
65
|
|
|
$folders['system'] = ['folder' => INJI_SYSTEM_DIR, 'classPath' => $classPath]; |
66
|
|
|
$paths = []; |
67
|
|
|
foreach ($folders as $code => $folderParams) { |
68
|
|
|
$paths = $paths + static::genFolderPaths($code, $folderParams['folder'], $folderParams['classPath']); |
69
|
|
|
} |
70
|
|
|
foreach ($paths as $path) { |
71
|
|
|
if (file_exists($path)) { |
72
|
|
|
include_once $path; |
73
|
|
|
if (in_array('Model', class_parents($className, false)) && \App::$primary) { |
74
|
|
|
$filename = Cache::getDir('system') . '/classData.php'; |
75
|
|
|
$classData = Config::custom($filename); |
76
|
|
|
if (empty($classData['Model'][$className]['tableCreated'])) { |
77
|
|
|
$classData['Model'][$className]['tableCreated'] = $className::createTable(); |
78
|
|
|
Config::save($filename, $classData); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
return true; |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
return false; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return posible paths for class path |
89
|
|
|
* |
90
|
|
|
* @param string $code |
91
|
|
|
* @param string $folder |
92
|
|
|
* @param string $classPath |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
|
|
public static function genFolderPaths($code, $folder, $classPath) { |
96
|
|
|
$paths = []; |
97
|
|
|
if (strpos($classPath, '/') === false) { |
98
|
|
|
$paths[$code . '_Object'] = $folder . '/objects/' . $classPath . '.php'; |
99
|
|
|
$paths[$code . '_ObjectDir'] = $folder . '/objects/' . $classPath . '/' . $classPath . '.php'; |
100
|
|
|
$paths[$code . '_Model'] = $folder . '/models/' . $classPath . '.php'; |
101
|
|
|
$paths[$code . '_ModelDir'] = $folder . '/models/' . $classPath . '/' . $classPath . '.php'; |
102
|
|
|
} else { |
103
|
|
|
$classFile = substr($classPath, strrpos($classPath, '/') + 1); |
104
|
|
|
$classPathWithotClass = substr($classPath, 0, strrpos($classPath, '/')); |
105
|
|
|
|
106
|
|
|
$paths[$code . '_Object'] = $folder . '/objects/' . $classPathWithotClass . '/' . $classFile . '.php'; |
107
|
|
|
$paths[$code . '_ObjectDir'] = $folder . '/objects/' . $classPath . '/' . $classFile . '.php'; |
108
|
|
|
$paths[$code . '_Model'] = $folder . '/models/' . $classPathWithotClass . '/' . $classFile . '.php'; |
109
|
|
|
$paths[$code . '_ModelDir'] = $folder . '/models/' . $classPath . '/' . $classFile . '.php'; |
110
|
|
|
} |
111
|
|
|
return $paths; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* Return dir for class name |
116
|
|
|
* |
117
|
|
|
* @param string $className |
118
|
|
|
* @return string |
119
|
|
|
*/ |
120
|
|
|
public static function getLoadedClassPath($className) { |
121
|
|
|
$rc = new ReflectionClass($className); |
122
|
|
|
return dirname($rc->getFileName()); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public static function resolvePath($path) { |
126
|
|
|
$params = Tools::uriParse($path); |
127
|
|
|
if ($params[0] == App::$cur->name) { |
128
|
|
|
$app = App::$cur; |
129
|
|
|
$params = array_slice($params, 1); |
130
|
|
|
} else { |
131
|
|
|
$app = App::$primary; |
132
|
|
|
} |
133
|
|
|
$module = Module::resolveModule($app, $params); |
134
|
|
|
if (!$module) { |
135
|
|
|
return false; |
136
|
|
|
} |
137
|
|
|
$controller = $module->findController(); |
138
|
|
|
if (!$controller) { |
139
|
|
|
return false; |
140
|
|
|
} |
141
|
|
|
$controller->resolveMethod(); |
142
|
|
|
return compact('module', 'controller', 'params'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
} |
146
|
|
|
|