Route.php
29.8 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
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
<?php
namespace Illuminate\Routing;
use Closure;
use Illuminate\Container\Container;
use Illuminate\Http\Exceptions\HttpResponseException;
use Illuminate\Http\Request;
use Illuminate\Routing\Contracts\ControllerDispatcher as ControllerDispatcherContract;
use Illuminate\Routing\Matching\HostValidator;
use Illuminate\Routing\Matching\MethodValidator;
use Illuminate\Routing\Matching\SchemeValidator;
use Illuminate\Routing\Matching\UriValidator;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Illuminate\Support\Traits\Macroable;
use Laravel\SerializableClosure\SerializableClosure;
use LogicException;
use Opis\Closure\SerializableClosure as OpisSerializableClosure;
use ReflectionFunction;
use Symfony\Component\Routing\Route as SymfonyRoute;
class Route
{
use CreatesRegularExpressionRouteConstraints, Macroable, RouteDependencyResolverTrait;
/**
* The URI pattern the route responds to.
*
* @var string
*/
public $uri;
/**
* The HTTP methods the route responds to.
*
* @var array
*/
public $methods;
/**
* The route action array.
*
* @var array
*/
public $action;
/**
* Indicates whether the route is a fallback route.
*
* @var bool
*/
public $isFallback = false;
/**
* The controller instance.
*
* @var mixed
*/
public $controller;
/**
* The default values for the route.
*
* @var array
*/
public $defaults = [];
/**
* The regular expression requirements.
*
* @var array
*/
public $wheres = [];
/**
* The array of matched parameters.
*
* @var array|null
*/
public $parameters;
/**
* The parameter names for the route.
*
* @var array|null
*/
public $parameterNames;
/**
* The array of the matched parameters' original values.
*
* @var array
*/
protected $originalParameters;
/**
* Indicates "trashed" models can be retrieved when resolving implicit model bindings for this route.
*
* @var bool
*/
protected $withTrashedBindings = false;
/**
* Indicates the maximum number of seconds the route should acquire a session lock for.
*
* @var int|null
*/
protected $lockSeconds;
/**
* Indicates the maximum number of seconds the route should wait while attempting to acquire a session lock.
*
* @var int|null
*/
protected $waitSeconds;
/**
* The computed gathered middleware.
*
* @var array|null
*/
public $computedMiddleware;
/**
* The compiled version of the route.
*
* @var \Symfony\Component\Routing\CompiledRoute
*/
public $compiled;
/**
* The router instance used by the route.
*
* @var \Illuminate\Routing\Router
*/
protected $router;
/**
* The container instance used by the route.
*
* @var \Illuminate\Container\Container
*/
protected $container;
/**
* The fields that implicit binding should use for a given parameter.
*
* @var array
*/
protected $bindingFields = [];
/**
* The validators used by the routes.
*
* @var array
*/
public static $validators;
/**
* Create a new Route instance.
*
* @param array|string $methods
* @param string $uri
* @param \Closure|array $action
* @return void
*/
public function __construct($methods, $uri, $action)
{
$this->uri = $uri;
$this->methods = (array) $methods;
$this->action = Arr::except($this->parseAction($action), ['prefix']);
if (in_array('GET', $this->methods) && ! in_array('HEAD', $this->methods)) {
$this->methods[] = 'HEAD';
}
$this->prefix(is_array($action) ? Arr::get($action, 'prefix') : '');
}
/**
* Parse the route action into a standard array.
*
* @param callable|array|null $action
* @return array
*
* @throws \UnexpectedValueException
*/
protected function parseAction($action)
{
return RouteAction::parse($this->uri, $action);
}
/**
* Run the route action and return the response.
*
* @return mixed
*/
public function run()
{
$this->container = $this->container ?: new Container;
try {
if ($this->isControllerAction()) {
return $this->runController();
}
return $this->runCallable();
} catch (HttpResponseException $e) {
return $e->getResponse();
}
}
/**
* Checks whether the route's action is a controller.
*
* @return bool
*/
protected function isControllerAction()
{
return is_string($this->action['uses']) && ! $this->isSerializedClosure();
}
/**
* Run the route action and return the response.
*
* @return mixed
*/
protected function runCallable()
{
$callable = $this->action['uses'];
if ($this->isSerializedClosure()) {
$callable = unserialize($this->action['uses'])->getClosure();
}
return $callable(...array_values($this->resolveMethodDependencies(
$this->parametersWithoutNulls(), new ReflectionFunction($callable)
)));
}
/**
* Determine if the route action is a serialized Closure.
*
* @return bool
*/
protected function isSerializedClosure()
{
return RouteAction::containsSerializedClosure($this->action);
}
/**
* Run the route action and return the response.
*
* @return mixed
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
*/
protected function runController()
{
return $this->controllerDispatcher()->dispatch(
$this, $this->getController(), $this->getControllerMethod()
);
}
/**
* Get the controller instance for the route.
*
* @return mixed
*/
public function getController()
{
if (! $this->controller) {
$class = $this->parseControllerCallback()[0];
$this->controller = $this->container->make(ltrim($class, '\\'));
}
return $this->controller;
}
/**
* Get the controller method used for the route.
*
* @return string
*/
protected function getControllerMethod()
{
return $this->parseControllerCallback()[1];
}
/**
* Parse the controller.
*
* @return array
*/
protected function parseControllerCallback()
{
return Str::parseCallback($this->action['uses']);
}
/**
* Flush the cached container instance on the route.
*
* @return void
*/
public function flushController()
{
$this->computedMiddleware = null;
$this->controller = null;
}
/**
* Determine if the route matches a given request.
*
* @param \Illuminate\Http\Request $request
* @param bool $includingMethod
* @return bool
*/
public function matches(Request $request, $includingMethod = true)
{
$this->compileRoute();
foreach (self::getValidators() as $validator) {
if (! $includingMethod && $validator instanceof MethodValidator) {
continue;
}
if (! $validator->matches($this, $request)) {
return false;
}
}
return true;
}
/**
* Compile the route into a Symfony CompiledRoute instance.
*
* @return \Symfony\Component\Routing\CompiledRoute
*/
protected function compileRoute()
{
if (! $this->compiled) {
$this->compiled = $this->toSymfonyRoute()->compile();
}
return $this->compiled;
}
/**
* Bind the route to a given request for execution.
*
* @param \Illuminate\Http\Request $request
* @return $this
*/
public function bind(Request $request)
{
$this->compileRoute();
$this->parameters = (new RouteParameterBinder($this))
->parameters($request);
$this->originalParameters = $this->parameters;
return $this;
}
/**
* Determine if the route has parameters.
*
* @return bool
*/
public function hasParameters()
{
return isset($this->parameters);
}
/**
* Determine a given parameter exists from the route.
*
* @param string $name
* @return bool
*/
public function hasParameter($name)
{
if ($this->hasParameters()) {
return array_key_exists($name, $this->parameters());
}
return false;
}
/**
* Get a given parameter from the route.
*
* @param string $name
* @param string|object|null $default
* @return string|object|null
*/
public function parameter($name, $default = null)
{
return Arr::get($this->parameters(), $name, $default);
}
/**
* Get original value of a given parameter from the route.
*
* @param string $name
* @param string|null $default
* @return string|null
*/
public function originalParameter($name, $default = null)
{
return Arr::get($this->originalParameters(), $name, $default);
}
/**
* Set a parameter to the given value.
*
* @param string $name
* @param string|object|null $value
* @return void
*/
public function setParameter($name, $value)
{
$this->parameters();
$this->parameters[$name] = $value;
}
/**
* Unset a parameter on the route if it is set.
*
* @param string $name
* @return void
*/
public function forgetParameter($name)
{
$this->parameters();
unset($this->parameters[$name]);
}
/**
* Get the key / value list of parameters for the route.
*
* @return array
*
* @throws \LogicException
*/
public function parameters()
{
if (isset($this->parameters)) {
return $this->parameters;
}
throw new LogicException('Route is not bound.');
}
/**
* Get the key / value list of original parameters for the route.
*
* @return array
*
* @throws \LogicException
*/
public function originalParameters()
{
if (isset($this->originalParameters)) {
return $this->originalParameters;
}
throw new LogicException('Route is not bound.');
}
/**
* Get the key / value list of parameters without null values.
*
* @return array
*/
public function parametersWithoutNulls()
{
return array_filter($this->parameters(), function ($p) {
return ! is_null($p);
});
}
/**
* Get all of the parameter names for the route.
*
* @return array
*/
public function parameterNames()
{
if (isset($this->parameterNames)) {
return $this->parameterNames;
}
return $this->parameterNames = $this->compileParameterNames();
}
/**
* Get the parameter names for the route.
*
* @return array
*/
protected function compileParameterNames()
{
preg_match_all('/\{(.*?)\}/', $this->getDomain().$this->uri, $matches);
return array_map(function ($m) {
return trim($m, '?');
}, $matches[1]);
}
/**
* Get the parameters that are listed in the route / controller signature.
*
* @param string|null $subClass
* @return array
*/
public function signatureParameters($subClass = null)
{
return RouteSignatureParameters::fromAction($this->action, $subClass);
}
/**
* Get the binding field for the given parameter.
*
* @param string|int $parameter
* @return string|null
*/
public function bindingFieldFor($parameter)
{
$fields = is_int($parameter) ? array_values($this->bindingFields) : $this->bindingFields;
return $fields[$parameter] ?? null;
}
/**
* Get the binding fields for the route.
*
* @return array
*/
public function bindingFields()
{
return $this->bindingFields ?? [];
}
/**
* Set the binding fields for the route.
*
* @param array $bindingFields
* @return $this
*/
public function setBindingFields(array $bindingFields)
{
$this->bindingFields = $bindingFields;
return $this;
}
/**
* Get the parent parameter of the given parameter.
*
* @param string $parameter
* @return string
*/
public function parentOfParameter($parameter)
{
$key = array_search($parameter, array_keys($this->parameters));
if ($key === 0) {
return;
}
return array_values($this->parameters)[$key - 1];
}
/**
* Allow "trashed" models to be retrieved when resolving implicit model bindings for this route.
*
* @param bool $withTrashed
* @return $this
*/
public function withTrashed($withTrashed = true)
{
$this->withTrashedBindings = $withTrashed;
return $this;
}
/**
* Determines if the route allows "trashed" models to be retrieved when resolving implicit model bindings.
*
* @return bool
*/
public function allowsTrashedBindings()
{
return $this->withTrashedBindings;
}
/**
* Set a default value for the route.
*
* @param string $key
* @param mixed $value
* @return $this
*/
public function defaults($key, $value)
{
$this->defaults[$key] = $value;
return $this;
}
/**
* Set the default values for the route.
*
* @param array $defaults
* @return $this
*/
public function setDefaults(array $defaults)
{
$this->defaults = $defaults;
return $this;
}
/**
* Set a regular expression requirement on the route.
*
* @param array|string $name
* @param string|null $expression
* @return $this
*/
public function where($name, $expression = null)
{
foreach ($this->parseWhere($name, $expression) as $name => $expression) {
$this->wheres[$name] = $expression;
}
return $this;
}
/**
* Parse arguments to the where method into an array.
*
* @param array|string $name
* @param string $expression
* @return array
*/
protected function parseWhere($name, $expression)
{
return is_array($name) ? $name : [$name => $expression];
}
/**
* Set a list of regular expression requirements on the route.
*
* @param array $wheres
* @return $this
*/
public function setWheres(array $wheres)
{
foreach ($wheres as $name => $expression) {
$this->where($name, $expression);
}
return $this;
}
/**
* Mark this route as a fallback route.
*
* @return $this
*/
public function fallback()
{
$this->isFallback = true;
return $this;
}
/**
* Set the fallback value.
*
* @param bool $isFallback
* @return $this
*/
public function setFallback($isFallback)
{
$this->isFallback = $isFallback;
return $this;
}
/**
* Get the HTTP verbs the route responds to.
*
* @return array
*/
public function methods()
{
return $this->methods;
}
/**
* Determine if the route only responds to HTTP requests.
*
* @return bool
*/
public function httpOnly()
{
return in_array('http', $this->action, true);
}
/**
* Determine if the route only responds to HTTPS requests.
*
* @return bool
*/
public function httpsOnly()
{
return $this->secure();
}
/**
* Determine if the route only responds to HTTPS requests.
*
* @return bool
*/
public function secure()
{
return in_array('https', $this->action, true);
}
/**
* Get or set the domain for the route.
*
* @param string|null $domain
* @return $this|string|null
*/
public function domain($domain = null)
{
if (is_null($domain)) {
return $this->getDomain();
}
$parsed = RouteUri::parse($domain);
$this->action['domain'] = $parsed->uri;
$this->bindingFields = array_merge(
$this->bindingFields, $parsed->bindingFields
);
return $this;
}
/**
* Get the domain defined for the route.
*
* @return string|null
*/
public function getDomain()
{
return isset($this->action['domain'])
? str_replace(['http://', 'https://'], '', $this->action['domain']) : null;
}
/**
* Get the prefix of the route instance.
*
* @return string|null
*/
public function getPrefix()
{
return $this->action['prefix'] ?? null;
}
/**
* Add a prefix to the route URI.
*
* @param string $prefix
* @return $this
*/
public function prefix($prefix)
{
$prefix = $prefix ?? '';
$this->updatePrefixOnAction($prefix);
$uri = rtrim($prefix, '/').'/'.ltrim($this->uri, '/');
return $this->setUri($uri !== '/' ? trim($uri, '/') : $uri);
}
/**
* Update the "prefix" attribute on the action array.
*
* @param string $prefix
* @return void
*/
protected function updatePrefixOnAction($prefix)
{
if (! empty($newPrefix = trim(rtrim($prefix, '/').'/'.ltrim($this->action['prefix'] ?? '', '/'), '/'))) {
$this->action['prefix'] = $newPrefix;
}
}
/**
* Get the URI associated with the route.
*
* @return string
*/
public function uri()
{
return $this->uri;
}
/**
* Set the URI that the route responds to.
*
* @param string $uri
* @return $this
*/
public function setUri($uri)
{
$this->uri = $this->parseUri($uri);
return $this;
}
/**
* Parse the route URI and normalize / store any implicit binding fields.
*
* @param string $uri
* @return string
*/
protected function parseUri($uri)
{
$this->bindingFields = [];
return tap(RouteUri::parse($uri), function ($uri) {
$this->bindingFields = $uri->bindingFields;
})->uri;
}
/**
* Get the name of the route instance.
*
* @return string|null
*/
public function getName()
{
return $this->action['as'] ?? null;
}
/**
* Add or change the route name.
*
* @param string $name
* @return $this
*/
public function name($name)
{
$this->action['as'] = isset($this->action['as']) ? $this->action['as'].$name : $name;
return $this;
}
/**
* Determine whether the route's name matches the given patterns.
*
* @param mixed ...$patterns
* @return bool
*/
public function named(...$patterns)
{
if (is_null($routeName = $this->getName())) {
return false;
}
foreach ($patterns as $pattern) {
if (Str::is($pattern, $routeName)) {
return true;
}
}
return false;
}
/**
* Set the handler for the route.
*
* @param \Closure|array|string $action
* @return $this
*/
public function uses($action)
{
if (is_array($action)) {
$action = $action[0].'@'.$action[1];
}
$action = is_string($action) ? $this->addGroupNamespaceToStringUses($action) : $action;
return $this->setAction(array_merge($this->action, $this->parseAction([
'uses' => $action,
'controller' => $action,
])));
}
/**
* Parse a string based action for the "uses" fluent method.
*
* @param string $action
* @return string
*/
protected function addGroupNamespaceToStringUses($action)
{
$groupStack = last($this->router->getGroupStack());
if (isset($groupStack['namespace']) && strpos($action, '\\') !== 0) {
return $groupStack['namespace'].'\\'.$action;
}
return $action;
}
/**
* Get the action name for the route.
*
* @return string
*/
public function getActionName()
{
return $this->action['controller'] ?? 'Closure';
}
/**
* Get the method name of the route action.
*
* @return string
*/
public function getActionMethod()
{
return Arr::last(explode('@', $this->getActionName()));
}
/**
* Get the action array or one of its properties for the route.
*
* @param string|null $key
* @return mixed
*/
public function getAction($key = null)
{
return Arr::get($this->action, $key);
}
/**
* Set the action array for the route.
*
* @param array $action
* @return $this
*/
public function setAction(array $action)
{
$this->action = $action;
if (isset($this->action['domain'])) {
$this->domain($this->action['domain']);
}
return $this;
}
/**
* Get the value of the action that should be taken on a missing model exception.
*
* @return \Closure|null
*/
public function getMissing()
{
$missing = $this->action['missing'] ?? null;
return is_string($missing) &&
Str::startsWith($missing, [
'C:32:"Opis\\Closure\\SerializableClosure',
'O:47:"Laravel\\SerializableClosure\\SerializableClosure',
]) ? unserialize($missing) : $missing;
}
/**
* Define the callable that should be invoked on a missing model exception.
*
* @param \Closure $missing
* @return $this
*/
public function missing($missing)
{
$this->action['missing'] = $missing;
return $this;
}
/**
* Get all middleware, including the ones from the controller.
*
* @return array
*/
public function gatherMiddleware()
{
if (! is_null($this->computedMiddleware)) {
return $this->computedMiddleware;
}
$this->computedMiddleware = [];
return $this->computedMiddleware = Router::uniqueMiddleware(array_merge(
$this->middleware(), $this->controllerMiddleware()
));
}
/**
* Get or set the middlewares attached to the route.
*
* @param array|string|null $middleware
* @return $this|array
*/
public function middleware($middleware = null)
{
if (is_null($middleware)) {
return (array) ($this->action['middleware'] ?? []);
}
if (! is_array($middleware)) {
$middleware = func_get_args();
}
foreach ($middleware as $index => $value) {
$middleware[$index] = (string) $value;
}
$this->action['middleware'] = array_merge(
(array) ($this->action['middleware'] ?? []), $middleware
);
return $this;
}
/**
* Specify that the "Authorize" / "can" middleware should be applied to the route with the given options.
*
* @param string $ability
* @param array|string $models
* @return $this
*/
public function can($ability, $models = [])
{
return empty($models)
? $this->middleware(['can:'.$ability])
: $this->middleware(['can:'.$ability.','.implode(',', Arr::wrap($models))]);
}
/**
* Get the middleware for the route's controller.
*
* @return array
*/
public function controllerMiddleware()
{
if (! $this->isControllerAction()) {
return [];
}
return $this->controllerDispatcher()->getMiddleware(
$this->getController(), $this->getControllerMethod()
);
}
/**
* Specify middleware that should be removed from the given route.
*
* @param array|string $middleware
* @return $this|array
*/
public function withoutMiddleware($middleware)
{
$this->action['excluded_middleware'] = array_merge(
(array) ($this->action['excluded_middleware'] ?? []), Arr::wrap($middleware)
);
return $this;
}
/**
* Get the middleware should be removed from the route.
*
* @return array
*/
public function excludedMiddleware()
{
return (array) ($this->action['excluded_middleware'] ?? []);
}
/**
* Indicate that the route should enforce scoping of multiple implicit Eloquent bindings.
*
* @return bool
*/
public function scopeBindings()
{
$this->action['scope_bindings'] = true;
return $this;
}
/**
* Determine if the route should enforce scoping of multiple implicit Eloquent bindings.
*
* @return bool
*/
public function enforcesScopedBindings()
{
return (bool) ($this->action['scope_bindings'] ?? false);
}
/**
* Specify that the route should not allow concurrent requests from the same session.
*
* @param int|null $lockSeconds
* @param int|null $waitSeconds
* @return $this
*/
public function block($lockSeconds = 10, $waitSeconds = 10)
{
$this->lockSeconds = $lockSeconds;
$this->waitSeconds = $waitSeconds;
return $this;
}
/**
* Specify that the route should allow concurrent requests from the same session.
*
* @return $this
*/
public function withoutBlocking()
{
return $this->block(null, null);
}
/**
* Get the maximum number of seconds the route's session lock should be held for.
*
* @return int|null
*/
public function locksFor()
{
return $this->lockSeconds;
}
/**
* Get the maximum number of seconds to wait while attempting to acquire a session lock.
*
* @return int|null
*/
public function waitsFor()
{
return $this->waitSeconds;
}
/**
* Get the dispatcher for the route's controller.
*
* @return \Illuminate\Routing\Contracts\ControllerDispatcher
*/
public function controllerDispatcher()
{
if ($this->container->bound(ControllerDispatcherContract::class)) {
return $this->container->make(ControllerDispatcherContract::class);
}
return new ControllerDispatcher($this->container);
}
/**
* Get the route validators for the instance.
*
* @return array
*/
public static function getValidators()
{
if (isset(static::$validators)) {
return static::$validators;
}
// To match the route, we will use a chain of responsibility pattern with the
// validator implementations. We will spin through each one making sure it
// passes and then we will know if the route as a whole matches request.
return static::$validators = [
new UriValidator, new MethodValidator,
new SchemeValidator, new HostValidator,
];
}
/**
* Convert the route to a Symfony route.
*
* @return \Symfony\Component\Routing\Route
*/
public function toSymfonyRoute()
{
return new SymfonyRoute(
preg_replace('/\{(\w+?)\?\}/', '{$1}', $this->uri()), $this->getOptionalParameterNames(),
$this->wheres, ['utf8' => true, 'action' => $this->action],
$this->getDomain() ?: '', [], $this->methods
);
}
/**
* Get the optional parameter names for the route.
*
* @return array
*/
protected function getOptionalParameterNames()
{
preg_match_all('/\{(\w+?)\?\}/', $this->uri(), $matches);
return isset($matches[1]) ? array_fill_keys($matches[1], null) : [];
}
/**
* Get the compiled version of the route.
*
* @return \Symfony\Component\Routing\CompiledRoute
*/
public function getCompiled()
{
return $this->compiled;
}
/**
* Set the router instance on the route.
*
* @param \Illuminate\Routing\Router $router
* @return $this
*/
public function setRouter(Router $router)
{
$this->router = $router;
return $this;
}
/**
* Set the container instance on the route.
*
* @param \Illuminate\Container\Container $container
* @return $this
*/
public function setContainer(Container $container)
{
$this->container = $container;
return $this;
}
/**
* Prepare the route instance for serialization.
*
* @return void
*
* @throws \LogicException
*/
public function prepareForSerialization()
{
if ($this->action['uses'] instanceof Closure) {
$this->action['uses'] = serialize(\PHP_VERSION_ID < 70400
? new OpisSerializableClosure($this->action['uses'])
: new SerializableClosure($this->action['uses'])
);
}
if (isset($this->action['missing']) && $this->action['missing'] instanceof Closure) {
$this->action['missing'] = serialize(\PHP_VERSION_ID < 70400
? new OpisSerializableClosure($this->action['missing'])
: new SerializableClosure($this->action['missing'])
);
}
$this->compileRoute();
unset($this->router, $this->container);
}
/**
* Dynamically access route parameters.
*
* @param string $key
* @return mixed
*/
public function __get($key)
{
return $this->parameter($key);
}
}