ApplicationModule::parseSourceName()   A
last analyzed

Complexity

Conditions 6
Paths 9

Size

Total Lines 17
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

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