|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Nip\Controllers\Response; |
|
4
|
|
|
|
|
5
|
|
|
use Nip\Http\Response\JsonResponse; |
|
6
|
|
|
use Nip\Http\Response\Response; |
|
7
|
|
|
use Symfony\Component\HttpFoundation\BinaryFileResponse; |
|
8
|
|
|
use Symfony\Component\HttpFoundation\ResponseHeaderBag; |
|
9
|
|
|
use Symfony\Component\HttpFoundation\StreamedResponse; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Class ResponseFactory |
|
13
|
|
|
* @package Nip\Controllers\Response |
|
14
|
|
|
*/ |
|
15
|
|
|
class ResponseFactory |
|
16
|
|
|
{ |
|
17
|
|
|
use ResponseFactory\HasViewResponseTrait; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Create a new response instance. |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $content |
|
23
|
|
|
* @param int $status |
|
24
|
|
|
* @param array $headers |
|
25
|
|
|
* @return Response |
|
26
|
|
|
*/ |
|
27
|
2 |
|
public function make($content = '', $status = 200, array $headers = []) |
|
28
|
|
|
{ |
|
29
|
2 |
|
return new Response($content, $status, $headers); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Create a new "no content" response. |
|
34
|
|
|
* |
|
35
|
|
|
* @param int $status |
|
36
|
|
|
* @param array $headers |
|
37
|
|
|
* @return Response |
|
38
|
|
|
*/ |
|
39
|
1 |
|
public function noContent($status = Response::HTTP_NO_CONTENT, array $headers = []) |
|
40
|
|
|
{ |
|
41
|
1 |
|
return $this->make('', $status, $headers); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* Create a new JSON response instance. |
|
46
|
|
|
* |
|
47
|
|
|
* @param mixed $data |
|
48
|
|
|
* @param int $status |
|
49
|
|
|
* @param array $headers |
|
50
|
|
|
* @param int $options |
|
51
|
|
|
* @return JsonResponse |
|
52
|
|
|
*/ |
|
53
|
|
|
public function json($data = [], $status = 200, array $headers = [], $options = 0) |
|
54
|
|
|
{ |
|
55
|
|
|
return new JsonResponse($data, $status, $headers, $options); |
|
|
|
|
|
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* Create a new streamed response instance. |
|
60
|
|
|
* |
|
61
|
|
|
* @param \Closure $callback |
|
62
|
|
|
* @param int $status |
|
63
|
|
|
* @param array $headers |
|
64
|
|
|
* @return StreamedResponse |
|
65
|
|
|
*/ |
|
66
|
|
|
public function stream($callback, $status = 200, array $headers = []) |
|
67
|
|
|
{ |
|
68
|
|
|
return new StreamedResponse($callback, $status, $headers); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Returns a BinaryFileResponse object with original or customized file name and disposition header. |
|
73
|
|
|
* |
|
74
|
|
|
* @param \SplFileInfo|string $file File object or path to file to be sent as response |
|
75
|
|
|
* @param string|null $fileName |
|
76
|
|
|
* @param string $disposition |
|
77
|
|
|
* @return BinaryFileResponse |
|
78
|
|
|
*/ |
|
79
|
|
|
protected function file( |
|
80
|
|
|
$file, |
|
81
|
|
|
string $fileName = null, |
|
82
|
|
|
string $disposition = ResponseHeaderBag::DISPOSITION_ATTACHMENT |
|
83
|
|
|
): BinaryFileResponse { |
|
84
|
|
|
$response = new BinaryFileResponse($file); |
|
85
|
|
|
$response->setContentDisposition($disposition, |
|
86
|
|
|
null === $fileName ? $response->getFile()->getFilename() : $fileName); |
|
87
|
|
|
|
|
88
|
|
|
return $response; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* Convert the string to ASCII characters that are equivalent to the given name. |
|
93
|
|
|
* |
|
94
|
|
|
* @param string $name |
|
95
|
|
|
* @return string |
|
96
|
|
|
*/ |
|
97
|
|
|
protected function fallbackName($name) |
|
98
|
|
|
{ |
|
99
|
|
|
return str_replace('%', '', Str::ascii($name)); |
|
|
|
|
|
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|