1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace suda\application\loader; |
4
|
|
|
|
5
|
|
|
use suda\application\Module; |
6
|
|
|
use suda\framework\loader\Loader; |
7
|
|
|
use suda\application\database\Database; |
8
|
|
|
use suda\database\exception\SQLException; |
9
|
|
|
use suda\framework\filesystem\FileSystem; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* 应用程序 |
13
|
|
|
*/ |
14
|
|
|
class ApplicationLoader extends ApplicationModuleLoader |
15
|
|
|
{ |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* 加载额外vendor |
19
|
|
|
*/ |
20
|
|
|
public function loadVendorIfExist() |
21
|
|
|
{ |
22
|
|
|
$vendorAutoload = $this->application->getPath() . '/vendor/autoload.php'; |
23
|
|
|
if (FileSystem::exist($vendorAutoload)) { |
24
|
|
|
Loader::requireOnce($vendorAutoload); |
25
|
|
|
} |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 加载APP |
30
|
|
|
*/ |
31
|
|
|
public function load() |
32
|
|
|
{ |
33
|
|
|
$this->loadVendorIfExist(); |
34
|
|
|
$this->loadGlobalConfig(); |
35
|
|
|
$this->loadModule(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* 加载全局配置 |
40
|
|
|
*/ |
41
|
|
|
public function loadGlobalConfig() |
42
|
|
|
{ |
43
|
|
|
$resource = $this->application->getResource(); |
44
|
|
|
if ($configPath = $resource->getConfigResourcePath('config/config')) { |
45
|
|
|
$this->application->getConfig()->load($configPath); |
46
|
|
|
} |
47
|
|
|
if ($listenerPath = $resource->getConfigResourcePath('config/listener')) { |
48
|
|
|
$this->application->loadEvent($listenerPath); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* 加载路由 |
54
|
|
|
*/ |
55
|
|
|
public function loadRoute() |
56
|
|
|
{ |
57
|
|
|
$name = 'application-route'; |
58
|
|
|
$this->application->debug()->time($name); |
59
|
|
|
if ($this->enableRouteCache() === false) { |
60
|
|
|
$this->loadRouteFromModules(); |
61
|
|
|
} elseif ($this->application->cache()->has($name.'-route')) { |
62
|
|
|
$route = $this->application->cache()->get($name.'-route'); |
63
|
|
|
$runnable = $this->application->cache()->get($name.'-runnable'); |
64
|
|
|
$this->application->getRoute()->setRouteCollection($route); |
65
|
|
|
$this->application->getRoute()->setRunnable($runnable); |
66
|
|
|
$this->application->debug()->info('load route from cache'); |
67
|
|
|
} else { |
68
|
|
|
$this->loadRouteFromModules(); |
69
|
|
|
if ($this->application->getRoute()->isContainClosure()) { |
70
|
|
|
$this->application->debug()->warning('route contain closure, route prepare cannot be cacheables'); |
71
|
|
|
} else { |
72
|
|
|
$this->application->cache()->set($name.'-route', $this->application->getRoute()->getRouteCollection()); |
73
|
|
|
$this->application->cache()->set($name.'-runnable', $this->application->getRoute()->getRunnable()); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
$this->application->debug()->timeEnd($name); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* 从模块中加载路由 |
81
|
|
|
*/ |
82
|
|
|
private function loadRouteFromModules() |
83
|
|
|
{ |
84
|
|
|
foreach ($this->application->getModules() as $name => $module) { |
85
|
|
|
if ($module->getStatus() === Module::REACHABLE) { |
86
|
|
|
call_user_func([$this->moduleLoader[$name], 'toReachable']); |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
private function enableRouteCache() { |
95
|
|
|
return boolval($this->application->getCache()->get('route-cache', static::isDebug())); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* 加载数据源 |
100
|
|
|
* |
101
|
|
|
* @throws SQLException |
102
|
|
|
*/ |
103
|
|
|
public function loadDataSource() |
104
|
|
|
{ |
105
|
|
|
Database::loadApplication($this->application); |
106
|
|
|
$dataSource = Database::getDefaultDataSource(); |
107
|
|
|
$this->application->setDataSource($dataSource); |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|