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