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

ApplicationContext::getManifest()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace suda\application;
3
4
use suda\orm\DataSource;
5
use suda\framework\Config;
6
use suda\framework\Context;
7
use suda\application\Resource;
8
use suda\framework\loader\Loader;
9
use suda\framework\arrayobject\ArrayDotAccess;
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 Resource
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
     */
80
    public function __construct(string $path, array $manifest, Loader $loader, string $dataPath = null)
81
    {
82
        parent::__construct(new Config(['app' => $manifest]), $loader);
83
        $this->path = $path;
84
        $this->routeGroup = $manifest['route-group'] ?? ['default'];
85
        $this->resource = new Resource([Resource::getPathByRelativePath($manifest['resource'] ?? './resource', $path)]);
86
        $this->locate = $manifest['locale'] ?? 'zh-cn';
87
        $this->style = $manifest['style'] ?? 'default';
88
        $this->manifest = $manifest;
89
        $this->dataPath = $dataPath ?? Resource::getPathByRelativePath($manifest['resource'] ?? './data', $path);
90
    }
91
92
    /**
93
     * Get 使用的样式
94
     *
95
     * @return  string
96
     */
97
    public function getStyle()
98
    {
99
        return $this->style;
100
    }
101
102
    /**
103
     * Set 使用的样式
104
     *
105
     * @param  string  $style  使用的样式
106
     *
107
     * @return  self
108
     */
109
    public function setStyle(string $style)
110
    {
111
        $this->style = $style;
112
113
        return $this;
114
    }
115
116
    /**
117
     * Get 语言
118
     *
119
     * @return  string
120
     */
121
    public function getLocate()
122
    {
123
        return $this->locate;
124
    }
125
126
    /**
127
     * Set 语言
128
     *
129
     * @param  string  $locate  语言
130
     *
131
     * @return  self
132
     */
133
    public function setLocate(string $locate)
134
    {
135
        $this->locate = $locate;
136
137
        return $this;
138
    }
139
140
    /**
141
     * Get 配置数组
142
     *
143
     * @param string|null $name
144
     * @param null $default
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $default is correct as it would always require null to be passed?
Loading history...
145
     * @return  mixed
146
     */
147
    public function getManifest(string $name = null, $default = null)
148
    {
149
        if ($name !== null) {
150
            return ArrayDotAccess::get($this->manifest, $name, $default);
151
        }
152
        return $this->manifest;
153
    }
154
155
    /**
156
     * Get 路由组
157
     *
158
     * @return  array
159
     */
160
    public function getRouteGroup()
161
    {
162
        return $this->routeGroup;
163
    }
164
165
    /**
166
     * Get 数据源
167
     *
168
     * @return  Resource
169
     */
170
    public function getResource(): Resource
171
    {
172
        return $this->resource;
173
    }
174
175
    /**
176
     * Set 数据源
177
     *
178
     * @param  Resource  $resource  数据源
179
     *
180
     * @return  self
181
     */
182
    public function setResource(Resource $resource)
183
    {
184
        $this->resource = $resource;
185
186
        return $this;
187
    }
188
189
    /**
190
     * Get 数据源
191
     *
192
     * @return  DataSource
193
     */
194
    public function getDataSource()
195
    {
196
        return $this->dataSource;
197
    }
198
199
    /**
200
     * Set 数据源
201
     *
202
     * @param  DataSource  $dataSource  数据源
203
     *
204
     * @return  self
205
     */
206
    public function setDataSource(DataSource $dataSource)
207
    {
208
        $this->dataSource = $dataSource;
209
210
        return $this;
211
    }
212
213
    /**
214
     * Get 应用路径
215
     *
216
     * @return  string
217
     */
218
    public function getPath()
219
    {
220
        return $this->path;
221
    }
222
223
    /**
224
     * Get 数据路径
225
     *
226
     * @return  string
227
     */ 
228
    public function getDataPath()
229
    {
230
        return $this->dataPath;
231
    }
232
233
    /**
234
     * Set 数据路径
235
     *
236
     * @param  string  $dataPath  数据路径
237
     *
238
     * @return  self
239
     */ 
240
    public function setDataPath(string $dataPath)
241
    {
242
        $this->dataPath = $dataPath;
243
244
        return $this;
245
    }
246
}
247