1
|
|
|
<?php |
2
|
|
|
namespace suda\framework; |
3
|
|
|
|
4
|
|
|
use function is_array; |
5
|
|
|
use suda\framework\loader\Loader; |
6
|
|
|
use suda\framework\cache\FileCache; |
7
|
|
|
use suda\framework\context\PHPContext; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* 环境容器 |
11
|
|
|
*/ |
12
|
|
|
class Context extends PHPContext |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* 事件监听器 |
16
|
|
|
* |
17
|
|
|
* @var Event |
18
|
|
|
*/ |
19
|
|
|
protected $event; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* 路由匹配工具 |
23
|
|
|
* |
24
|
|
|
* @var Route |
25
|
|
|
*/ |
26
|
|
|
protected $route; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* 缓存 |
30
|
|
|
* |
31
|
|
|
* @var Cache |
32
|
|
|
*/ |
33
|
|
|
protected $cache; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* PHP错误调试 |
37
|
|
|
* |
38
|
|
|
* @var Debugger |
39
|
|
|
*/ |
40
|
|
|
protected $debug; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* 创建PHP环境 |
44
|
|
|
* |
45
|
|
|
* @param Config $config |
46
|
|
|
* @param Loader $loader |
47
|
|
|
* @param Event|null $event |
48
|
|
|
* @param Route|null $route |
49
|
|
|
*/ |
50
|
|
|
public function __construct(Config $config, Loader $loader, ?Event $event = null, ?Route $route = null) |
51
|
|
|
{ |
52
|
|
|
parent::__construct($config, $loader); |
53
|
|
|
$this->event = $event ?: new Event; |
54
|
|
|
$this->route = $route ?: new Route; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* 获取路由 |
59
|
|
|
* |
60
|
|
|
* @return Route |
61
|
|
|
*/ |
62
|
|
|
public function route():Route |
63
|
|
|
{ |
64
|
|
|
return $this->route; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* 获取缓存 |
69
|
|
|
* |
70
|
|
|
* @return Cache |
71
|
|
|
*/ |
72
|
|
|
public function cache(): Cache |
73
|
|
|
{ |
74
|
|
|
if ($this->cache === null) { |
75
|
|
|
$this->setCache($this->getDefaultCache()); |
76
|
|
|
} |
77
|
|
|
return $this->cache; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* 获取事件 |
82
|
|
|
* |
83
|
|
|
* @return Event |
84
|
|
|
*/ |
85
|
|
|
public function event():Event |
86
|
|
|
{ |
87
|
|
|
return $this->event; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* 创建Cache |
92
|
|
|
* |
93
|
|
|
* @param string $cacheName |
94
|
|
|
* @param array $cacheConfig |
95
|
|
|
* @return Cache |
96
|
|
|
*/ |
97
|
|
|
protected function createCacheFrom(string $cacheName, array $cacheConfig):Cache |
98
|
|
|
{ |
99
|
|
|
return new $cacheName($cacheConfig); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* 获取默认缓存 |
104
|
|
|
* |
105
|
|
|
* @return Cache |
106
|
|
|
*/ |
107
|
|
|
protected function getDefaultCache():Cache |
108
|
|
|
{ |
109
|
|
|
return $this->createCacheFrom($this->conf('cache.class', FileCache::class), $this->conf('cache', [])); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Get 事件监听器 |
114
|
|
|
* |
115
|
|
|
* @return Event |
116
|
|
|
*/ |
117
|
|
|
public function getEvent() |
118
|
|
|
{ |
119
|
|
|
return $this->event; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set 事件监听器 |
124
|
|
|
* |
125
|
|
|
* @param Event $event 事件监听器 |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
|
|
public function setEvent(Event $event) |
130
|
|
|
{ |
131
|
|
|
$this->event = $event; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get 缓存 |
138
|
|
|
* |
139
|
|
|
* @return Cache |
140
|
|
|
*/ |
141
|
|
|
public function getCache() |
142
|
|
|
{ |
143
|
|
|
return $this->cache(); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set 缓存 |
148
|
|
|
* |
149
|
|
|
* @param Cache $cache 缓存 |
150
|
|
|
* |
151
|
|
|
* @return self |
152
|
|
|
*/ |
153
|
|
|
public function setCache(Cache $cache) |
154
|
|
|
{ |
155
|
|
|
$this->cache = $cache; |
156
|
|
|
|
157
|
|
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* 加载事件 |
162
|
|
|
* |
163
|
|
|
* @param string $path |
164
|
|
|
* @return void |
165
|
|
|
*/ |
166
|
|
|
public function loadEvent(string $path) |
167
|
|
|
{ |
168
|
|
|
$listener = Config::loadConfig($path, $this->config); |
169
|
|
|
if (is_array($listener)) { |
170
|
|
|
$this->event->load($listener); |
171
|
|
|
} |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Get 路由匹配工具 |
176
|
|
|
* |
177
|
|
|
* @return Route |
178
|
|
|
*/ |
179
|
|
|
public function getRoute() |
180
|
|
|
{ |
181
|
|
|
return $this->route; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Set 路由匹配工具 |
186
|
|
|
* |
187
|
|
|
* @param Route $route 路由匹配工具 |
188
|
|
|
* |
189
|
|
|
* @return self |
190
|
|
|
*/ |
191
|
|
|
public function setRoute(Route $route) |
192
|
|
|
{ |
193
|
|
|
$this->route = $route; |
194
|
|
|
|
195
|
|
|
return $this; |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get pHP错误调试 |
200
|
|
|
* |
201
|
|
|
* @return Debugger |
202
|
|
|
*/ |
203
|
|
|
public function getDebug() |
204
|
|
|
{ |
205
|
|
|
return $this->debug; |
206
|
|
|
} |
207
|
|
|
|
208
|
|
|
/** |
209
|
|
|
* Set PHP错误调试 |
210
|
|
|
* |
211
|
|
|
* @param Debugger $debug PHP错误调试 |
212
|
|
|
* |
213
|
|
|
* @return self |
214
|
|
|
*/ |
215
|
|
|
public function setDebug(Debugger $debug) |
216
|
|
|
{ |
217
|
|
|
$this->debug = $debug; |
218
|
|
|
return $this; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
/** |
222
|
|
|
* 获取调试工具 |
223
|
|
|
* |
224
|
|
|
* @return Debugger |
225
|
|
|
*/ |
226
|
|
|
public function debug(): Debugger |
227
|
|
|
{ |
228
|
|
|
return $this->debug; |
229
|
|
|
} |
230
|
|
|
} |
231
|
|
|
|