Repository navigation
Expand file tree
/
Copy pathDispatcher.php
More file actions
750 lines (647 loc) · 16.1 KB
/
Copy pathDispatcher.php
File metadata and controls
750 lines (647 loc) · 16.1 KB
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
<?php
namespace Dingo\Api;
use Dingo\Api\Auth\Auth;
use Illuminate\Support\Str;
use Dingo\Api\Routing\Router;
use Dingo\Api\Http\InternalRequest;
use Illuminate\Container\Container;
use Illuminate\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\Cookie;
use Dingo\Api\Exception\InternalHttpException;
use Symfony\Component\HttpFoundation\File\UploadedFile;
use Illuminate\Support\Facades\Request as RequestFacade;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
class Dispatcher
{
/**
* Illuminate container instance.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* Illuminate filesystem instance.
*
* @var \Illuminate\Filesystem\Filesystem
*/
protected $files;
/**
* Router instance.
*
* @var \Dingo\Api\Routing\Router
*/
protected $router;
/**
* Auth instance.
*
* @var \Dingo\Api\Auth\Auth
*/
protected $auth;
/**
* Internal request stack.
*
* @var array
*/
protected $requestStack = [];
/**
* Internal route stack.
*
* @var array
*/
protected $routeStack = [];
/**
* Version for the request.
*
* @var string
*/
protected $version;
/**
* Request headers.
*
* @var array
*/
protected $headers = [];
/**
* Request cookies.
*
* @var array
*/
protected $cookies = [];
/**
* Request parameters.
*
* @var array
*/
protected $parameters = [];
/**
* Request raw content.
*
* @var string
*/
protected $content;
/**
* Request uploaded files.
*
* @var array
*/
protected $uploads = [];
/**
* Domain for the request.
*
* @var string
*/
protected $domain;
/**
* Indicates whether the returned response is the raw response object.
*
* @var bool
*/
protected $raw = false;
/**
* Indicates whether authentication is persisted.
*
* @var bool
*/
protected $persistAuthentication = true;
/**
* API subtype.
*
* @var string
*/
protected $subtype;
/**
* API standards tree.
*
* @var string
*/
protected $standardsTree;
/**
* API prefix.
*
* @var string
*/
protected $prefix;
/**
* Default version.
*
* @var string
*/
protected $defaultVersion;
/**
* Default domain.
*
* @var string
*/
protected $defaultDomain;
/**
* Default format.
*
* @var string
*/
protected $defaultFormat;
/**
* Create a new dispatcher instance.
*
* @param \Illuminate\Container\Container $container
* @param \Illuminate\Filesystem\Filesystem $files
* @param \Dingo\Api\Routing\Router $router
* @param \Dingo\Api\Auth\Auth $auth
*
* @return void
*/
public function __construct(Container $container, Filesystem $files, Router $router, Auth $auth)
{
$this->container = $container;
$this->files = $files;
$this->router = $router;
$this->auth = $auth;
$this->setupRequestStack();
}
/**
* Setup the request stack by grabbing the initial request.
*
* @return void
*/
protected function setupRequestStack()
{
$this->requestStack[] = $this->container['request'];
}
/**
* Attach files to be uploaded.
*
* @param array $files
*
* @return \Dingo\Api\Dispatcher
*/
public function attach(array $files)
{
foreach ($files as $key => $file) {
if (is_array($file)) {
$file = new UploadedFile($file['path'], basename($file['path']), $file['mime'], $file['size']);
} elseif (is_string($file)) {
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$file = new UploadedFile($file, basename($file), finfo_file($finfo, $file), $this->files->size($file));
} elseif (! $file instanceof UploadedFile) {
continue;
}
$this->uploads[$key] = $file;
}
return $this;
}
/**
* Internal request will be authenticated as the given user.
*
* @param mixed $user
*
* @return \Dingo\Api\Dispatcher
*/
public function be($user)
{
$this->auth->setUser($user);
return $this;
}
/**
* Send a JSON payload in the request body.
*
* @param string|array $content
*
* @return \Dingo\Api\Dispatcher
*/
public function json($content)
{
if (is_array($content)) {
$content = json_encode($content);
}
$this->content = $content;
return $this->header('Content-Type', 'application/json');
}
/**
* Sets the domain to be used for the request.
*
* @param string $domain
*
* @return \Dingo\Api\Dispatcher
*/
public function on($domain)
{
$this->domain = $domain;
return $this;
}
/**
* Return the raw response object once request is dispatched.
*
* @return \Dingo\Api\Dispatcher
*/
public function raw()
{
$this->raw = true;
return $this;
}
/**
* Only authenticate with the given user for a single request.
*
* @return \Dingo\Api\Dispatcher
*/
public function once()
{
$this->persistAuthentication = false;
return $this;
}
/**
* Set the version of the API for the next request.
*
* @param string $version
*
* @return \Dingo\Api\Dispatcher
*/
public function version($version)
{
$this->version = $version;
return $this;
}
/**
* Set the parameters to be sent on the next API request.
*
* @param string|array $parameters
*
* @return \Dingo\Api\Dispatcher
*/
public function with($parameters)
{
$this->parameters = array_merge($this->parameters, is_array($parameters) ? $parameters : func_get_args());
return $this;
}
/**
* Set a header to be sent on the next API request.
*
* @param string $key
* @param string $value
*
* @return \Dingo\Api\Dispatcher
*/
public function header($key, $value)
{
$this->headers[$key] = $value;
return $this;
}
/**
* Set a cookie to be sent on the next API request.
*
* @param \Symfony\Component\HttpFoundation\Cookie $cookie
*
* @return \Dingo\Api\Dispatcher
*/
public function cookie(Cookie $cookie)
{
$this->cookies[] = $cookie;
return $this;
}
/**
* Perform API GET request.
*
* @param string $uri
* @param string|array $parameters
*
* @return mixed
*/
public function get($uri, $parameters = [])
{
return $this->queueRequest('get', $uri, $parameters);
}
/**
* Perform API POST request.
*
* @param string $uri
* @param string|array $parameters
* @param string $content
*
* @return mixed
*/
public function post($uri, $parameters = [], $content = '')
{
return $this->queueRequest('post', $uri, $parameters, $content);
}
/**
* Perform API PUT request.
*
* @param string $uri
* @param string|array $parameters
* @param string $content
*
* @return mixed
*/
public function put($uri, $parameters = [], $content = '')
{
return $this->queueRequest('put', $uri, $parameters, $content);
}
/**
* Perform API PATCH request.
*
* @param string $uri
* @param string|array $parameters
* @param string $content
*
* @return mixed
*/
public function patch($uri, $parameters = [], $content = '')
{
return $this->queueRequest('patch', $uri, $parameters, $content);
}
/**
* Perform API DELETE request.
*
* @param string $uri
* @param string|array $parameters
* @param string $content
*
* @return mixed
*/
public function delete($uri, $parameters = [], $content = '')
{
return $this->queueRequest('delete', $uri, $parameters, $content);
}
/**
* Queue up and dispatch a new request.
*
* @param string $verb
* @param string $uri
* @param string|array $parameters
* @param string $content
*
* @return mixed
*/
protected function queueRequest($verb, $uri, $parameters, $content = '')
{
if (! empty($content)) {
$this->content = $content;
}
// Sometimes after setting the initial request another request might be made prior to
// internally dispatching an API request. We need to capture this request as well
// and add it to the request stack as it has become the new parent request to
// this internal request. This will generally occur during tests when
// using the crawler to navigate pages that also make internal
// requests.
if (end($this->requestStack) != $this->container['request']) {
$this->requestStack[] = $this->container['request'];
}
$this->requestStack[] = $request = $this->createRequest($verb, $uri, $parameters);
return $this->dispatch($request);
}
/**
* Create a new internal request from an HTTP verb and URI.
*
* @param string $verb
* @param string $uri
* @param string|array $parameters
*
* @return \Dingo\Api\Http\InternalRequest
*/
protected function createRequest($verb, $uri, $parameters)
{
$parameters = array_merge($this->parameters, (array) $parameters);
$uri = $this->addPrefixToUri($uri);
// If the URI does not have a scheme then we can assume that there it is not an
// absolute URI, in this case we'll prefix the root requests path to the URI.
$rootUrl = $this->getRootRequest()->root();
if ((! parse_url($uri, PHP_URL_SCHEME)) && parse_url($rootUrl) !== false) {
$uri = rtrim($rootUrl, '/').'/'.ltrim($uri, '/');
}
$request = InternalRequest::create(
$uri,
$verb,
$parameters,
$this->cookies,
$this->uploads,
$this->container['request']->server->all(),
$this->content
);
$request->headers->set('host', $this->getDomain());
foreach ($this->headers as $header => $value) {
$request->headers->set($header, $value);
}
$request->headers->set('accept', $this->getAcceptHeader());
return $request;
}
/**
* Add the prefix to the URI.
*
* @param string $uri
*
* @return string
*/
protected function addPrefixToUri($uri)
{
if (! isset($this->prefix)) {
return $uri;
}
$uri = trim($uri, '/');
if (Str::startsWith($uri, $this->prefix)) {
return $uri;
}
return rtrim('/'.trim($this->prefix, '/').'/'.$uri, '/');
}
/**
* Build the "Accept" header.
*
* @return string
*/
protected function getAcceptHeader()
{
return sprintf('application/%s.%s.%s+%s', $this->getStandardsTree(), $this->getSubtype(), $this->getVersion(), $this->getFormat());
}
/**
* Attempt to dispatch an internal request.
*
* @param \Dingo\Api\Http\InternalRequest $request
*
* @throws \Exception|\Symfony\Component\HttpKernel\Exception\HttpExceptionInterface
*
* @return mixed
*/
protected function dispatch(InternalRequest $request)
{
$this->routeStack[] = $this->router->getCurrentRoute();
$this->clearCachedFacadeInstance();
try {
$this->container->instance('request', $request);
$response = $this->router->dispatch($request);
if (! $response->isSuccessful() && ! $response->isRedirection()) {
throw new InternalHttpException($response);
}
if (! $this->raw) {
$response = $response->getOriginalContent();
}
} catch (HttpExceptionInterface $exception) {
$this->refreshRequestStack();
throw $exception;
}
$this->refreshRequestStack();
return $response;
}
/**
* Refresh the request stack.
*
* This is done by resetting the authentication, popping
* the last request from the stack, replacing the input,
* and resetting the version and parameters.
*
* @return void
*/
protected function refreshRequestStack()
{
if (! $this->persistAuthentication) {
$this->auth->setUser(null);
$this->persistAuthentication = true;
}
if ($route = array_pop($this->routeStack)) {
$this->router->setCurrentRoute($route);
}
$this->replaceRequestInstance();
$this->clearCachedFacadeInstance();
$this->raw = false;
$this->version = $this->domain = $this->content = null;
$this->parameters = $this->uploads = [];
}
/**
* Replace the request instance with the previous request instance.
*
* @return void
*/
protected function replaceRequestInstance()
{
array_pop($this->requestStack);
$this->container->instance('request', end($this->requestStack));
}
/**
* Clear the cached facade instance.
*
* @return void
*/
protected function clearCachedFacadeInstance()
{
// Facades cache the resolved instance so we need to clear out the
// request instance that may have been cached. Otherwise we'll
// may get unexpected results.
RequestFacade::clearResolvedInstance('request');
}
/**
* Get the root request instance.
*
* @return \Illuminate\Http\Request
*/
protected function getRootRequest()
{
return reset($this->requestStack);
}
/**
* Get the domain.
*
* @return string
*/
public function getDomain()
{
return $this->domain ?: $this->defaultDomain;
}
/**
* Get the version.
*
* @return string
*/
public function getVersion()
{
return $this->version ?: $this->defaultVersion;
}
/**
* Get the format.
*
* @return string
*/
public function getFormat()
{
return $this->defaultFormat;
}
/**
* Get the subtype.
*
* @return string
*/
public function getSubtype()
{
return $this->subtype;
}
/**
* Set the subtype.
*
* @param string $subtype
*
* @return void
*/
public function setSubtype($subtype)
{
$this->subtype = $subtype;
}
/**
* Get the standards tree.
*
* @return string
*/
public function getStandardsTree()
{
return $this->standardsTree;
}
/**
* Set the standards tree.
*
* @param string $standardsTree
*
* @return void
*/
public function setStandardsTree($standardsTree)
{
$this->standardsTree = $standardsTree;
}
/**
* Set the prefix.
*
* @param string $prefix
*
* @return void
*/
public function setPrefix($prefix)
{
$this->prefix = $prefix;
}
/**
* Set the default version.
*
* @param string $version
*
* @return void
*/
public function setDefaultVersion($version)
{
$this->defaultVersion = $version;
}
/**
* Set the default domain.
*
* @param string $domain
*
* @return void
*/
public function setDefaultDomain($domain)
{
$this->defaultDomain = $domain;
}
/**
* Set the default format.
*
* @param string $format
*
* @return void
*/
public function setDefaultFormat($format)
{
$this->defaultFormat = $format;
}
}