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
|
|
|
public function path() |
24
|
|
|
{ |
25
|
|
|
$pattern = trim($this->getPathInfo(), '/'); |
26
|
|
|
return $pattern == '' ? '/' : $pattern; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Get the request method. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function method() |
35
|
|
|
{ |
36
|
|
|
return $this->getMethod(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get the root URL |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function rootURL() |
45
|
|
|
{ |
46
|
|
|
return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/'); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Return true if server HTTP_REFERER isset |
51
|
|
|
* |
52
|
|
|
* @return bool |
53
|
|
|
*/ |
54
|
|
|
public function hasRefererURL() |
55
|
|
|
{ |
56
|
|
|
return $this->server->has("HTTP_REFERER"); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Return server HTTP_REFERER |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function refererURL() |
65
|
|
|
{ |
66
|
|
|
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
|
|
View Code Duplication |
public function post($name = null, $default = null) |
|
|
|
|
118
|
|
|
{ |
119
|
|
|
if (is_null($name)) { |
120
|
|
|
return $this->request->all(); |
121
|
|
|
} |
122
|
|
|
$result = $this->request->get($name); |
123
|
|
|
return $result?$result:$default; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param $name |
128
|
|
|
* @return bool |
129
|
|
|
*/ |
130
|
|
|
public function hasOldPost($name) |
131
|
|
|
{ |
132
|
|
|
return $this->session->hasFlash("old.post.{$name}"); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param null $name |
137
|
|
|
* @param null $default |
138
|
|
|
* @return array |
139
|
|
|
*/ |
140
|
|
View Code Duplication |
public function oldPost($name = null, $default = null) |
|
|
|
|
141
|
|
|
{ |
142
|
|
|
if (is_null($name)) { |
143
|
|
|
return $this->session->getFlash("old.post.{$name}"); |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
if (null !== $result = $this->session->getFlash("old.post.{$name}")) { |
147
|
|
|
return $result; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return $default; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
/** |
154
|
|
|
* @param $name |
155
|
|
|
* @return bool |
156
|
|
|
*/ |
157
|
|
|
public function hasQuery($name) |
158
|
|
|
{ |
159
|
|
|
return $this->query->has($name); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* @param null $name |
164
|
|
|
* @param null $default |
165
|
|
|
* @return array|mixed|null |
166
|
|
|
*/ |
167
|
|
View Code Duplication |
public function query($name = null, $default = null) |
|
|
|
|
168
|
|
|
{ |
169
|
|
|
if (is_null($name)) { |
170
|
|
|
return $this->query->all(); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$result = $this->query->get($name); |
174
|
|
|
|
175
|
|
|
return $result?$result:$default; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* @param $name |
180
|
|
|
* @return bool |
181
|
|
|
*/ |
182
|
|
|
public function hasOldQuery($name) |
183
|
|
|
{ |
184
|
|
|
return $this->session->hasFlash("old.query.{$name}"); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* @param null $name |
189
|
|
|
* @param null $default |
190
|
|
|
* @return array |
191
|
|
|
*/ |
192
|
|
View Code Duplication |
public function oldQuery($name = null, $default = null) |
|
|
|
|
193
|
|
|
{ |
194
|
|
|
if (is_null($name)) { |
195
|
|
|
return $this->session->getFlash("old.query.{$name}"); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
if (null !== $result = $this->session->getFlash("old.query.{$name}")) { |
199
|
|
|
return $result; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
return $default; |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* @param $name |
207
|
|
|
* @param null $default |
208
|
|
|
* @return array|mixed|null |
209
|
|
|
*/ |
210
|
|
|
public function input($name, $default = null) |
211
|
|
|
{ |
212
|
|
|
if (null !== $result = $this->query($name)) { |
213
|
|
|
return $result; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
if (null !== $result = $this->post($name)) { |
217
|
|
|
return $result; |
218
|
|
|
} |
219
|
|
|
|
220
|
|
|
return $default; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param $name |
225
|
|
|
* @return bool |
226
|
|
|
*/ |
227
|
|
|
public function hasFiles($name) |
228
|
|
|
{ |
229
|
|
|
return $this->files->has($name); |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* @param null $name |
234
|
|
|
* @return array|mixed |
235
|
|
|
*/ |
236
|
|
|
public function files($name = null) |
237
|
|
|
{ |
238
|
|
|
if (is_null($name)) { |
239
|
|
|
return $this->files->all(); |
240
|
|
|
} |
241
|
|
|
return $this->files->get($name); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.