Passed
Push — dev ( dedc6e...cbfb41 )
by 世昌
02:24
created

ApplicationLoader::routeCacheAvailable()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
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
    const CACHE_ROUTE = 'application-route';
18
    const CACHE_ROUTE_RUNNABLE = 'application-route-runnable';
19
20
    /**
21
     * 加载额外vendor
22
     */
23
    public function loadVendorIfExist()
24
    {
25
        $vendorAutoload = $this->application->getPath() . '/vendor/autoload.php';
26
        if (FileSystem::exist($vendorAutoload)) {
27
            Loader::requireOnce($vendorAutoload);
28
        }
29
    }
30
31
    /**
32
     * 加载APP
33
     */
34
    public function load()
35
    {
36
        $this->loadVendorIfExist();
37
        $this->loadGlobalConfig();
38
        $this->loadModule();
39
    }
40
41
    /**
42
     * 加载全局配置
43
     */
44
    public function loadGlobalConfig()
45
    {
46
        $resource = $this->application->getResource();
47
        if ($configPath = $resource->getConfigResourcePath('config/config')) {
48
            $this->application->getConfig()->load($configPath);
49
        }
50
        if ($listenerPath = $resource->getConfigResourcePath('config/listener')) {
51
            $this->application->loadEvent($listenerPath);
52
        }
53
    }
54
55
    /**
56
     * 加载路由
57
     */
58
    public function loadRoute()
59
    {
60
        $name = 'application-route';
61
        $this->application->debug()->time($name);
62
        if (static::isDebug()) {
63
            $this->loadRouteFromModules();
64
            if ($this->application->getRoute()->isContainClosure()) {
65
                $this->application->debug()->warning('route contain closure, route prepare cannot be cacheables');
66
            } else {
67
                $this->application->cache()->set(ApplicationLoader::CACHE_ROUTE, $this->application->getRoute()->getRouteCollection());
68
                $this->application->cache()->set(ApplicationLoader::CACHE_ROUTE_RUNNABLE, $this->application->getRoute()->getRunnable());
69
            }
70
        } elseif ($this->routeCacheAvailable()) {
71
            $route = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE);
72
            $runnable = $this->application->cache()->get(ApplicationLoader::CACHE_ROUTE_RUNNABLE);
73
            $this->application->getRoute()->setRouteCollection($route);
74
            $this->application->getRoute()->setRunnable($runnable);
75
            $this->application->debug()->info('load route from cache');
76
        } else {
77
            $this->loadRouteFromModules();
78
        }
79
        $this->application->debug()->timeEnd($name);
80
    }
81
82
    /**
83
     * 从模块中加载路由
84
     */
85
    private function loadRouteFromModules()
86
    {
87
        foreach ($this->application->getModules() as $name => $module) {
88
            if ($module->getStatus() === Module::REACHABLE) {
89
                call_user_func([$this->moduleLoader[$name], 'toReachable']);
90
            }
91
        }
92
    }
93
94
    /**
95
     * @return bool
96
     */
97
    private function routeCacheAvailable()
98
    {
99
        return $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE)
100
            && $this->application->cache()->has(ApplicationLoader::CACHE_ROUTE_RUNNABLE);
101
    }
102
103
    /**
104
     * 加载数据源
105
     *
106
     * @throws SQLException
107
     */
108
    public function loadDataSource()
109
    {
110
        Database::loadApplication($this->application);
111
        $dataSource = Database::getDefaultDataSource();
112
        $this->application->setDataSource($dataSource);
113
    }
114
}
115