1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Inji; |
4
|
|
|
|
5
|
|
|
use Inji\Router\Folder; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Router |
9
|
|
|
* |
10
|
|
|
* @author Alexey Krupskiy <[email protected]> |
11
|
|
|
* @link http://inji.ru/ |
12
|
|
|
* @copyright 2015 Alexey Krupskiy |
13
|
|
|
* @license https://github.com/injitools/cms-Inji/blob/master/LICENSE |
14
|
|
|
*/ |
15
|
|
|
class Router { |
16
|
|
|
/** |
17
|
|
|
* @var Folder[] |
18
|
|
|
*/ |
19
|
|
|
public static $folders = []; |
20
|
|
|
public static $touchedModules = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param string $folder |
24
|
|
|
* @param string $prefix |
25
|
|
|
* @param int|float $priority |
26
|
|
|
* @param int|bool $moduleIndex |
27
|
|
|
* @param array $moduleDirs |
28
|
|
|
*/ |
29
|
|
|
public static function addPath(string $folder, $prefix = '*', $priority = 0, $moduleIndex = false, $moduleDirs = []): void { |
30
|
|
|
self::$folders[] = new Folder($folder, $prefix, $priority, $moduleIndex, $moduleDirs); |
31
|
|
|
usort(self::$folders, function ($a, $b) { |
32
|
|
|
return $b->priority <=> $a->priority; |
33
|
|
|
}); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Find class by name |
38
|
|
|
* |
39
|
|
|
* @param string $className |
40
|
|
|
* @return bool |
41
|
|
|
*/ |
42
|
2 |
|
public static function findClass(string $className): bool { |
43
|
2 |
|
foreach (self::$folders as $folder) { |
44
|
2 |
|
$paths = $folder->forClass($className); |
45
|
2 |
|
foreach ($paths as $path) { |
46
|
2 |
|
if (file_exists($path->path)) { |
47
|
2 |
|
self::loadClass($path->path); |
48
|
2 |
|
if ($path->moduleName && !isset(static::$touchedModules[$path->moduleName])) { |
49
|
1 |
|
static::$touchedModules[$path->moduleName] = true; |
50
|
2 |
|
App::$cur->{$path->moduleName}; |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
2 |
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Include class by name |
60
|
|
|
* |
61
|
|
|
* @param string $className |
62
|
|
|
* @return boolean |
63
|
|
|
*/ |
64
|
2 |
|
public static function loadClass($classPath) { |
65
|
2 |
|
include_once $classPath; |
66
|
2 |
|
return true; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Return dir for class name |
72
|
|
|
* |
73
|
|
|
* @param string $className |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
1 |
|
public static function getLoadedClassPath($className) { |
77
|
1 |
|
$rc = new \ReflectionClass($className); |
78
|
1 |
|
return dirname($rc->getFileName()); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public static function resolvePath($path) { |
82
|
|
|
$params = Tools::uriParse($path); |
83
|
|
|
if ($params[0] == App::$cur->name) { |
84
|
|
|
$app = App::$cur; |
85
|
|
|
$params = array_slice($params, 1); |
86
|
|
|
} else { |
87
|
|
|
$app = App::$primary; |
88
|
|
|
} |
89
|
|
|
$module = Module::resolveModule($app, $params); |
90
|
|
|
if (!$module) { |
91
|
|
|
return false; |
92
|
|
|
} |
93
|
|
|
$controller = $module->findController(); |
94
|
|
|
if (!$controller) { |
95
|
|
|
return false; |
96
|
|
|
} |
97
|
|
|
$controller->resolveMethod(); |
98
|
|
|
return compact('module', 'controller', 'params'); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
} |
102
|
|
|
|