| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Routing;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Contracts\Routing\ResponseFactory as FactoryContract;
|
|
|
6 |
use Illuminate\Contracts\View\Factory as ViewFactory;
|
|
|
7 |
use Illuminate\Http\JsonResponse;
|
|
|
8 |
use Illuminate\Http\Response;
|
|
|
9 |
use Illuminate\Routing\Exceptions\StreamedResponseException;
|
|
|
10 |
use Illuminate\Support\Str;
|
|
|
11 |
use Illuminate\Support\Traits\Macroable;
|
|
|
12 |
use Symfony\Component\HttpFoundation\BinaryFileResponse;
|
|
|
13 |
use Symfony\Component\HttpFoundation\StreamedResponse;
|
|
|
14 |
use Throwable;
|
|
|
15 |
|
|
|
16 |
class ResponseFactory implements FactoryContract
|
|
|
17 |
{
|
|
|
18 |
use Macroable;
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
* The view factory instance.
|
|
|
22 |
*
|
|
|
23 |
* @var \Illuminate\Contracts\View\Factory
|
|
|
24 |
*/
|
|
|
25 |
protected $view;
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* The redirector instance.
|
|
|
29 |
*
|
|
|
30 |
* @var \Illuminate\Routing\Redirector
|
|
|
31 |
*/
|
|
|
32 |
protected $redirector;
|
|
|
33 |
|
|
|
34 |
/**
|
|
|
35 |
* Create a new response factory instance.
|
|
|
36 |
*
|
|
|
37 |
* @param \Illuminate\Contracts\View\Factory $view
|
|
|
38 |
* @param \Illuminate\Routing\Redirector $redirector
|
|
|
39 |
* @return void
|
|
|
40 |
*/
|
|
|
41 |
public function __construct(ViewFactory $view, Redirector $redirector)
|
|
|
42 |
{
|
|
|
43 |
$this->view = $view;
|
|
|
44 |
$this->redirector = $redirector;
|
|
|
45 |
}
|
|
|
46 |
|
|
|
47 |
/**
|
|
|
48 |
* Create a new response instance.
|
|
|
49 |
*
|
|
|
50 |
* @param mixed $content
|
|
|
51 |
* @param int $status
|
|
|
52 |
* @param array $headers
|
|
|
53 |
* @return \Illuminate\Http\Response
|
|
|
54 |
*/
|
|
|
55 |
public function make($content = '', $status = 200, array $headers = [])
|
|
|
56 |
{
|
|
|
57 |
return new Response($content, $status, $headers);
|
|
|
58 |
}
|
|
|
59 |
|
|
|
60 |
/**
|
|
|
61 |
* Create a new "no content" response.
|
|
|
62 |
*
|
|
|
63 |
* @param int $status
|
|
|
64 |
* @param array $headers
|
|
|
65 |
* @return \Illuminate\Http\Response
|
|
|
66 |
*/
|
|
|
67 |
public function noContent($status = 204, array $headers = [])
|
|
|
68 |
{
|
|
|
69 |
return $this->make('', $status, $headers);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
* Create a new response for a given view.
|
|
|
74 |
*
|
|
|
75 |
* @param string|array $view
|
|
|
76 |
* @param array $data
|
|
|
77 |
* @param int $status
|
|
|
78 |
* @param array $headers
|
|
|
79 |
* @return \Illuminate\Http\Response
|
|
|
80 |
*/
|
|
|
81 |
public function view($view, $data = [], $status = 200, array $headers = [])
|
|
|
82 |
{
|
|
|
83 |
if (is_array($view)) {
|
|
|
84 |
return $this->make($this->view->first($view, $data), $status, $headers);
|
|
|
85 |
}
|
|
|
86 |
|
|
|
87 |
return $this->make($this->view->make($view, $data), $status, $headers);
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
/**
|
|
|
91 |
* Create a new JSON response instance.
|
|
|
92 |
*
|
|
|
93 |
* @param mixed $data
|
|
|
94 |
* @param int $status
|
|
|
95 |
* @param array $headers
|
|
|
96 |
* @param int $options
|
|
|
97 |
* @return \Illuminate\Http\JsonResponse
|
|
|
98 |
*/
|
|
|
99 |
public function json($data = [], $status = 200, array $headers = [], $options = 0)
|
|
|
100 |
{
|
|
|
101 |
return new JsonResponse($data, $status, $headers, $options);
|
|
|
102 |
}
|
|
|
103 |
|
|
|
104 |
/**
|
|
|
105 |
* Create a new JSONP response instance.
|
|
|
106 |
*
|
|
|
107 |
* @param string $callback
|
|
|
108 |
* @param mixed $data
|
|
|
109 |
* @param int $status
|
|
|
110 |
* @param array $headers
|
|
|
111 |
* @param int $options
|
|
|
112 |
* @return \Illuminate\Http\JsonResponse
|
|
|
113 |
*/
|
|
|
114 |
public function jsonp($callback, $data = [], $status = 200, array $headers = [], $options = 0)
|
|
|
115 |
{
|
|
|
116 |
return $this->json($data, $status, $headers, $options)->setCallback($callback);
|
|
|
117 |
}
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
* Create a new streamed response instance.
|
|
|
121 |
*
|
| 150 |
lars |
122 |
* @param callable $callback
|
| 148 |
lars |
123 |
* @param int $status
|
|
|
124 |
* @param array $headers
|
|
|
125 |
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
|
|
126 |
*/
|
|
|
127 |
public function stream($callback, $status = 200, array $headers = [])
|
|
|
128 |
{
|
|
|
129 |
return new StreamedResponse($callback, $status, $headers);
|
|
|
130 |
}
|
|
|
131 |
|
|
|
132 |
/**
|
|
|
133 |
* Create a new streamed response instance as a file download.
|
|
|
134 |
*
|
| 150 |
lars |
135 |
* @param callable $callback
|
| 148 |
lars |
136 |
* @param string|null $name
|
|
|
137 |
* @param array $headers
|
|
|
138 |
* @param string|null $disposition
|
|
|
139 |
* @return \Symfony\Component\HttpFoundation\StreamedResponse
|
|
|
140 |
*/
|
|
|
141 |
public function streamDownload($callback, $name = null, array $headers = [], $disposition = 'attachment')
|
|
|
142 |
{
|
|
|
143 |
$withWrappedException = function () use ($callback) {
|
|
|
144 |
try {
|
|
|
145 |
$callback();
|
|
|
146 |
} catch (Throwable $e) {
|
|
|
147 |
throw new StreamedResponseException($e);
|
|
|
148 |
}
|
|
|
149 |
};
|
|
|
150 |
|
|
|
151 |
$response = new StreamedResponse($withWrappedException, 200, $headers);
|
|
|
152 |
|
|
|
153 |
if (! is_null($name)) {
|
|
|
154 |
$response->headers->set('Content-Disposition', $response->headers->makeDisposition(
|
|
|
155 |
$disposition,
|
|
|
156 |
$name,
|
|
|
157 |
$this->fallbackName($name)
|
|
|
158 |
));
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
return $response;
|
|
|
162 |
}
|
|
|
163 |
|
|
|
164 |
/**
|
|
|
165 |
* Create a new file download response.
|
|
|
166 |
*
|
|
|
167 |
* @param \SplFileInfo|string $file
|
|
|
168 |
* @param string|null $name
|
|
|
169 |
* @param array $headers
|
|
|
170 |
* @param string|null $disposition
|
|
|
171 |
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
|
|
172 |
*/
|
|
|
173 |
public function download($file, $name = null, array $headers = [], $disposition = 'attachment')
|
|
|
174 |
{
|
|
|
175 |
$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);
|
|
|
176 |
|
|
|
177 |
if (! is_null($name)) {
|
|
|
178 |
return $response->setContentDisposition($disposition, $name, $this->fallbackName($name));
|
|
|
179 |
}
|
|
|
180 |
|
|
|
181 |
return $response;
|
|
|
182 |
}
|
|
|
183 |
|
|
|
184 |
/**
|
|
|
185 |
* Convert the string to ASCII characters that are equivalent to the given name.
|
|
|
186 |
*
|
|
|
187 |
* @param string $name
|
|
|
188 |
* @return string
|
|
|
189 |
*/
|
|
|
190 |
protected function fallbackName($name)
|
|
|
191 |
{
|
|
|
192 |
return str_replace('%', '', Str::ascii($name));
|
|
|
193 |
}
|
|
|
194 |
|
|
|
195 |
/**
|
|
|
196 |
* Return the raw contents of a binary file.
|
|
|
197 |
*
|
|
|
198 |
* @param \SplFileInfo|string $file
|
|
|
199 |
* @param array $headers
|
|
|
200 |
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
|
|
|
201 |
*/
|
|
|
202 |
public function file($file, array $headers = [])
|
|
|
203 |
{
|
|
|
204 |
return new BinaryFileResponse($file, 200, $headers);
|
|
|
205 |
}
|
|
|
206 |
|
|
|
207 |
/**
|
|
|
208 |
* Create a new redirect response to the given path.
|
|
|
209 |
*
|
|
|
210 |
* @param string $path
|
|
|
211 |
* @param int $status
|
|
|
212 |
* @param array $headers
|
|
|
213 |
* @param bool|null $secure
|
|
|
214 |
* @return \Illuminate\Http\RedirectResponse
|
|
|
215 |
*/
|
|
|
216 |
public function redirectTo($path, $status = 302, $headers = [], $secure = null)
|
|
|
217 |
{
|
|
|
218 |
return $this->redirector->to($path, $status, $headers, $secure);
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Create a new redirect response to a named route.
|
|
|
223 |
*
|
|
|
224 |
* @param string $route
|
|
|
225 |
* @param mixed $parameters
|
|
|
226 |
* @param int $status
|
|
|
227 |
* @param array $headers
|
|
|
228 |
* @return \Illuminate\Http\RedirectResponse
|
|
|
229 |
*/
|
|
|
230 |
public function redirectToRoute($route, $parameters = [], $status = 302, $headers = [])
|
|
|
231 |
{
|
|
|
232 |
return $this->redirector->route($route, $parameters, $status, $headers);
|
|
|
233 |
}
|
|
|
234 |
|
|
|
235 |
/**
|
|
|
236 |
* Create a new redirect response to a controller action.
|
|
|
237 |
*
|
|
|
238 |
* @param string $action
|
|
|
239 |
* @param mixed $parameters
|
|
|
240 |
* @param int $status
|
|
|
241 |
* @param array $headers
|
|
|
242 |
* @return \Illuminate\Http\RedirectResponse
|
|
|
243 |
*/
|
|
|
244 |
public function redirectToAction($action, $parameters = [], $status = 302, $headers = [])
|
|
|
245 |
{
|
|
|
246 |
return $this->redirector->action($action, $parameters, $status, $headers);
|
|
|
247 |
}
|
|
|
248 |
|
|
|
249 |
/**
|
|
|
250 |
* Create a new redirect response, while putting the current URL in the session.
|
|
|
251 |
*
|
|
|
252 |
* @param string $path
|
|
|
253 |
* @param int $status
|
|
|
254 |
* @param array $headers
|
|
|
255 |
* @param bool|null $secure
|
|
|
256 |
* @return \Illuminate\Http\RedirectResponse
|
|
|
257 |
*/
|
|
|
258 |
public function redirectGuest($path, $status = 302, $headers = [], $secure = null)
|
|
|
259 |
{
|
|
|
260 |
return $this->redirector->guest($path, $status, $headers, $secure);
|
|
|
261 |
}
|
|
|
262 |
|
|
|
263 |
/**
|
|
|
264 |
* Create a new redirect response to the previously intended location.
|
|
|
265 |
*
|
|
|
266 |
* @param string $default
|
|
|
267 |
* @param int $status
|
|
|
268 |
* @param array $headers
|
|
|
269 |
* @param bool|null $secure
|
|
|
270 |
* @return \Illuminate\Http\RedirectResponse
|
|
|
271 |
*/
|
|
|
272 |
public function redirectToIntended($default = '/', $status = 302, $headers = [], $secure = null)
|
|
|
273 |
{
|
|
|
274 |
return $this->redirector->intended($default, $status, $headers, $secure);
|
|
|
275 |
}
|
|
|
276 |
}
|