Passed
Push — dev ( 8e1c31...1930e5 )
by 世昌
02:24
created

ApplicationModule   A

Complexity

Total Complexity 26

Size/Duplication

Total Lines 238
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 48
c 0
b 0
f 0
dl 0
loc 238
rs 10
wmc 26

15 Methods

Rating   Name   Duplication   Size   Complexity  
A parseSourceName() 0 17 6
A add() 0 3 1
A getModules() 0 3 1
A setModule() 0 5 1
A __construct() 0 5 1
A _() 0 3 1
A getRunning() 0 3 1
A getModuleSourceName() 0 11 4
A get() 0 6 2
A setLanguage() 0 5 1
A setRunning() 0 4 1
A initProperty() 0 9 3
A getModulePaths() 0 3 1
A getLanguage() 0 3 1
A find() 0 3 1
1
<?php
2
namespace suda\application;
3
4
use function array_key_exists;
5
use function sprintf;
6
use suda\framework\loader\Loader;
7
use suda\application\exception\ApplicationException;
8
9
/**
10
 * 模块化应用
11
 */
12
class ApplicationModule extends ApplicationContext
13
{
14
15
    /**
16
     * 模块集合
17
     *
18
     * @var ModuleBag
19
     */
20
    protected $module;
21
22
    /**
23
     * 字符串包
24
     *
25
     * @var LanguageBag
26
     */
27
    protected $language;
28
29
    /**
30
     * 模块路径
31
     *
32
     * @var string[]
33
     */
34
    protected $modulePaths;
35
36
    /**
37
     * 运行的模块
38
     *
39
     * @var Module
40
     */
41
    protected $running;
42
43
44
    /**
45
     * 创建应用
46
     *
47
     * @param string $path
48
     * @param array $manifest
49
     * @param Loader $loader
50
     * @param string|null $dataPath
51
     */
52
    public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null)
53
    {
54
        parent::__construct($path, $manifest, $loader, $dataPath);
55
        $this->module = new ModuleBag;
56
        $this->initProperty($manifest);
57
    }
58
59
    /**
60
     * 添加模块
61
     *
62
     * @param Module $module
63
     * @return void
64
     */
65
    public function add(Module $module)
66
    {
67
        $this->module->add($module);
68
    }
69
70
    /**
71
     * 查找模块
72
     *
73
     * @param string $name
74
     * @return Module|null
75
     */
76
    public function find(string $name):?Module
77
    {
78
        return $this->module->get($name);
79
    }
80
81
    /**
82
     * 获取模块
83
     *
84
     * @param string $name
85
     * @return Module
86
     */
87
    public function get(string $name): Module
88
    {
89
        if (($module = $this->find($name)) !== null) {
90
            return $module;
91
        }
92
        throw new ApplicationException(sprintf('module %s not exist', $name), ApplicationException::ERR_MODULE_NAME);
93
    }
94
95
    /**
96
     * 初始化属性
97
     *
98
     * @param array $manifest
99
     * @return void
100
     */
101
    protected function initProperty(array $manifest)
102
    {
103
        if (array_key_exists('module-paths', $manifest)) {
104
            $modulePaths = $manifest['module-paths'];
105
            foreach ($modulePaths as $name => $path) {
106
                $this->modulePaths[] = Resource::getPathByRelativePath($path, $this->path);
107
            }
108
        } else {
109
            $this->modulePaths = [ Resource::getPathByRelativePath('modules', $this->path) ];
110
        }
111
    }
112
113
    /**
114
     * Get 模块路径
115
     *
116
     * @return  string[]
117
     */
118
    public function getModulePaths()
119
    {
120
        return $this->modulePaths;
121
    }
122
123
124
    /**
125
     * 语言翻译
126
     *
127
     * @param string|null $message
128
     * @param mixed ...$args
129
     * @return string
130
     */
131
    public function _(?string $message, ...$args):string
132
    {
133
        return $this->language->interpolate($message, ...$args);
134
    }
135
136
    /**
137
     * Get 字符串包
138
     *
139
     * @return  LanguageBag
140
     */
141
    public function getLanguage()
142
    {
143
        return $this->language;
144
    }
145
146
    /**
147
     * Set 字符串包
148
     *
149
     * @param  LanguageBag  $language  字符串包
150
     *
151
     * @return  self
152
     */
153
    public function setLanguage(LanguageBag $language)
154
    {
155
        $this->language = $language;
156
157
        return $this;
158
    }
159
160
    /**
161
     * Get 运行的模块
162
     *
163
     * @return  Module
164
     */
165
    public function getRunning()
166
    {
167
        return $this->running;
168
    }
169
170
171
    /**
172
     * @param Module $running
173
     * @return $this
174
     */
175
    public function setRunning(Module $running)
176
    {
177
        $this->running = $running;
178
        return $this;
179
    }
180
181
    /**
182
     * Get 模块集合
183
     *
184
     * @return  ModuleBag
185
     */
186
    public function getModules():ModuleBag
187
    {
188
        return $this->module;
189
    }
190
191
    /**
192
     * Set 模块集合
193
     *
194
     * @param  ModuleBag  $module  模块集合
195
     *
196
     * @return  self
197
     */
198
    public function setModule(ModuleBag $module)
199
    {
200
        $this->module = $module;
201
202
        return $this;
203
    }
204
205
    /**
206
     * 解析资源名
207
     *
208
     * @param string $name
209
     * @param string|null $default
210
     * @param string|null $groupName
211
     * @return array
212
     */
213
    public function parseSourceName(string $name, ?string $default = null, ?string $groupName = null)
214
    {
215
        if (strpos($name, ':') !== false) {
216
            $dotpos = strrpos($name, ':');
217
            $module = substr($name, 0, $dotpos);
218
            $name = substr($name, $dotpos + 1);
219
            if (strlen($module) === 0) {
220
                $module = $default;
221
            }
222
        } else {
223
            $module = $default;
224
        }
225
        if ($module !== null && strpos($module, '@') !== false) {
226
            list($module, $groupName) = explode('@', $module, 2);
227
            $module = strlen($module) ? $module : $default;
228
        }
229
        return [$module, $groupName, $name];
230
    }
231
232
    /**
233
     * 获取模板下的资源名
234
     *
235
     * @param string $name
236
     * @param string|null $default
237
     * @return string
238
     */
239
    public function getModuleSourceName(string $name, ?string $default = null): string
240
    {
241
        if (strpos($name, ':') > 0) {
242
            list($module, $group, $name) = $this->parseSourceName($name, $default);
243
        } else {
244
            $module = $default;
245
        }
246
        if ($module !== null && ($moduleObj = $this->find($module))) {
247
            return $moduleObj->getFullName() . ':' . $name;
248
        }
249
        return $name;
250
    }
251
}
252