forked from illuminate/routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathResponseFactory.php
209 lines (188 loc) · 5.68 KB
/
ResponseFactory.php
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
208
209
<?php namespace Illuminate\Routing;
use Illuminate\Support\Str;
use Illuminate\Http\Response;
use Illuminate\Http\JsonResponse;
use Illuminate\Contracts\Support\Arrayable;
use Illuminate\Support\Traits\MacroableTrait;
use Illuminate\Contracts\View\Factory as ViewFactory;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Illuminate\Contracts\Routing\ResponseFactory as FactoryContract;
class ResponseFactory implements FactoryContract {
use MacroableTrait;
/**
* The view factory implementation.
*
* @var \Illuminate\Contracts\View\Factory
*/
protected $view;
/**
* The redirector instance.
*
* @var \Illuminate\Routing\Redirector
*/
protected $redirector;
/**
* Create a new response factory instance.
*
* @param \Illuminate\Contracts\View\Factory $view
* @param \Illuminate\Routing\Redirector $redirector
* @return void
*/
public function __construct(ViewFactory $view, Redirector $redirector)
{
$this->view = $view;
$this->redirector = $redirector;
}
/**
* Return a new response from the application.
*
* @param string $content
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\Response
*/
public function make($content = '', $status = 200, array $headers = array())
{
return new Response($content, $status, $headers);
}
/**
* Return a new view response from the application.
*
* @param string $view
* @param array $data
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\Response
*/
public function view($view, $data = array(), $status = 200, array $headers = array())
{
return static::make($this->view->make($view, $data), $status, $headers);
}
/**
* Return a new JSON response from the application.
*
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Symfony\Component\HttpFoundation\Response
*/
public function json($data = array(), $status = 200, array $headers = array(), $options = 0)
{
if ($data instanceof Arrayable)
{
$data = $data->toArray();
}
return new JsonResponse($data, $status, $headers, $options);
}
/**
* Return a new JSONP response from the application.
*
* @param string $callback
* @param string|array $data
* @param int $status
* @param array $headers
* @param int $options
* @return \Symfony\Component\HttpFoundation\Response
*/
public function jsonp($callback, $data = array(), $status = 200, array $headers = array(), $options = 0)
{
return $this->json($data, $status, $headers, $options)->setCallback($callback);
}
/**
* Return a new streamed response from the application.
*
* @param \Closure $callback
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\StreamedResponse
*/
public function stream($callback, $status = 200, array $headers = array())
{
return new StreamedResponse($callback, $status, $headers);
}
/**
* Create a new file download response.
*
* @param \SplFileInfo|string $file
* @param string $name
* @param array $headers
* @param null|string $disposition
* @return \Symfony\Component\HttpFoundation\BinaryFileResponse
*/
public function download($file, $name = null, array $headers = array(), $disposition = 'attachment')
{
$response = new BinaryFileResponse($file, 200, $headers, true, $disposition);
if ( ! is_null($name))
{
return $response->setContentDisposition($disposition, $name, Str::ascii($name));
}
return $response;
}
/**
* Create a new redirect response to the given path.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Symfony\Component\HttpFoundation\Response
*/
public function redirectTo($path, $status = 302, $headers = array(), $secure = null)
{
return $this->redirector->to($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to a named route.
*
* @param string $route
* @param array $parameters
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\Response
*/
public function redirectToRoute($route, $parameters = array(), $status = 302, $headers = array())
{
return $this->redirector->route($route, $parameters, $status, $headers);
}
/**
* Create a new redirect response to a controller action.
*
* @param string $action
* @param array $parameters
* @param int $status
* @param array $headers
* @return \Symfony\Component\HttpFoundation\Response
*/
public function redirectToAction($action, $parameters = array(), $status = 302, $headers = array())
{
return $this->redirector->action($action, $parameters, $status, $headers);
}
/**
* Create a new redirect response, while putting the current URL in the session.
*
* @param string $path
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Symfony\Component\HttpFoundation\Response
*/
public function redirectGuest($path, $status = 302, $headers = array(), $secure = null)
{
return $this->redirector->guest($path, $status, $headers, $secure);
}
/**
* Create a new redirect response to the previously intended location.
*
* @param string $default
* @param int $status
* @param array $headers
* @param bool $secure
* @return \Symfony\Component\HttpFoundation\Response
*/
public function redirectToIntended($default = '/', $status = 302, $headers = array(), $secure = null)
{
return $this->redirector->intended($default, $status, $headers, $secure);
}
}