1
|
|
|
<?php |
2
|
|
|
namespace JayaCode\Framework\Core\Http; |
3
|
|
|
|
4
|
|
|
use JayaCode\Framework\Core\Session\Session; |
5
|
|
|
use Symfony\Component\HttpFoundation\Request as BaseRequest; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class Request |
9
|
|
|
* @package JayaCode\Framework\Core\Http |
10
|
|
|
*/ |
11
|
|
|
class Request extends BaseRequest |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var Session |
15
|
|
|
*/ |
16
|
|
|
protected $session; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Get the current path info for the request. |
20
|
|
|
* |
21
|
|
|
* @return string |
22
|
|
|
*/ |
23
|
3 |
|
public function path() |
24
|
|
|
{ |
25
|
3 |
|
$pattern = trim($this->getPathInfo(), '/'); |
26
|
3 |
|
return $pattern == '' ? '/' : $pattern; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the request method. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
3 |
|
public function method() |
35
|
|
|
{ |
36
|
3 |
|
return $this->getMethod(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the root URL |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
3 |
|
public function rootURL() |
45
|
|
|
{ |
46
|
3 |
|
return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return true if server HTTP_REFERER isset |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
3 |
|
public function hasRefererURL() |
55
|
|
|
{ |
56
|
3 |
|
return $this->server->has("HTTP_REFERER"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return server HTTP_REFERER |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
3 |
|
public function refererURL() |
65
|
|
|
{ |
66
|
3 |
|
return $this->server->get("HTTP_REFERER"); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a new request with values from PHP's super globals. |
71
|
|
|
* |
72
|
|
|
* @param Session $session |
73
|
|
|
* @return Request A new request |
74
|
|
|
*/ |
75
|
|
|
public static function createFromSymfonyGlobal(Session $session) |
76
|
|
|
{ |
77
|
|
|
$baseRequest = BaseRequest::createFromGlobals(); |
78
|
|
|
|
79
|
|
|
$query = $baseRequest->query->all(); |
80
|
|
|
$request = $baseRequest->request->all(); |
81
|
|
|
$attributes = array(); |
82
|
|
|
$cookies = $baseRequest->cookies->all(); |
83
|
|
|
$files = $baseRequest->files->all(); |
84
|
|
|
$server = $baseRequest->server->all(); |
85
|
|
|
$content = $baseRequest->getContent(); |
86
|
|
|
|
87
|
|
|
$req = new static($query, $request, $attributes, $cookies, $files, $server, $content); |
88
|
|
|
$req->setSession($session); |
89
|
|
|
|
90
|
|
|
return $req; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Gets the Session. |
95
|
|
|
* |
96
|
|
|
* @return Session|null The session |
97
|
|
|
*/ |
98
|
|
|
public function getSession() |
99
|
|
|
{ |
100
|
|
|
return $this->session; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param $name |
105
|
|
|
* @return bool |
106
|
|
|
*/ |
107
|
|
|
public function hasPost($name) |
108
|
|
|
{ |
109
|
|
|
return $this->request->has($name); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param null $name |
114
|
|
|
* @param null $default |
115
|
|
|
* @return array|mixed|null |
116
|
|
|
*/ |
117
|
|
|
public function post($name = null, $default = null) |
118
|
|
|
{ |
119
|
|
|
return is_null($name)?$this->request->all():$this->request->get($name, $default); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @param $name |
124
|
|
|
* @return bool |
125
|
|
|
*/ |
126
|
|
|
public function hasOldPost($name) |
127
|
|
|
{ |
128
|
|
|
return $this->session->hasFlash("old.post.{$name}"); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param null $name |
133
|
|
|
* @param null $default |
134
|
|
|
* @return array |
135
|
|
|
*/ |
136
|
|
|
public function oldPost($name = null, $default = null) |
137
|
|
|
{ |
138
|
|
|
return $this->getOldByFullName("old.post.{$name}", $default); |
139
|
|
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* @param $name |
143
|
|
|
* @return bool |
144
|
|
|
*/ |
145
|
|
|
public function hasQuery($name) |
146
|
|
|
{ |
147
|
|
|
return $this->query->has($name); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* @param null $name |
152
|
|
|
* @param null $default |
153
|
|
|
* @return array|mixed|null |
154
|
|
|
*/ |
155
|
|
|
public function query($name = null, $default = null) |
156
|
|
|
{ |
157
|
|
|
return is_null($name)?$this->query->all():$this->query->get($name, $default); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* @param $name |
162
|
|
|
* @return bool |
163
|
|
|
*/ |
164
|
|
|
public function hasOldQuery($name) |
165
|
|
|
{ |
166
|
|
|
return $this->session->hasFlash("old.query.{$name}"); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
/** |
170
|
|
|
* @param null $name |
171
|
|
|
* @param null $default |
172
|
|
|
* @return array |
173
|
|
|
*/ |
174
|
|
|
public function oldQuery($name, $default = null) |
175
|
|
|
{ |
176
|
|
|
return $this->getOldByFullName("old.query.{$name}", $default); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
private function getOldByFullName($name, $default = null) |
180
|
|
|
{ |
181
|
|
|
if (null !== $result = $this->session->getFlash($name)) { |
182
|
|
|
return $result; |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
return $default; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
/** |
189
|
|
|
* @param $name |
190
|
|
|
* @param null $default |
191
|
|
|
* @return array|mixed|null |
192
|
|
|
*/ |
193
|
|
|
public function input($name, $default = null) |
194
|
|
|
{ |
195
|
|
|
if (null !== $result = $this->query($name)) { |
196
|
|
|
return $result; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
if (null !== $result = $this->post($name)) { |
200
|
|
|
return $result; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
return $default; |
204
|
|
|
} |
205
|
|
|
|
206
|
|
|
/** |
207
|
|
|
* @param $name |
208
|
|
|
* @return bool |
209
|
|
|
*/ |
210
|
|
|
public function hasFiles($name) |
211
|
|
|
{ |
212
|
|
|
return $this->files->has($name); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @param null $name |
217
|
|
|
* @return array|mixed |
218
|
|
|
*/ |
219
|
|
|
public function files($name = null) |
220
|
|
|
{ |
221
|
|
|
if (is_null($name)) { |
222
|
|
|
return $this->files->all(); |
223
|
|
|
} |
224
|
|
|
return $this->files->get($name); |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
|