Passed
Push — master ( acba12...b2f89b )
by 世昌
04:15
created

ApplicationContext::setRouteGroup()   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 ApplicationResource
56
     */
57
    protected $resource;
58
59
    /**
60
     * 数据源
61
     *
62
     * @var DataSource
63
     */
64
    protected $dataSource;
65
66
    /**
67
     * 创建应用
68
     *
69
     * @param string $path
70
     * @param array $manifest
71
     * @param Loader $loader
72
     * @param string|null $dataPath
73
     */
74
    public function __construct(string $path, array $manifest, Loader $loader, ?string $dataPath = null)
75
    {
76
        parent::__construct(new Config(['app' => $manifest]), $loader);
77
        $this->setPath($path);
78
        $this->setManifest($manifest);
79
        $resource = new Resource();
80
        $resource->registerResourcePath($path, $manifest['resource'] ?? './resource');
81
        $this->setResource($resource);
82
        $this->setLocate($manifest['locale'] ?? 'zh-cn');
83
        $this->setStyle($manifest['style'] ?? 'default');
84
        $this->setDataPath($dataPath ?? Resource::getPathByRelativePath($manifest['resource'] ?? './data', $path));
85
        $this->config->set('data_path', $this->dataPath);
86
    }
87
88
    /**
89
     * @param string $path
90
     */
91
    public function setPath(string $path): void
92
    {
93
        $this->path = $path;
94
    }
95
96
    /**
97
     * @param array $manifest
98
     */
99
    public function setManifest(array $manifest): void
100
    {
101
        $this->manifest = $manifest;
102
    }
103
104
    /**
105
     * Get 使用的样式
106
     *
107
     * @return  string
108
     */
109
    public function getStyle()
110
    {
111
        return $this->style;
112
    }
113
114
    /**
115
     * Set 使用的样式
116
     *
117
     * @param string $style 使用的样式
118
     *
119
     * @return  self
120
     */
121
    public function setStyle(string $style)
122
    {
123
        $this->style = $style;
124
125
        return $this;
126
    }
127
128
    /**
129
     * @return string
130
     */
131
    public function getLocate(): string
132
    {
133
        return $this->locate;
134
    }
135
136
    /**
137
     * @param string $locate
138
     */
139
    public function setLocate(string $locate): void
140
    {
141
        $this->locate = $locate;
142
    }
143
144
145
    /**
146
     * Get 配置数组
147
     *
148
     * @param string|null $name
149
     * @param mixed $default
150
     * @return  mixed
151
     */
152
    public function getManifest(string $name = null, $default = null)
153
    {
154
        if ($name !== null) {
155
            return ArrayDotAccess::get($this->manifest, $name, $default);
156
        }
157
        return $this->manifest;
158
    }
159
160
    /**
161
     * Get 数据源
162
     *
163
     * @return  ApplicationResource
164
     */
165
    public function getResource(): ApplicationResource
166
    {
167
        return $this->resource;
168
    }
169
170
    /**
171
     * Set 数据源
172
     *
173
     * @param ApplicationResource $resource 数据源
174
     *
175
     * @return  self
176
     */
177
    public function setResource(ApplicationResource $resource)
178
    {
179
        $this->resource = $resource;
180
181
        return $this;
182
    }
183
184
    /**
185
     * Get 数据源
186
     *
187
     * @return  DataSource
188
     */
189
    public function getDataSource()
190
    {
191
        return $this->dataSource;
192
    }
193
194
    /**
195
     * Set 数据源
196
     *
197
     * @param  DataSource  $dataSource  数据源
198
     *
199
     * @return  self
200
     */
201
    public function setDataSource(DataSource $dataSource)
202
    {
203
        $this->dataSource = $dataSource;
204
205
        return $this;
206
    }
207
208
    /**
209
     * Get 应用路径
210
     *
211
     * @return  string
212
     */
213
    public function getPath()
214
    {
215
        return $this->path;
216
    }
217
218
    /**
219
     * Get 数据路径
220
     *
221
     * @return  string
222
     */
223
    public function getDataPath()
224
    {
225
        return $this->dataPath;
226
    }
227
228
    /**
229
     * Set 数据路径
230
     *
231
     * @param string $dataPath 数据路径
232
     *
233
     * @return  self
234
     */
235
    public function setDataPath(string $dataPath)
236
    {
237
        $this->dataPath = $dataPath;
238
239
        return $this;
240
    }
241
}
242