|
1
|
|
|
<?php |
|
2
|
|
|
namespace JayaCode\Framework\Core\Http; |
|
3
|
|
|
|
|
4
|
|
|
use Symfony\Component\HttpFoundation\Request as BaseRequest; |
|
5
|
|
|
|
|
6
|
|
|
class Request extends BaseRequest |
|
7
|
|
|
{ |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Get the current path info for the request. |
|
11
|
|
|
* |
|
12
|
|
|
* @return string |
|
13
|
|
|
*/ |
|
14
|
|
|
public function path() |
|
15
|
|
|
{ |
|
16
|
|
|
$pattern = trim($this->getPathInfo(), '/'); |
|
17
|
|
|
return $pattern == '' ? '/' : $pattern; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Get the request method. |
|
22
|
|
|
* |
|
23
|
|
|
* @return string |
|
24
|
|
|
*/ |
|
25
|
|
|
public function method() |
|
26
|
|
|
{ |
|
27
|
|
|
return $this->getMethod(); |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* Get the root URL |
|
32
|
|
|
* |
|
33
|
|
|
* @return string |
|
34
|
|
|
*/ |
|
35
|
|
|
public function rootURL() |
|
36
|
|
|
{ |
|
37
|
|
|
return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/'); |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Return true if server HTTP_REFERER isset |
|
42
|
|
|
* |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function hasRefererURL() |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->server->has("HTTP_REFERER"); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* Return server HTTP_REFERER |
|
52
|
|
|
* |
|
53
|
|
|
* @return string |
|
54
|
|
|
*/ |
|
55
|
|
|
public function refererURL() |
|
56
|
|
|
{ |
|
57
|
|
|
return $this->server->get("HTTP_REFERER"); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** |
|
61
|
|
|
* Return IP address client |
|
62
|
|
|
* |
|
63
|
|
|
* @return string |
|
64
|
|
|
*/ |
|
65
|
|
|
public function ip() |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->getClientIp(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Return IP address client |
|
72
|
|
|
* |
|
73
|
|
|
* @return array |
|
74
|
|
|
*/ |
|
75
|
|
|
public function ipAll() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->getClientIps(); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param array $query |
|
82
|
|
|
* @param array $request |
|
83
|
|
|
* @param array $attributes |
|
84
|
|
|
* @param array $cookies |
|
85
|
|
|
* @param array $files |
|
86
|
|
|
* @param array $server |
|
87
|
|
|
* @param null $content |
|
88
|
|
|
* @return array|mixed|static |
|
89
|
|
|
*/ |
|
90
|
|
|
private static function createRequestFromFactory( |
|
|
|
|
|
|
91
|
|
|
array $query = array(), |
|
92
|
|
|
array $request = array(), |
|
93
|
|
|
array $attributes = array(), |
|
94
|
|
|
array $cookies = array(), |
|
95
|
|
|
array $files = array(), |
|
96
|
|
|
array $server = array(), |
|
97
|
|
|
$content = null |
|
98
|
|
|
) { |
|
99
|
|
|
|
|
100
|
|
|
if (self::$requestFactory) { |
|
101
|
|
|
$request = call_user_func( |
|
102
|
|
|
self::$requestFactory, |
|
103
|
|
|
$query, |
|
104
|
|
|
$request, |
|
105
|
|
|
$attributes, |
|
106
|
|
|
$cookies, |
|
107
|
|
|
$files, |
|
108
|
|
|
$server, |
|
109
|
|
|
$content |
|
110
|
|
|
); |
|
111
|
|
|
|
|
112
|
|
|
if (!$request instanceof self) { |
|
113
|
|
|
$message = 'The Request factory must return an instance of Symfony\Component\HttpFoundation\Request.'; |
|
114
|
|
|
throw new \LogicException($message); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
return $request; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
return new static($query, $request, $attributes, $cookies, $files, $server, $content); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Creates a new request with values from PHP's super globals. |
|
125
|
|
|
* |
|
126
|
|
|
* @return Request A new request |
|
127
|
|
|
*/ |
|
128
|
|
|
public static function createFromGlobals() |
|
|
|
|
|
|
129
|
|
|
{ |
|
130
|
|
|
// With the php's bug #66606, the php's built-in web server |
|
131
|
|
|
// stores the Content-Type and Content-Length header values in |
|
132
|
|
|
// HTTP_CONTENT_TYPE and HTTP_CONTENT_LENGTH fields. |
|
133
|
|
|
$server = $_SERVER; |
|
134
|
|
|
if ('cli-server' === PHP_SAPI) { |
|
135
|
|
|
if (array_key_exists('HTTP_CONTENT_LENGTH', $_SERVER)) { |
|
136
|
|
|
$server['CONTENT_LENGTH'] = $_SERVER['HTTP_CONTENT_LENGTH']; |
|
137
|
|
|
} |
|
138
|
|
|
if (array_key_exists('HTTP_CONTENT_TYPE', $_SERVER)) { |
|
139
|
|
|
$server['CONTENT_TYPE'] = $_SERVER['HTTP_CONTENT_TYPE']; |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
$request = self::createRequestFromFactory($_GET, $_POST, array(), $_COOKIE, $_FILES, $server); |
|
144
|
|
|
|
|
145
|
|
|
if (0 === strpos($request->headers->get('CONTENT_TYPE'), 'application/x-www-form-urlencoded') |
|
146
|
|
|
&& in_array(strtoupper($request->server->get('REQUEST_METHOD', 'GET')), array('PUT', 'DELETE', 'PATCH')) |
|
147
|
|
|
) { |
|
148
|
|
|
parse_str($request->getContent(), $data); |
|
149
|
|
|
$request->request = new ParameterBag($data); |
|
|
|
|
|
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
return $request; |
|
153
|
|
|
} |
|
154
|
|
|
} |
|
155
|
|
|
|
Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.