Passed
Push — dev ( 849c07...677b95 )
by 世昌
03:06
created

ApplicationLoader::activeModule()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 3
nc 3
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace suda\application\loader;
4
5
use suda\framework\Cache;
6
use suda\framework\Config;
7
use suda\application\Module;
8
use suda\application\Resource;
9
use suda\application\ModuleBag;
10
use suda\application\Application;
11
use suda\framework\loader\Loader;
12
use suda\framework\runnable\Runnable;
13
use suda\application\database\Database;
14
use suda\database\exception\SQLException;
15
use suda\framework\filesystem\FileSystem;
16
use suda\application\builder\ModuleBuilder;
17
18
/**
19
 * 应用程序
20
 */
21
class ApplicationLoader extends ApplicationModuleLoader
22
{
23
24
    /**
25
     * 加载额外vendor
26
     */
27
    public function loadVendorIfExist()
28
    {
29
        $vendorAutoload = $this->application->getPath() . '/vendor/autoload.php';
30
        if (FileSystem::exist($vendorAutoload)) {
31
            Loader::requireOnce($vendorAutoload);
32
        }
33
    }
34
35
    /**
36
     * 加载APP
37
     */
38
    public function load()
39
    {
40
        $this->loadVendorIfExist();
41
        $this->loadGlobalConfig();
42
        $this->loadModule();
43
    }
44
45
    /**
46
     * 加载全局配置
47
     */
48
    public function loadGlobalConfig()
49
    {
50
        $resource = $this->application->getResource();
51
        if ($configPath = $resource->getConfigResourcePath('config/config')) {
52
            $this->application->getConfig()->load($configPath);
53
        }
54
        if ($listenerPath = $resource->getConfigResourcePath('config/listener')) {
55
            $this->application->loadEvent($listenerPath);
56
        }
57
    }
58
59
    /**
60
     * 加载路由
61
     */
62
    public function loadRoute()
63
    {
64
        $name = 'application-route';
65
        $this->application->debug()->time($name);
66
        if (static::enableRouteCache() === false) {
0 ignored issues
show
Bug Best Practice introduced by
The method suda\application\loader\...der::enableRouteCache() is not static, but was called statically. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

66
        if (static::/** @scrutinizer ignore-call */ enableRouteCache() === false) {
Loading history...
67
            $this->loadRouteFromModules();
68
        } elseif ($this->application->cache()->has($name.'-route')) {
69
            $route = $this->application->cache()->get($name.'-route');
70
            $runnable = $this->application->cache()->get($name.'-runnable');
71
            $this->application->getRoute()->setRouteCollection($route);
72
            $this->application->getRoute()->setRunnable($runnable);
73
            $this->application->debug()->info('load route from cache');
74
        } else {
75
            $this->loadRouteFromModules();
76
            if ($this->application->getRoute()->isContainClosure()) {
77
                $this->application->debug()->warning('route contain closure, route prepare cannot be cacheables');
78
            } else {
79
                $this->application->cache()->set($name.'-route', $this->application->getRoute()->getRouteCollection());
80
                $this->application->cache()->set($name.'-runnable', $this->application->getRoute()->getRunnable());
81
            }
82
        }
83
        $this->application->debug()->timeEnd($name);
84
    }
85
86
    /**
87
     * 从模块中加载路由
88
     */
89
    private function loadRouteFromModules()
90
    {
91
        foreach ($this->application->getModules() as $name => $module) {
92
            if ($module->getStatus() === Module::REACHABLE) {
93
                call_user_func([$this->moduleLoader[$name], 'toReachable']);
94
            }
95
        }
96
    }
97
98
    /**
99
     * @return bool
100
     */
101
    private function enableRouteCache() {
102
        return boolval($this->application->getCache()->get('route-cache', static::isDebug()));
103
    }
104
105
    /**
106
     * 加载数据源
107
     *
108
     * @throws SQLException
109
     */
110
    public function loadDataSource()
111
    {
112
        Database::loadApplication($this->application);
113
        $dataSource = Database::getDefaultDataSource();
114
        $this->application->setDataSource($dataSource);
115
    }
116
}
117