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