1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application\loader; |
4
|
|
|
|
5
|
|
|
use suda\framework\Config; |
6
|
|
|
use suda\application\Module; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* 应用程序 |
10
|
|
|
*/ |
11
|
|
|
class ApplicationLoader extends ApplicationBaseLoader |
12
|
|
|
{ |
13
|
|
|
|
14
|
|
|
const CACHE_ROUTE = 'application-route'; |
15
|
|
|
const CACHE_ROUTE_RUNNABLE = 'application-route-runnable'; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 加载路由 |
19
|
|
|
*/ |
20
|
|
|
public function loadRoute() |
21
|
|
|
{ |
22
|
|
|
$name = 'application-route'; |
23
|
|
|
$this->application->debug()->time($name); |
24
|
|
|
if (static::isDebug()) { |
25
|
|
|
$this->loadRouteFromModules(); |
26
|
|
|
if ($this->application->getRoute()->isContainClosure()) { |
27
|
|
|
$this->application->debug()->warning('route contain closure, route prepare cannot be cacheables'); |
28
|
|
|
} else { |
29
|
|
|
$this->application->cache()->set(ApplicationLoader::CACHE_ROUTE, $this->application->getRoute()->getRouteCollection()); |
30
|
|
|
$this->application->cache()->set(ApplicationLoader::CACHE_ROUTE_RUNNABLE, $this->application->getRoute()->getRunnable()); |
31
|
|
|
} |
32
|
|
|
} elseif ($this->routeCacheAvailable()) { |
33
|
|
|
$route = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE); |
34
|
|
|
$runnable = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE_RUNNABLE); |
35
|
|
|
$this->application->getRoute()->setRouteCollection($route); |
36
|
|
|
$this->application->getRoute()->setRunnable($runnable); |
37
|
|
|
$this->application->debug()->info('load route from cache'); |
38
|
|
|
} else { |
39
|
|
|
$this->loadRouteFromModules(); |
40
|
|
|
} |
41
|
|
|
$this->application->debug()->timeEnd($name); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* 从模块中加载路由 |
46
|
|
|
*/ |
47
|
|
|
private function loadRouteFromModules() |
48
|
|
|
{ |
49
|
|
|
foreach ($this->application->getModules() as $name => $module) { |
50
|
|
|
if ($module->getStatus() === Module::REACHABLE) { |
51
|
|
|
$this->loadModuleRoute($module); |
52
|
|
|
$this->application->debug()->debug('reachable # ' . $module->getFullName()); |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @return bool |
59
|
|
|
*/ |
60
|
|
|
private function routeCacheAvailable() |
61
|
|
|
{ |
62
|
|
|
return $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE) |
63
|
|
|
&& $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE_RUNNABLE); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* 加载路由 |
68
|
|
|
* @param Module $module |
69
|
|
|
*/ |
70
|
|
|
protected function loadModuleRoute(Module $module) |
71
|
|
|
{ |
72
|
|
|
foreach ($this->application->getRouteGroup() as $group) { |
|
|
|
|
73
|
|
|
$this->loadRouteGroup($module, $group); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* 加载路由组 |
79
|
|
|
* |
80
|
|
|
* @param Module $module |
81
|
|
|
* @param string $groupName |
82
|
|
|
* @return void |
83
|
|
|
*/ |
84
|
|
|
protected function loadRouteGroup(Module $module, string $groupName) |
85
|
|
|
{ |
86
|
|
|
$group = $groupName === 'default' ? '' : '-' . $groupName; |
87
|
|
|
if ($path = $module->getResource()->getConfigResourcePath('config/route' . $group)) { |
88
|
|
|
$routeConfig = Config::loadConfig($path, [ |
89
|
|
|
'module' => $module->getName(), |
90
|
|
|
'group' => $groupName, |
91
|
|
|
'property' => $module->getProperty(), |
92
|
|
|
'config' => $module->getConfig(), |
93
|
|
|
]); |
94
|
|
|
if ($routeConfig !== null) { |
95
|
|
|
$prefix = $module->getConfig('route-prefix.' . $groupName, ''); |
96
|
|
|
$this->loadRouteConfig($module, $prefix, $groupName, $routeConfig); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* 加载模块路由配置 |
103
|
|
|
* |
104
|
|
|
* @param Module $module |
105
|
|
|
* @param string $prefix |
106
|
|
|
* @param string $groupName |
107
|
|
|
* @param array $routeConfig |
108
|
|
|
* @return void |
109
|
|
|
*/ |
110
|
|
|
protected function loadRouteConfig(Module $module, string $prefix, string $groupName, array $routeConfig) |
111
|
|
|
{ |
112
|
|
|
$module = $module->getFullName(); |
113
|
|
|
foreach ($routeConfig as $name => $config) { |
114
|
|
|
$exname = $this->application->getRouteName($name, $module, $groupName); |
|
|
|
|
115
|
|
|
$method = $config['method'] ?? []; |
116
|
|
|
$attributes = []; |
117
|
|
|
$attributes['module'] = $module; |
118
|
|
|
$attributes['config'] = $config; |
119
|
|
|
$attributes['group'] = $groupName; |
120
|
|
|
$attributes['route'] = $exname; |
121
|
|
|
$uri = $config['uri'] ?? '/'; |
122
|
|
|
$anti = array_key_exists('anti-prefix', $config) && $config['anti-prefix']; |
123
|
|
|
if ($anti) { |
124
|
|
|
$uri = '/' . trim($uri, '/'); |
125
|
|
|
} else { |
126
|
|
|
$uri = '/' . trim($prefix . $uri, '/'); |
127
|
|
|
} |
128
|
|
|
$this->application->request($method, $exname, $uri, $attributes); |
|
|
|
|
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.