1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application\loader; |
4
|
|
|
|
5
|
|
|
use suda\application\Module; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* 应用程序 |
9
|
|
|
*/ |
10
|
|
|
class ApplicationLoader extends ApplicationBaseLoader |
11
|
|
|
{ |
12
|
|
|
|
13
|
|
|
const CACHE_ROUTE = 'application-route'; |
14
|
|
|
const CACHE_ROUTE_RUNNABLE = 'application-route-runnable'; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* 加载路由 |
18
|
|
|
*/ |
19
|
|
|
public function loadRoute() |
20
|
|
|
{ |
21
|
|
|
$name = 'application-route'; |
22
|
|
|
$this->application->debug()->time($name); |
23
|
|
|
if (static::isDebug()) { |
24
|
|
|
$this->loadRouteFromModules(); |
25
|
|
|
if ($this->application->getRoute()->isContainClosure()) { |
26
|
|
|
$this->application->debug()->warning('route contain closure, route prepare cannot be cacheables'); |
27
|
|
|
} else { |
28
|
|
|
$this->application->cache()->set(ApplicationLoader::CACHE_ROUTE, $this->application->getRoute()->getRouteCollection()); |
29
|
|
|
$this->application->cache()->set(ApplicationLoader::CACHE_ROUTE_RUNNABLE, $this->application->getRoute()->getRunnable()); |
30
|
|
|
} |
31
|
|
|
} elseif ($this->routeCacheAvailable()) { |
32
|
|
|
$route = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE); |
33
|
|
|
$runnable = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE_RUNNABLE); |
34
|
|
|
$this->application->getRoute()->setRouteCollection($route); |
35
|
|
|
$this->application->getRoute()->setRunnable($runnable); |
36
|
|
|
$this->application->debug()->info('load route from cache'); |
37
|
|
|
} else { |
38
|
|
|
$this->loadRouteFromModules(); |
39
|
|
|
} |
40
|
|
|
$this->application->debug()->timeEnd($name); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* 从模块中加载路由 |
45
|
|
|
*/ |
46
|
|
|
private function loadRouteFromModules() |
47
|
|
|
{ |
48
|
|
|
foreach ($this->application->getModules() as $name => $module) { |
49
|
|
|
if ($module->getStatus() === Module::REACHABLE) { |
50
|
|
|
call_user_func([$this->moduleLoader[$name], 'toReachable']); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return bool |
57
|
|
|
*/ |
58
|
|
|
private function routeCacheAvailable() |
59
|
|
|
{ |
60
|
|
|
return $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE) |
61
|
|
|
&& $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE_RUNNABLE); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
} |
65
|
|
|
|