Test Failed
Push — master ( f4c898...1a3120 )
by 世昌
01:58
created

Module::getConfigFrom()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 8
nc 2
nop 2
dl 0
loc 12
rs 10
c 0
b 0
f 0
1
<?php
2
namespace nebula\application\module;
3
4
use nebula\application\Route;
5
use nebula\application\Resource;
6
use nebula\component\loader\Path;
7
use nebula\application\config\Config;
8
use nebula\component\route\RouteMatcher;
9
use nebula\application\route\GroupRoutes;
10
11
/**
12
 * 模块
13
 */
14
class Module
15
{
16
    /**
17
     * 模块资源
18
     *
19
     * @var Resource
20
     */
21
    protected $resource;
22
23
    /**
24
     * 配置信息
25
     *
26
     * @var Config
27
     */
28
    protected $config;
29
30
    /**
31
     * 模块名称
32
     *
33
     * @var string
34
     */
35
    protected $name;
36
37
    /**
38
     * 模块版本
39
     *
40
     * @var string
41
     */
42
    protected $version;
43
44
    /**
45
     * 模块路径
46
     *
47
     * @var string
48
     */
49
    protected $path;
50
51
    /**
52
     * 优先级
53
     *
54
     * @var int
55
     */
56
    protected $priority;
57
58
59
    public function __construct(string $path, Config $config)
60
    {
61
        $this->config = $config;
62
        $this->path = $path;
63
        $this->resource = new Resource;
64
        $this->name = $config->get('name');
65
        $this->version = $config->get('version', '1.0.0');
66
        $this->priority = $config->get('priority', 0);
67
    }
68
    
69
    public function registerResource()
70
    {
71
        if ($this->config->has("resource")) {
72
            $paths = $this->config->get('resource');
73
            if (\is_string($paths)) {
74
                $paths = [$paths];
75
            }
76
            foreach ($paths as $path) {
77
                if (Path::isRelativePath($path)) {
78
                    $path = Resource::getPathByRelativedPath($path, $this->path);
79
                }
80
                $this->resource->addResourcePath($path);
81
            }
82
        }
83
    }
84
85
    public function getRoute(string $groupName): GroupRoutes
86
    {
87
        $group = $groupName === 'default' ? '': '-'. $groupName;
88
        $routeConfig = $this->getConfigFrom('route'.$group, ['group' => $group,]);
89
        $routes =  new GroupRoutes($groupName);
90
        if (\is_array($routeConfig)) {
91
            // debug()->debug('load route {group} from {module}',['group'=>$groupName, 'module' => $this->getFullName()]);
92
            $prefix = $this->config->get('route.prefix', '');
93
            foreach ($routeConfig as $name => $value) {
94
                if (\array_key_exists('url', $value)) {
95
                    $uri = $prefix.$value['url'];
96
                    $method = $value['method'] ?? ['GET'];
97
                    $matcher = new RouteMatcher($method, $uri, $value);
98
                    $matcher->addAttribute('module', $this->getFullName());
99
                    $matcher->addAttribute('group', $groupName);
100
                    $routes->add($this->getFullName(), $name, $matcher);
101
                }
102
            }
103
        }
104
        return  $routes;
105
    }
106
107
    /**
108
     * 获取模块配置
109
     *
110
     * @return Config
111
     */
112
    public function getConfig():Config {
113
        return $this->config;
114
    }
115
116
    /**
117
     * 获取配置信息
118
     *
119
     * @param string $name
120
     * @param array $extra
121
     * @return array|null
122
     */
123
    public function getConfigFrom(string $name, array $extra = []): ?array
124
    {
125
        $path = $this->resource->getConfigResourcePath('config/'.$name);
126
        if (\is_null($path)) {
127
            return null;
128
        }
129
        $extra['module'] = $this->getFullName();
130
        $extra['module-version'] = $this->getVersion();
131
        $extra['module-name'] = $this->getName();
132
        $routeConfig = $this->config->loadConfig($path, $extra);
133
        // debug()->debug('get {module} {name} config from {path}',['name'=> $name, 'path' => $path, 'module' => $this->getFullName()]);
134
        return $routeConfig;
135
    }
136
137
    /**
138
     * Get 模块全名称
139
     *
140
     * @return  string
141
     */
142
    public function getFullName()
143
    {
144
        return $this->name .':'. $this->version;
145
    }
146
147
    /**
148
     * Set 模块名称
149
     *
150
     * @param  string  $name  模块名称
151
     *
152
     * @return  self
153
     */
154
    public function setName(string $name)
155
    {
156
        $this->name = $name;
157
158
        return $this;
159
    }
160
161
    /**
162
     * Get 模块路径
163
     *
164
     * @return  string
165
     */
166
    public function getPath()
167
    {
168
        return $this->path;
169
    }
170
171
    /**
172
     * Get 模块版本
173
     *
174
     * @return  string
175
     */ 
176
    public function getVersion()
177
    {
178
        return $this->version;
179
    }
180
181
    /**
182
     * Set 模块版本
183
     *
184
     * @param  string  $version  模块版本
185
     *
186
     * @return  self
187
     */ 
188
    public function setVersion(string $version)
189
    {
190
        $this->version = $version;
191
192
        return $this;
193
    }
194
195
    /**
196
     * Get 模块名称
197
     *
198
     * @return  string
199
     */ 
200
    public function getName()
201
    {
202
        return $this->name;
203
    }
204
205
    /**
206
     * Get 优先级
207
     *
208
     * @return  int
209
     */ 
210
    public function getPriority()
211
    {
212
        return $this->priority;
213
    }
214
215
    /**
216
     * Set 优先级
217
     *
218
     * @param  int  $priority  优先级
219
     *
220
     * @return  self
221
     */ 
222
    public function setPriority(int $priority)
223
    {
224
        $this->priority = $priority;
225
226
        return $this;
227
    }
228
}
229