1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inji; |
4
|
|
|
/** |
5
|
|
|
* Router |
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 Router { |
13
|
|
|
public static $folders = []; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @param string $folder |
17
|
|
|
* @param string $prefix |
18
|
|
|
* @param int|float $priority |
19
|
|
|
* @param int|bool $moduleIndex |
20
|
|
|
* @param array $moduleDirs |
21
|
|
|
*/ |
22
|
|
|
public static function addPath(string $folder, $prefix = '*', $priority = 0, $moduleIndex = false, $moduleDirs = []): void { |
23
|
|
|
self::$folders[] = compact('folder', 'prefix', 'priority', 'moduleIndex', 'moduleDirs'); |
24
|
|
|
usort(self::$folders, function ($a, $b) { |
25
|
|
|
return $b['priority'] <=> $a['priority']; |
26
|
|
|
}); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Find class by name |
31
|
|
|
* |
32
|
|
|
* @param string $className |
33
|
|
|
* @return bool |
34
|
|
|
*/ |
35
|
|
|
public static function findClass(string $className): bool { |
36
|
|
|
foreach (self::$folders as $folder) { |
37
|
|
|
$paths = self::genFolderPaths($folder, $className); |
38
|
|
|
foreach ($paths as $path) { |
39
|
|
|
if (file_exists($path)) { |
40
|
|
|
return self::loadClass($path); |
41
|
|
|
} |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
return false; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Return possible paths for class path |
49
|
|
|
* |
50
|
|
|
* @param array $folder |
51
|
|
|
* @param string $className |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
|
|
public static function genFolderPaths($folder, $className) { |
55
|
|
|
$classPath = str_replace('\\', '/', $className); |
56
|
|
|
$paths = []; |
57
|
|
|
if ($folder['moduleIndex'] !== false && $folder['moduleIndex'] < substr_count($classPath, '/')) { |
58
|
|
|
$classPathItems = explode('/', $classPath); |
59
|
|
|
$moduleName = $classPathItems[$folder['moduleIndex']]; |
|
|
|
|
60
|
|
|
$classPathStart = implode('/', array_slice($classPathItems, 0, $folder['moduleIndex'] + 1)); |
61
|
|
|
if (strpos($classPathStart, str_replace('\\', '/', $folder['prefix'])) === 0) { |
62
|
|
|
$classPathStart = substr($classPathStart, strlen($folder['prefix'])); |
63
|
|
|
} |
64
|
|
|
$classPathEnd = implode('/', array_slice($classPathItems, $folder['moduleIndex'] + 1)); |
65
|
|
|
foreach ($folder['moduleDirs'] as $moduleDir) { |
66
|
|
|
$dirPath = implode('/', [rtrim($folder['folder'], '/'), $classPathStart, $moduleDir, $classPathEnd]); |
67
|
|
|
$paths['folderModuleDirPath_' . $moduleDir] = $dirPath . '.php'; |
68
|
|
|
$paths['folderModuleDirPathDir_' . $moduleDir] = $dirPath . substr($dirPath, strrpos($dirPath, '/')) . '.php'; |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
if (strpos($className, $folder['prefix']) === 0) { |
72
|
|
|
$cuttedPath = substr($classPath, strlen($folder['prefix'])); |
73
|
|
|
$paths['folderPrefixPath'] = $folder['folder'] . $cuttedPath . '.php'; |
74
|
|
|
$paths['folderPrefixPathDir'] = $folder['folder'] . $cuttedPath . substr($classPath, strrpos($classPath, '/')) . '.php'; |
75
|
|
|
} |
76
|
|
|
if ($folder['prefix'] === '*') { |
77
|
|
|
$paths['folderPath'] = $folder['folder'] . $classPath . '.php'; |
78
|
|
|
$paths['folderPathDir'] = $folder['folder'] . $classPath . substr($classPath, strrpos($classPath, '/')) . '.php'; |
79
|
|
|
} |
80
|
|
|
return $paths; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Include class by name |
85
|
|
|
* |
86
|
|
|
* @param string $className |
|
|
|
|
87
|
|
|
* @return boolean |
88
|
|
|
*/ |
89
|
|
|
public static function loadClass($classPath) { |
90
|
|
|
include_once $classPath; |
91
|
|
|
return true; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Return dir for class name |
97
|
|
|
* |
98
|
|
|
* @param string $className |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
public static function getLoadedClassPath($className) { |
102
|
|
|
$rc = new \ReflectionClass($className); |
103
|
|
|
return dirname($rc->getFileName()); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public static function resolvePath($path) { |
107
|
|
|
$params = Tools::uriParse($path); |
108
|
|
|
if ($params[0] == App::$cur->name) { |
109
|
|
|
$app = App::$cur; |
110
|
|
|
$params = array_slice($params, 1); |
111
|
|
|
} else { |
112
|
|
|
$app = App::$primary; |
113
|
|
|
} |
114
|
|
|
$module = Module::resolveModule($app, $params); |
115
|
|
|
if (!$module) { |
116
|
|
|
return false; |
117
|
|
|
} |
118
|
|
|
$controller = $module->findController(); |
119
|
|
|
if (!$controller) { |
120
|
|
|
return false; |
121
|
|
|
} |
122
|
|
|
$controller->resolveMethod(); |
123
|
|
|
return compact('module', 'controller', 'params'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
} |
127
|
|
|
|
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.