Passed
Push — master ( 9d5835...e8364e )
by 世昌
02:16
created

ApplicationBase::getModulePaths()   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 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 ApplicationBase extends ApplicationContext
13
{
14
    /**
15
     * 模块集合
16
     *
17
     * @var ModuleBag
18
     */
19
    protected $module;
20
21
    /**
22
     * 字符串包
23
     *
24
     * @var LanguageBag
25
     */
26
    protected $language;
27
28
    /**
29
     * 模块路径
30
     *
31
     * @var string[]
32
     */
33
    protected $modulePaths;
34
35
    /**
36
     * 运行的模块
37
     *
38
     * @var Module
39
     */
40
    protected $running;
41
42
    /**
43
     * 系统准备完成
44
     *
45
     * @var boolean
46
     */
47
    protected $isPrepared = false;
48
49
    /**
50
     * 创建应用
51
     *
52
     * @param string $path
53
     * @param array $manifest
54
     * @param Loader $loader
55
     */
56
    public function __construct(string $path, array $manifest, Loader $loader)
57
    {
58
        parent::__construct($path, $manifest, $loader);
59
        $this->module = new ModuleBag;
60
        $this->initProperty($manifest);
61
    }
62
63
    /**
64
     * 添加模块
65
     *
66
     * @param Module $module
67
     * @return void
68
     */
69
    public function add(Module $module)
70
    {
71
        $this->module->add($module);
72
    }
73
74
    /**
75
     * 查找模块
76
     *
77
     * @param string $name
78
     * @return Module|null
79
     */
80
    public function find(string $name):?Module
81
    {
82
        return $this->module->get($name);
83
    }
84
85
    /**
86
     * 获取模块
87
     *
88
     * @param string $name
89
     * @return Module
90
     *@throws ApplicationException
91
     */
92
    public function get(string $name): Module
93
    {
94
        if (($module = $this->find($name)) !== null) {
95
            return $module;
96
        }
97
        throw new ApplicationException(sprintf('module %s not exist', $name), ApplicationException::ERR_MODULE_NAME);
98
    }
99
100
    /**
101
     * 初始化属性
102
     *
103
     * @param array $manifest
104
     * @return void
105
     */
106
    protected function initProperty(array $manifest)
107
    {
108
        if (array_key_exists('module-paths', $manifest)) {
109
            $modulePaths = $manifest['module-paths'];
110
            foreach ($modulePaths as $name => $path) {
111
                $this->modulePaths[] = Resource::getPathByRelativePath($path, $this->path);
112
            }
113
        } else {
114
            $this->modulePaths = [ Resource::getPathByRelativePath('modules', $this->path) ];
115
        }
116
    }
117
118
    /**
119
     * Get 模块路径
120
     *
121
     * @return  string[]
122
     */
123
    public function getModulePaths()
124
    {
125
        return $this->modulePaths;
126
    }
127
128
129
    /**
130
     * 转换类名
131
     *
132
     * @param string $name
133
     * @return string
134
     */
135
    protected function className(string $name)
136
    {
137
        return str_replace(['.','/'], '\\', $name);
138
    }
139
140
    /**
141
     * 语言翻译
142
     *
143
     * @param string $message
144
     * @param mixed  ...$args
145
     * @return string
146
     */
147
    public function _(string $message, ...$args):string
148
    {
149
        return $this->language->interpolate($message, ...$args);
150
    }
151
152
    /**
153
     * Get 字符串包
154
     *
155
     * @return  LanguageBag
156
     */
157
    public function getLanguage()
158
    {
159
        return $this->language;
160
    }
161
162
    /**
163
     * Set 字符串包
164
     *
165
     * @param  LanguageBag  $language  字符串包
166
     *
167
     * @return  self
168
     */
169
    public function setLanguage(LanguageBag $language)
170
    {
171
        $this->language = $language;
172
173
        return $this;
174
    }
175
176
    /**
177
     * Get 运行的模块
178
     *
179
     * @return  Module
180
     */
181
    public function getRunning()
182
    {
183
        return $this->running;
184
    }
185
186
    /**
187
     * Set 运行的模块
188
     *
189
     * @param  Module  $running  运行的模块
190
     *
191
     * @return  self
192
     */
193
    public function setRunning(Module $running)
194
    {
195
        $this->running = $running;
196
197
        return $this;
198
    }
199
200
    /**
201
     * Get 模块集合
202
     *
203
     * @return  ModuleBag
204
     */
205
    public function getModules():ModuleBag
206
    {
207
        return $this->module;
208
    }
209
210
    /**
211
     * Set 模块集合
212
     *
213
     * @param  ModuleBag  $module  模块集合
214
     *
215
     * @return  self
216
     */
217
    public function setModule(ModuleBag $module)
218
    {
219
        $this->module = $module;
220
221
        return $this;
222
    }
223
}
224