This repository has been archived by the owner on Mar 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
functions.php
147 lines (134 loc) · 3.9 KB
/
functions.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
<?php
namespace Wandu\Http
{
use Wandu\Http\Factory\ResponseFactory;
/**
* @return \Wandu\Http\Factory\ResponseFactory
*/
function response()
{
if (!isset(ResponseFactory::$instance)) {
ResponseFactory::$instance = new ResponseFactory();
}
return ResponseFactory::$instance;
}
/**
* @reference https://gist.github.com/Mulkave/65daabb82752f9b9a0dd
* @param string $url
* @return array|boolean
*/
function parseUrl($url)
{
$parts = parse_url(preg_replace_callback('/[^:\/@?&=#]+/u', function ($matches) {
return urlencode($matches[0]);
}, $url));
if ($parts === false) {
return false;
}
foreach($parts as $name => $value) {
$parts[$name] = ($name === 'port') ? $value : urldecode($value);
}
return $parts;
}
}
namespace Wandu\Http\Response
{
use Closure;
use Generator;
use Psr\Http\Message\ServerRequestInterface;
use Traversable;
use Wandu\Http\Exception\BadRequestException;
use function Wandu\Http\response;
/**
* @param string $content
* @param int $status
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function create($content = null, $status = 200, array $headers = [])
{
return response()->create($content, $status, $headers);
}
/**
* @param \Closure $area
* @param int $status
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function capture(Closure $area, $status = 200, array $headers = [])
{
return response()->capture($area, $status, $headers);
}
/**
* @param string|array $data
* @param int $status
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function json($data = [], $status = 200, array $headers = [])
{
return response()->json($data, $status, $headers);
}
/**
* @param string $file
* @param string $name
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function download($file, $name = null, array $headers = [])
{
return response()->download($file, $name, $headers);
}
/**
* @param string $path
* @param array $queries
* @param int $status
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function redirect($path, $queries = [], $status = 302, $headers = [])
{
$parsedQueries = [];
foreach ($queries as $key => $value) {
$parsedQueries[] = "{$key}=" . urlencode($value);
}
if (count($parsedQueries)) {
$path .= '?' . implode('&', $parsedQueries);
}
return response()->redirect($path, $status, $headers);
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Psr\Http\Message\ResponseInterface
* @throws \Wandu\Http\Exception\BadRequestException
*/
function back(ServerRequestInterface $request)
{
if ($request->hasHeader('referer')) {
return redirect($request->getHeader('referer'));
}
throw new BadRequestException();
}
/**
* @deprecated use iterator
*
* @param \Generator $generator
* @param int $status
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function generator(Generator $generator, $status = 200, array $headers = [])
{
return response()->iterator($generator, $status, $headers);
}
/**
* @param \Traversable $iterator
* @param int $status
* @param array $headers
* @return \Psr\Http\Message\ResponseInterface
*/
function iterator(Traversable $iterator, $status = 200, array $headers = [])
{
return response()->iterator($iterator, $status, $headers);
}
}