-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasControllerAble.php
More file actions
207 lines (183 loc) · 4.63 KB
/
Copy pathHasControllerAble.php
File metadata and controls
207 lines (183 loc) · 4.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
<?php
declare(strict_types=1);
/**
* This file is part of Coole.
*
* @link https://github.com/guanguans/coole
* @contact guanguans <ityaozm@gmail.com>
* @license https://github.com/guanguans/coole/blob/main/LICENSE
*/
namespace Coole\HttpKernel\Controller;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpKernel\Exception\HttpException;
use Symfony\Component\HttpKernel\KernelEvents;
trait HasControllerAble
{
/**
* 中间件.
*
* @var array
*/
protected $middleware = [];
/**
* 排除的中间件.
*
* @var array
*/
protected $excludedMiddleware = [];
/**
* {@inheritdoc}
*/
public function render($name, $context = []): string
{
return app('view')->render($name, $context);
}
/**
* 重定 url.
*
* @param $url
* @param int $status
*/
public function redirect($url, $status = 302, array $headers = []): RedirectResponse
{
return new RedirectResponse($url, $status, $headers);
}
/**
* 抛出 http 异常.
*
* @param $statusCode
* @param string $message
*/
public function abort($statusCode, $message = '', array $headers = [])
{
throw new HttpException($statusCode, $message, null, $headers);
}
/**
* 返回流响应.
*
* @param null $callback
* @param int $status
*/
public function stream($callback = null, $status = 200, array $headers = []): StreamedResponse
{
return new StreamedResponse($callback, $status, $headers);
}
/**
* 返回 json 响应.
*
* @param array $data
* @param int $status
*/
public function json($data = [], $status = 200, array $headers = []): JsonResponse
{
return new JsonResponse($data, $status, $headers);
}
/**
* 返回二进制文件响应.
*
* @param $file
* @param int $status
* @param null $contentDisposition
*/
public function sendFile($file, $status = 200, array $headers = [], $contentDisposition = null): BinaryFileResponse
{
return new BinaryFileResponse($file, $status, $headers, true, $contentDisposition);
}
/**
* 获取中间件.
*/
public function getMiddleware(): array
{
return $this->middleware;
}
/**
* 设置中间件.
*
* @param $middleware
*
* @return $this
*/
public function setMiddleware($middleware): self
{
return $this->addMiddleware($middleware);
}
/**
* 添加中间件.
*
* @param $middleware
*
* @return $this
*/
public function addMiddleware($middleware): self
{
$this->middleware = array_unique(array_merge($this->middleware, (array) $middleware));
return $this;
}
/**
* 获取排除的中间件.
*/
public function getExcludedMiddleware(): array
{
return $this->excludedMiddleware;
}
/**
* 排除中间件.
*
* @param $excludedMiddleware
*
* @return $this
*/
public function withoutMiddleware($excludedMiddleware): self
{
return $this->addExcludedMiddleware($excludedMiddleware);
}
/**
* 设置排除的中间件.
*
* @param $excludedMiddleware
*
* @return $this
*/
public function setExcludedMiddleware($excludedMiddleware): self
{
return $this->addExcludedMiddleware($excludedMiddleware);
}
/**
* 添加排除的中间件.
*
* @param $excludedMiddleware
*
* @return $this
*/
public function addExcludedMiddleware($excludedMiddleware): self
{
$this->excludedMiddleware = array_unique(array_merge($this->excludedMiddleware, (array) $excludedMiddleware));
return $this;
}
/**
* 添加一个 `KernelEvents::TERMINATE` 事件监听处理器.
*
* 用来处理耗时逻辑业务
*
* @param callable $listener
*/
public function addFinishHandler($listener, int $priority = 0)
{
app('event_dispatcher')->addListener(KernelEvents::TERMINATE, $listener, $priority);
return $this;
}
/**
* 设置一个 `KernelEvents::TERMINATE` 事件监听处理器.
*
* 用来处理耗时逻辑业务
*
* @param callable $listener
*/
public function setFinishHandler($listener, int $priority = 0)
{
return $this->addFinishHandler($listener, $priority);
}
}