Passed
Push — master ( 934657...c3ba90 )
by 世昌
02:07
created

Module::setName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 5
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
    public function __construct(string $path, Config $config)
52
    {
53
        $this->config = $config;
54
        $this->path = $path;
55
        $this->resource = new Resource;
56
        $this->name = $config->get('name');
57
        $this->version = $config->get('version', '1.0.0');
58
    }
59
    
60
    public function registerResource()
61
    {
62
        if ($this->config->has("resource")) {
63
            $paths = $this->config->get('resource');
64
            if (\is_string($paths)) {
65
                $paths = [$paths];
66
            }
67
            foreach ($paths as $path) {
68
                if (Path::isRelativePath($path)) {
69
                    $path = Resource::getPathByRelativedPath($path, $this->path);
70
                }
71
                $this->resource->addResourcePath($path);
72
            }
73
        }
74
    }
75
76
    public function getRoute(string $groupName): GroupRoutes
77
    {
78
        $group = $groupName === 'default' ? '': '-'. $groupName;
79
        $routeConfig = $this->getConfig('route'.$group, ['group' => $group,]);
80
        $routes =  new GroupRoutes($groupName);
81
        if (\is_array($routeConfig)) {
82
            $prefix = $this->config->get('route.prefix', '');
83
            foreach ($routeConfig as $name => $value) {
84
                if (\array_key_exists('url', $value)) {
85
                    $uri = $prefix.$value['url'];
86
                    $method = $value['method'] ?? ['GET'];
87
                    $routes->add($this->getName(), $name, new RouteMatcher($method, $uri, $value));
88
                }
89
            }
90
        }
91
        return  $routes;
92
    }
93
94
    /**
95
     * 获取配置信息
96
     *
97
     * @param string $name
98
     * @param array $extra
99
     * @return array|null
100
     */
101
    public function getConfig(string $name, array $extra = []): ?array
102
    {
103
        $path = $this->resource->getConfigResourcePath('config/'.$name);
104
        if (\is_null($path)) {
105
            return null;
106
        }
107
        $extra['module'] = $this->getFullName();
108
        $extra['module-version'] = $this->getVersion();
109
        $extra['module-name'] = $this->getName();
110
        $routeConfig = $this->config->loadConfig($path, $extra);
111
        return $routeConfig;
112
    }
113
114
    /**
115
     * Get 模块全名称
116
     *
117
     * @return  string
118
     */
119
    public function getFullName()
120
    {
121
        return $this->name .':'. $this->version;
122
    }
123
124
    /**
125
     * Set 模块名称
126
     *
127
     * @param  string  $name  模块名称
128
     *
129
     * @return  self
130
     */
131
    public function setName(string $name)
132
    {
133
        $this->name = $name;
134
135
        return $this;
136
    }
137
138
    /**
139
     * Get 模块路径
140
     *
141
     * @return  string
142
     */
143
    public function getPath()
144
    {
145
        return $this->path;
146
    }
147
148
    /**
149
     * Get 模块版本
150
     *
151
     * @return  string
152
     */ 
153
    public function getVersion()
154
    {
155
        return $this->version;
156
    }
157
158
    /**
159
     * Set 模块版本
160
     *
161
     * @param  string  $version  模块版本
162
     *
163
     * @return  self
164
     */ 
165
    public function setVersion(string $version)
166
    {
167
        $this->version = $version;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Get 模块名称
174
     *
175
     * @return  string
176
     */ 
177
    public function getName()
178
    {
179
        return $this->name;
180
    }
181
}
182