-
Notifications
You must be signed in to change notification settings - Fork 11.1k
/
Request.php
1088 lines (940 loc) · 23.9 KB
/
Request.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
namespace Illuminate\Http;
use Closure;
use ArrayAccess;
use SplFileInfo;
use RuntimeException;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Illuminate\Contracts\Support\Arrayable;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
class Request extends SymfonyRequest implements Arrayable, ArrayAccess
{
use Macroable;
/**
* The decoded JSON content for the request.
*
* @var string
*/
protected $json;
/**
* All of the converted files for the request.
*
* @var array
*/
protected $convertedFiles;
/**
* The user resolver callback.
*
* @var \Closure
*/
protected $userResolver;
/**
* The route resolver callback.
*
* @var \Closure
*/
protected $routeResolver;
/**
* Create a new Illuminate HTTP request from server variables.
*
* @return static
*/
public static function capture()
{
static::enableHttpMethodParameterOverride();
return static::createFromBase(SymfonyRequest::createFromGlobals());
}
/**
* Return the Request instance.
*
* @return $this
*/
public function instance()
{
return $this;
}
/**
* Get the request method.
*
* @return string
*/
public function method()
{
return $this->getMethod();
}
/**
* Get the root URL for the application.
*
* @return string
*/
public function root()
{
return rtrim($this->getSchemeAndHttpHost().$this->getBaseUrl(), '/');
}
/**
* Get the URL (no query string) for the request.
*
* @return string
*/
public function url()
{
return rtrim(preg_replace('/\?.*/', '', $this->getUri()), '/');
}
/**
* Get the full URL for the request.
*
* @return string
*/
public function fullUrl()
{
$query = $this->getQueryString();
$question = $this->getBaseUrl().$this->getPathInfo() == '/' ? '/?' : '?';
return $query ? $this->url().$question.$query : $this->url();
}
/**
* Get the full URL for the request with the added query string parameters.
*
* @param array $query
* @return string
*/
public function fullUrlWithQuery(array $query)
{
$question = $this->getBaseUrl().$this->getPathInfo() == '/' ? '/?' : '?';
return count($this->query()) > 0
? $this->url().$question.http_build_query(array_merge($this->query(), $query))
: $this->fullUrl().$question.http_build_query($query);
}
/**
* Get the current path info for the request.
*
* @return string
*/
public function path()
{
$pattern = trim($this->getPathInfo(), '/');
return $pattern == '' ? '/' : $pattern;
}
/**
* Get the current encoded path info for the request.
*
* @return string
*/
public function decodedPath()
{
return rawurldecode($this->path());
}
/**
* Get a segment from the URI (1 based index).
*
* @param int $index
* @param string|null $default
* @return string|null
*/
public function segment($index, $default = null)
{
return Arr::get($this->segments(), $index - 1, $default);
}
/**
* Get all of the segments for the request path.
*
* @return array
*/
public function segments()
{
$segments = explode('/', $this->decodedPath());
return array_values(array_filter($segments, function ($v) {
return $v != '';
}));
}
/**
* Determine if the current request URI matches a pattern.
*
* @return bool
*/
public function is()
{
foreach (func_get_args() as $pattern) {
if (Str::is($pattern, $this->decodedPath())) {
return true;
}
}
return false;
}
/**
* Determine if the current request URL and query string matches a pattern.
*
* @return bool
*/
public function fullUrlIs()
{
$url = $this->fullUrl();
foreach (func_get_args() as $pattern) {
if (Str::is($pattern, $url)) {
return true;
}
}
return false;
}
/**
* Determine if the request is the result of an AJAX call.
*
* @return bool
*/
public function ajax()
{
return $this->isXmlHttpRequest();
}
/**
* Determine if the request is the result of an PJAX call.
*
* @return bool
*/
public function pjax()
{
return $this->headers->get('X-PJAX') == true;
}
/**
* Determine if the request is over HTTPS.
*
* @return bool
*/
public function secure()
{
return $this->isSecure();
}
/**
* Returns the client IP address.
*
* @return string
*/
public function ip()
{
return $this->getClientIp();
}
/**
* Returns the client IP addresses.
*
* @return array
*/
public function ips()
{
return $this->getClientIps();
}
/**
* Determine if the request contains a given input item key.
*
* @param string|array $key
* @return bool
*/
public function exists($key)
{
$keys = is_array($key) ? $key : func_get_args();
$input = $this->all();
foreach ($keys as $value) {
if (! Arr::has($input, $value)) {
return false;
}
}
return true;
}
/**
* Determine if the request contains a non-empty value for an input item.
*
* @param string|array $key
* @return bool
*/
public function has($key)
{
$keys = is_array($key) ? $key : func_get_args();
foreach ($keys as $value) {
if ($this->isEmptyString($value)) {
return false;
}
}
return true;
}
/**
* Determine if the given input key is an empty string for "has".
*
* @param string $key
* @return bool
*/
protected function isEmptyString($key)
{
$value = $this->input($key);
$boolOrArray = is_bool($value) || is_array($value);
return ! $boolOrArray && trim((string) $value) === '';
}
/**
* Get all of the input and files for the request.
*
* @return array
*/
public function all()
{
return array_replace_recursive($this->input(), $this->allFiles());
}
/**
* Retrieve an input item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function input($key = null, $default = null)
{
$input = $this->getInputSource()->all() + $this->query->all();
return data_get($input, $key, $default);
}
/**
* Get a subset containing the provided keys with values from the input data.
*
* @param array|mixed $keys
* @return array
*/
public function only($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$results = [];
$input = $this->all();
foreach ($keys as $key) {
Arr::set($results, $key, data_get($input, $key));
}
return $results;
}
/**
* Get all of the input except for a specified array of items.
*
* @param array|mixed $keys
* @return array
*/
public function except($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
$results = $this->all();
Arr::forget($results, $keys);
return $results;
}
/**
* Intersect an array of items with the input data.
*
* @param array|mixed $keys
* @return array
*/
public function intersect($keys)
{
return array_filter($this->only(is_array($keys) ? $keys : func_get_args()));
}
/**
* Retrieve a query string item from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function query($key = null, $default = null)
{
return $this->retrieveItem('query', $key, $default);
}
/**
* Determine if a cookie is set on the request.
*
* @param string $key
* @return bool
*/
public function hasCookie($key)
{
return ! is_null($this->cookie($key));
}
/**
* Retrieve a cookie from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function cookie($key = null, $default = null)
{
return $this->retrieveItem('cookies', $key, $default);
}
/**
* Get an array of all of the files on the request.
*
* @return array
*/
public function allFiles()
{
$files = $this->files->all();
return $this->convertedFiles
? $this->convertedFiles
: $this->convertedFiles = $this->convertUploadedFiles($files);
}
/**
* Convert the given array of Symfony UploadedFiles to custom Laravel UploadedFiles.
*
* @param array $files
* @return array
*/
protected function convertUploadedFiles(array $files)
{
return array_map(function ($file) {
if (is_null($file) || (is_array($file) && empty(array_filter($file)))) {
return $file;
}
return is_array($file)
? $this->convertUploadedFiles($file)
: UploadedFile::createFromBase($file);
}, $files);
}
/**
* Retrieve a file from the request.
*
* @param string $key
* @param mixed $default
* @return \Illuminate\Http\UploadedFile|array|null
*/
public function file($key = null, $default = null)
{
return data_get($this->allFiles(), $key, $default);
}
/**
* Determine if the uploaded data contains a file.
*
* @param string $key
* @return bool
*/
public function hasFile($key)
{
if (! is_array($files = $this->file($key))) {
$files = [$files];
}
foreach ($files as $file) {
if ($this->isValidFile($file)) {
return true;
}
}
return false;
}
/**
* Check that the given file is a valid file instance.
*
* @param mixed $file
* @return bool
*/
protected function isValidFile($file)
{
return $file instanceof SplFileInfo && $file->getPath() != '';
}
/**
* Determine if a header is set on the request.
*
* @param string $key
* @return bool
*/
public function hasHeader($key)
{
return ! is_null($this->header($key));
}
/**
* Retrieve a header from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function header($key = null, $default = null)
{
return $this->retrieveItem('headers', $key, $default);
}
/**
* Retrieve a server variable from the request.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function server($key = null, $default = null)
{
return $this->retrieveItem('server', $key, $default);
}
/**
* Retrieve an old input item.
*
* @param string $key
* @param string|array|null $default
* @return string|array
*/
public function old($key = null, $default = null)
{
return $this->session()->getOldInput($key, $default);
}
/**
* Flash the input for the current request to the session.
*
* @param string $filter
* @param array $keys
* @return void
*/
public function flash($filter = null, $keys = [])
{
$flash = (! is_null($filter)) ? $this->$filter($keys) : $this->input();
$this->session()->flashInput($flash);
}
/**
* Flash only some of the input to the session.
*
* @param array|mixed $keys
* @return void
*/
public function flashOnly($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return $this->flash('only', $keys);
}
/**
* Flash only some of the input to the session.
*
* @param array|mixed $keys
* @return void
*/
public function flashExcept($keys)
{
$keys = is_array($keys) ? $keys : func_get_args();
return $this->flash('except', $keys);
}
/**
* Flush all of the old input from the session.
*
* @return void
*/
public function flush()
{
$this->session()->flashInput([]);
}
/**
* Retrieve a parameter item from a given source.
*
* @param string $source
* @param string $key
* @param string|array|null $default
* @return string|array
*/
protected function retrieveItem($source, $key, $default)
{
if (is_null($key)) {
return $this->$source->all();
}
return $this->$source->get($key, $default);
}
/**
* Merge new input into the current request's input array.
*
* @param array $input
* @return void
*/
public function merge(array $input)
{
$this->getInputSource()->add($input);
}
/**
* Replace the input for the current request.
*
* @param array $input
* @return void
*/
public function replace(array $input)
{
$this->getInputSource()->replace($input);
}
/**
* Get the JSON payload for the request.
*
* @param string $key
* @param mixed $default
* @return mixed
*/
public function json($key = null, $default = null)
{
if (! isset($this->json)) {
$this->json = new ParameterBag((array) json_decode($this->getContent(), true));
}
if (is_null($key)) {
return $this->json;
}
return data_get($this->json->all(), $key, $default);
}
/**
* Get the input source for the request.
*
* @return \Symfony\Component\HttpFoundation\ParameterBag
*/
protected function getInputSource()
{
if ($this->isJson()) {
return $this->json();
}
return $this->getRealMethod() == 'GET' ? $this->query : $this->request;
}
/**
* Determine if the given content types match.
*
* @param string $actual
* @param string $type
* @return bool
*/
public static function matchesType($actual, $type)
{
if ($actual === $type) {
return true;
}
$split = explode('/', $actual);
return isset($split[1]) && preg_match('#'.preg_quote($split[0], '#').'/.+\+'.preg_quote($split[1], '#').'#', $type);
}
/**
* Determine if the request is sending JSON.
*
* @return bool
*/
public function isJson()
{
return Str::contains($this->header('CONTENT_TYPE'), ['/json', '+json']);
}
/**
* Determine if the current request probably expects a JSON response.
*
* @return bool
*/
public function expectsJson()
{
return ($this->ajax() && ! $this->pjax()) || $this->wantsJson();
}
/**
* Determine if the current request is asking for JSON in return.
*
* @return bool
*/
public function wantsJson()
{
$acceptable = $this->getAcceptableContentTypes();
return isset($acceptable[0]) && Str::contains($acceptable[0], ['/json', '+json']);
}
/**
* Determines whether the current requests accepts a given content type.
*
* @param string|array $contentTypes
* @return bool
*/
public function accepts($contentTypes)
{
$accepts = $this->getAcceptableContentTypes();
if (count($accepts) === 0) {
return true;
}
$types = (array) $contentTypes;
foreach ($accepts as $accept) {
if ($accept === '*/*' || $accept === '*') {
return true;
}
foreach ($types as $type) {
if ($this->matchesType($accept, $type) || $accept === strtok($type, '/').'/*') {
return true;
}
}
}
return false;
}
/**
* Return the most suitable content type from the given array based on content negotiation.
*
* @param string|array $contentTypes
* @return string|null
*/
public function prefers($contentTypes)
{
$accepts = $this->getAcceptableContentTypes();
$contentTypes = (array) $contentTypes;
foreach ($accepts as $accept) {
if (in_array($accept, ['*/*', '*'])) {
return $contentTypes[0];
}
foreach ($contentTypes as $contentType) {
$type = $contentType;
if (! is_null($mimeType = $this->getMimeType($contentType))) {
$type = $mimeType;
}
if ($this->matchesType($type, $accept) || $accept === strtok($type, '/').'/*') {
return $contentType;
}
}
}
}
/**
* Determines whether a request accepts JSON.
*
* @return bool
*/
public function acceptsJson()
{
return $this->accepts('application/json');
}
/**
* Determines whether a request accepts HTML.
*
* @return bool
*/
public function acceptsHtml()
{
return $this->accepts('text/html');
}
/**
* Get the data format expected in the response.
*
* @param string $default
* @return string
*/
public function format($default = 'html')
{
foreach ($this->getAcceptableContentTypes() as $type) {
if ($format = $this->getFormat($type)) {
return $format;
}
}
return $default;
}
/**
* Get the bearer token from the request headers.
*
* @return string|null
*/
public function bearerToken()
{
$header = $this->header('Authorization', '');
if (Str::startsWith($header, 'Bearer ')) {
return Str::substr($header, 7);
}
}
/**
* Create an Illuminate request from a Symfony instance.
*
* @param \Symfony\Component\HttpFoundation\Request $request
* @return \Illuminate\Http\Request
*/
public static function createFromBase(SymfonyRequest $request)
{
if ($request instanceof static) {
return $request;
}
$content = $request->content;
$request = (new static)->duplicate(
$request->query->all(), $request->request->all(), $request->attributes->all(),
$request->cookies->all(), $request->files->all(), $request->server->all()
);
$request->content = $content;
$request->request = $request->getInputSource();
return $request;
}
/**
* {@inheritdoc}
*/
public function duplicate(array $query = null, array $request = null, array $attributes = null, array $cookies = null, array $files = null, array $server = null)
{
return parent::duplicate($query, $request, $attributes, $cookies, $this->filterFiles($files), $server);
}
/**
* Filter the given array of files, removing any empty values.
*
* @param mixed $files
* @return mixed
*/
protected function filterFiles($files)
{
if (! $files) {
return;
}
foreach ($files as $key => $file) {
if (is_array($file)) {
$files[$key] = $this->filterFiles($files[$key]);
}
if (empty($files[$key])) {
unset($files[$key]);
}
}
return $files;
}
/**
* Get the session associated with the request.
*
* @return \Illuminate\Session\Store
*
* @throws \RuntimeException
*/
public function session()
{
if (! $this->hasSession()) {
throw new RuntimeException('Session store not set on request.');
}
return $this->getSession();
}
/**
* Get the user making the request.
*
* @param string|null $guard
* @return mixed
*/
public function user($guard = null)
{
return call_user_func($this->getUserResolver(), $guard);
}
/**
* Get the route handling the request.
*
* @param string|null $param
*
* @return \Illuminate\Routing\Route|object|string
*/
public function route($param = null)
{
$route = call_user_func($this->getRouteResolver());
if (is_null($route) || is_null($param)) {
return $route;
} else {
return $route->parameter($param);
}
}
/**
* Get a unique fingerprint for the request / route / IP address.
*
* @return string
*
* @throws \RuntimeException
*/
public function fingerprint()
{
if (! $route = $this->route()) {
throw new RuntimeException('Unable to generate fingerprint. Route unavailable.');
}
return sha1(implode('|', array_merge(
$route->methods(), [$route->domain(), $route->uri(), $this->ip()]
)));
}
/**
* Get the user resolver callback.
*
* @return \Closure
*/
public function getUserResolver()
{
return $this->userResolver ?: function () {
//
};
}
/**
* Set the user resolver callback.
*
* @param \Closure $callback
* @return $this
*/
public function setUserResolver(Closure $callback)
{
$this->userResolver = $callback;
return $this;
}
/**
* Get the route resolver callback.
*
* @return \Closure
*/
public function getRouteResolver()
{
return $this->routeResolver ?: function () {
//
};
}
/**
* Set the route resolver callback.
*
* @param \Closure $callback
* @return $this
*/