Passed
Push — master ( f081ea...af57fd )
by 世昌
04:00
created

Module::getPriority()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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->getConfig('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
                    $routes->add($this->getFullName(), $name, new RouteMatcher($method, $uri, $value));
98
                }
99
            }
100
        }
101
        return  $routes;
102
    }
103
104
    /**
105
     * 获取配置信息
106
     *
107
     * @param string $name
108
     * @param array $extra
109
     * @return array|null
110
     */
111
    public function getConfig(string $name, array $extra = []): ?array
112
    {
113
        $path = $this->resource->getConfigResourcePath('config/'.$name);
114
        if (\is_null($path)) {
115
            return null;
116
        }
117
        $extra['module'] = $this->getFullName();
118
        $extra['module-version'] = $this->getVersion();
119
        $extra['module-name'] = $this->getName();
120
        $routeConfig = $this->config->loadConfig($path, $extra);
121
        // debug()->debug('get {module} {name} config from {path}',['name'=> $name, 'path' => $path, 'module' => $this->getFullName()]);
122
        return $routeConfig;
123
    }
124
125
    /**
126
     * Get 模块全名称
127
     *
128
     * @return  string
129
     */
130
    public function getFullName()
131
    {
132
        return $this->name .':'. $this->version;
133
    }
134
135
    /**
136
     * Set 模块名称
137
     *
138
     * @param  string  $name  模块名称
139
     *
140
     * @return  self
141
     */
142
    public function setName(string $name)
143
    {
144
        $this->name = $name;
145
146
        return $this;
147
    }
148
149
    /**
150
     * Get 模块路径
151
     *
152
     * @return  string
153
     */
154
    public function getPath()
155
    {
156
        return $this->path;
157
    }
158
159
    /**
160
     * Get 模块版本
161
     *
162
     * @return  string
163
     */ 
164
    public function getVersion()
165
    {
166
        return $this->version;
167
    }
168
169
    /**
170
     * Set 模块版本
171
     *
172
     * @param  string  $version  模块版本
173
     *
174
     * @return  self
175
     */ 
176
    public function setVersion(string $version)
177
    {
178
        $this->version = $version;
179
180
        return $this;
181
    }
182
183
    /**
184
     * Get 模块名称
185
     *
186
     * @return  string
187
     */ 
188
    public function getName()
189
    {
190
        return $this->name;
191
    }
192
193
    /**
194
     * Get 优先级
195
     *
196
     * @return  int
197
     */ 
198
    public function getPriority()
199
    {
200
        return $this->priority;
201
    }
202
203
    /**
204
     * Set 优先级
205
     *
206
     * @param  int  $priority  优先级
207
     *
208
     * @return  self
209
     */ 
210
    public function setPriority(int $priority)
211
    {
212
        $this->priority = $priority;
213
214
        return $this;
215
    }
216
}
217