Passed
Push — dev ( 785e91...c8721b )
by 世昌
02:13
created

ApplicationContext::setPath()   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
c 0
b 0
f 0
nc 1
nop 1
dl 0
loc 3
rs 10
1
<?php
2
namespace suda\application;
3
4
use suda\database\DataSource;
5
use suda\framework\Config;
6
use suda\framework\Context;
7
use suda\framework\loader\Loader;
8
use suda\framework\arrayobject\ArrayDotAccess;
9
use suda\application\Resource as ApplicationResource;
10
11
/**
12
 * 应用程序环境
13
 */
14
class ApplicationContext extends Context
15
{
16
    /**
17
     * 应用路径
18
     *
19
     * @var string
20
     */
21
    protected $path;
22
23
24
    /**
25
     * 数据路径
26
     *
27
     * @var string
28
     */
29
    protected $dataPath;
30
31
    /**
32
     * 配置数组
33
     *
34
     * @var array
35
     */
36
    protected $manifest;
37
38
    /**
39
     * 语言
40
     *
41
     * @var string
42
     */
43
    protected $locate;
44
45
    /**
46
     * 使用的样式
47
     *
48
     * @var string
49
     */
50
    protected $style;
51
52
    /**
53
     * 路由组
54
     *
55
     * @var array
56
     */
57
    protected $routeGroup;
58
59
    /**
60
     * 资源目录
61
     *
62
     * @var ApplicationResource
63
     */
64
    protected $resource;
65
66
    /**
67
     * 数据源
68
     *
69
     * @var DataSource
70
     */
71
    protected $dataSource;
72
73
    /**
74
     * 创建应用
75
     *
76
     * @param string $path
77
     * @param array $manifest
78
     * @param Loader $loader
79
     * @param string|null $dataPath
80
     */
81
    public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null)
82
    {
83
        parent::__construct(new Config(['app' => $manifest]), $loader);
84
        $this->setPath($path);
85
        $this->setManifest($manifest);
86
        $this->setRouteGroup($manifest['route-group'] ?? ['default']);
87
        $resource = new Resource();
88
        $resource->registerResourcePath($path, $manifest['resource'] ?? './resource');
89
        $this->setResource($resource);
90
        $this->setLocate($manifest['locale'] ?? 'zh-cn');
91
        $this->setStyle($manifest['style'] ?? 'default');
92
        $this->setDataPath($dataPath ?? Resource::getPathByRelativePath($manifest['resource'] ?? './data', $path));
93
        $this->config->set('data_path', $this->dataPath);
94
    }
95
96
    /**
97
     * @param string $path
98
     */
99
    public function setPath(string $path): void
100
    {
101
        $this->path = $path;
102
    }
103
104
    /**
105
     * @param array $manifest
106
     */
107
    public function setManifest(array $manifest): void
108
    {
109
        $this->manifest = $manifest;
110
    }
111
112
    /**
113
     * @param array $routeGroup
114
     */
115
    public function setRouteGroup(array $routeGroup): void
116
    {
117
        $this->routeGroup = $routeGroup;
118
    }
119
120
    /**
121
     * Get 使用的样式
122
     *
123
     * @return  string
124
     */
125
    public function getStyle()
126
    {
127
        return $this->style;
128
    }
129
130
    /**
131
     * Set 使用的样式
132
     *
133
     * @param string $style 使用的样式
134
     *
135
     * @return  self
136
     */
137
    public function setStyle(string $style)
138
    {
139
        $this->style = $style;
140
141
        return $this;
142
    }
143
144
    /**
145
     * @return string
146
     */
147
    public function getLocate(): string
148
    {
149
        return $this->locate;
150
    }
151
152
    /**
153
     * @param string $locate
154
     */
155
    public function setLocate(string $locate): void
156
    {
157
        $this->locate = $locate;
158
    }
159
160
161
    /**
162
     * Get 配置数组
163
     *
164
     * @param string|null $name
165
     * @param mixed $default
166
     * @return  mixed
167
     */
168
    public function getManifest(string $name = null, $default = null)
169
    {
170
        if ($name !== null) {
171
            return ArrayDotAccess::get($this->manifest, $name, $default);
172
        }
173
        return $this->manifest;
174
    }
175
176
    /**
177
     * Get 路由组
178
     *
179
     * @return  array
180
     */
181
    public function getRouteGroup()
182
    {
183
        return $this->routeGroup;
184
    }
185
186
    /**
187
     * Get 数据源
188
     *
189
     * @return  ApplicationResource
190
     */
191
    public function getResource(): ApplicationResource
192
    {
193
        return $this->resource;
194
    }
195
196
    /**
197
     * Set 数据源
198
     *
199
     * @param ApplicationResource $resource 数据源
200
     *
201
     * @return  self
202
     */
203
    public function setResource(ApplicationResource $resource)
204
    {
205
        $this->resource = $resource;
206
207
        return $this;
208
    }
209
210
    /**
211
     * Get 数据源
212
     *
213
     * @return  DataSource
214
     */
215
    public function getDataSource()
216
    {
217
        return $this->dataSource;
218
    }
219
220
    /**
221
     * Set 数据源
222
     *
223
     * @param  DataSource  $dataSource  数据源
224
     *
225
     * @return  self
226
     */
227
    public function setDataSource(DataSource $dataSource)
228
    {
229
        $this->dataSource = $dataSource;
230
231
        return $this;
232
    }
233
234
    /**
235
     * Get 应用路径
236
     *
237
     * @return  string
238
     */
239
    public function getPath()
240
    {
241
        return $this->path;
242
    }
243
244
    /**
245
     * Get 数据路径
246
     *
247
     * @return  string
248
     */
249
    public function getDataPath()
250
    {
251
        return $this->dataPath;
252
    }
253
254
    /**
255
     * Set 数据路径
256
     *
257
     * @param string $dataPath 数据路径
258
     *
259
     * @return  self
260
     */
261
    public function setDataPath(string $dataPath)
262
    {
263
        $this->dataPath = $dataPath;
264
265
        return $this;
266
    }
267
}
268