Passed
Push — master ( 4c0566...cfb65e )
by 世昌
02:26
created

ModuleLoader::assertModuleVersion()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 2
dl 0
loc 18
rs 9.8333
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A ModuleLoader::loadModuleConfig() 0 3 1
A ModuleLoader::loadBaseConfig() 0 4 3
1
<?php
2
namespace suda\application\loader;
3
4
use Exception;
5
use suda\framework\Config;
6
use suda\application\Module;
7
use suda\application\Resource;
8
use suda\application\Application;
9
use suda\framework\filesystem\FileSystem;
10
use suda\application\builder\ApplicationBuilder;
11
use suda\application\exception\ApplicationException;
12
13
/**
14
 * 应用程序
15
 */
16
class ModuleLoader extends ModuleLoaderUtil
17
{
18
    public function toLoad()
19
    {
20
        $this->loadVendorIfExist();
21
        $this->loadShareLibrary();
22
        $this->application->debug()->info('loaded - '.$this->module->getFullName());
23
    }
24
25
    public function toActive()
26
    {
27
        $this->loadConfig();
28
        $this->application->debug()->info('active = '.$this->module->getFullName());
29
    }
30
31
    /**
32
     * @throws Exception
33
     */
34
    public function toReachable()
35
    {
36
        $this->loadRoute();
37
        $this->application->debug()->info('reachable # '.$this->module->getFullName());
38
    }
39
40
    public function toRunning()
41
    {
42
        $this->checkRequirements();
43
        $this->loadPrivateLibrary();
44
        $this->application->setRunning($this->module);
45
        $this->application->debug()->info('run + '.$this->module->getFullName());
46
    }
47
48
    /**
49
     * 加载模块额外资源
50
     */
51
    public function loadExtraModuleResourceLibrary()
52
    {
53
        $resource = $this->module->getProperty('module-resource', []);
54
        if (count($resource)) {
55
            foreach ($resource as $name => $path) {
56
                if ($find = $this->application->find($name)) {
57
                    $find->getResource()->addResourcePath(
58
                        Resource::getPathByRelativePath($path, $this->module->getPath())
59
                    );
60
                }
61
            }
62
        }
63
    }
64
65
    protected function loadShareLibrary()
66
    {
67
        $import = $this->module->getProperty('import.share', []);
68
        if (count($import)) {
69
            $this->importClassLoader($import, $this->module->getPath());
70
        }
71
    }
72
73
    protected function loadPrivateLibrary()
74
    {
75
        $import = $this->module->getProperty('import.src', []);
76
        if (count($import)) {
77
            $this->importClassLoader($import, $this->module->getPath());
78
        }
79
    }
80
81
    public function loadVendorIfExist()
82
    {
83
        $vendorAutoload = $this->module->getPath().'/vendor/autoload.php';
84
        if (FileSystem::exist($vendorAutoload)) {
85
            require_once $vendorAutoload;
86
        }
87
    }
88
89
    protected function loadConfig() {
90
        $this->loadModuleConfig($this->module);
91
    }
92
93
    protected function loadModuleConfig(Module $module) {
94
        $this->loadBaseConfig($module);
95
        $this->loadEventListener($module);
96
    }
97
98
    protected function loadBaseConfig(Module $module) {
99
        $path = $module->getResource()->getConfigResourcePath('config/config');
100
        if ($path !== null && ($config = Config::loadConfig($path, $module->getProperty())) !== null) {
101
            $module->setConfig($config);
102
        }
103
    }
104
105
    protected function loadEventListener(Module $module)
106
    {
107
        if ($path = $module->getResource()->getConfigResourcePath('config/event')) {
108
            $event = Config::loadConfig($path, [
109
                'module' => $this->module->getName(),
110
                'property' => $this->module->getProperty(),
111
                'config' => $this->module->getConfig(),
112
            ]);
113
            if (is_array($event)) {
114
                $this->application->event()->load($event);
115
            }
116
        }
117
    }
118
119
    /**
120
     * 加载路由
121
     */
122
    protected function loadRoute()
123
    {
124
        foreach ($this->application->getRouteGroup() as $group) {
125
            $this->loadRouteGroup($group);
126
        }
127
    }
128
129
    /**
130
     * 加载路由组
131
     *
132
     * @param string $groupName
133
     * @return void
134
     */
135
    protected function loadRouteGroup(string $groupName)
136
    {
137
        $group = $groupName === 'default' ? '': '-'. $groupName;
138
        if ($path = $this->module->getResource()->getConfigResourcePath('config/route'.$group)) {
139
            $routeConfig = Config::loadConfig($path, [
140
                'module' => $this->module->getName(),
141
                'group' => $groupName,
142
                'property' => $this->module->getProperty(),
143
                'config' => $this->module->getConfig(),
144
            ]);
145
            if ($routeConfig !== null) {
146
                $prefix = $this->module->getConfig('route-prefix.'.$groupName, '');
147
                $this->loadRouteConfig($prefix, $groupName, $routeConfig);
148
            }
149
        }
150
    }
151
152
    /**
153
     * 加载模块路由配置
154
     *
155
     * @param string $prefix
156
     * @param string $groupName
157
     * @param array $routeConfig
158
     * @return void
159
     */
160
    protected function loadRouteConfig(string $prefix, string $groupName, array $routeConfig)
161
    {
162
        $module =  $this->module->getFullName();
163
        foreach ($routeConfig as $name => $config) {
164
            $exname = $this->application->getRouteName($name, $module, $groupName);
165
            $method = $config['method'] ?? [];
166
            $attributes = [];
167
            $attributes['module'] = $module;
168
            $attributes['config'] = $config;
169
            $attributes['group'] = $groupName;
170
            $attributes['route'] = $exname;
171
            $uri = $config['uri'] ?? '/';
172
            $anti = array_key_exists('anti-prefix', $config) && $config['anti-prefix'];
173
            if ($anti) {
174
                $uri = '/'.trim($uri, '/');
175
            } else {
176
                $uri = '/'.trim($prefix . $uri, '/');
177
            }
178
            $this->application->request($method, $exname, $uri, $attributes);
179
        }
180
    }
181
}
182