| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Routing;
|
|
|
4 |
|
|
|
5 |
use Illuminate\Support\Arr;
|
|
|
6 |
|
|
|
7 |
class RouteParameterBinder
|
|
|
8 |
{
|
|
|
9 |
/**
|
|
|
10 |
* The route instance.
|
|
|
11 |
*
|
|
|
12 |
* @var \Illuminate\Routing\Route
|
|
|
13 |
*/
|
|
|
14 |
protected $route;
|
|
|
15 |
|
|
|
16 |
/**
|
|
|
17 |
* Create a new Route parameter binder instance.
|
|
|
18 |
*
|
|
|
19 |
* @param \Illuminate\Routing\Route $route
|
|
|
20 |
* @return void
|
|
|
21 |
*/
|
|
|
22 |
public function __construct($route)
|
|
|
23 |
{
|
|
|
24 |
$this->route = $route;
|
|
|
25 |
}
|
|
|
26 |
|
|
|
27 |
/**
|
|
|
28 |
* Get the parameters for the route.
|
|
|
29 |
*
|
|
|
30 |
* @param \Illuminate\Http\Request $request
|
|
|
31 |
* @return array
|
|
|
32 |
*/
|
|
|
33 |
public function parameters($request)
|
|
|
34 |
{
|
|
|
35 |
$parameters = $this->bindPathParameters($request);
|
|
|
36 |
|
|
|
37 |
// If the route has a regular expression for the host part of the URI, we will
|
|
|
38 |
// compile that and get the parameter matches for this domain. We will then
|
|
|
39 |
// merge them into this parameters array so that this array is completed.
|
|
|
40 |
if (! is_null($this->route->compiled->getHostRegex())) {
|
|
|
41 |
$parameters = $this->bindHostParameters(
|
|
|
42 |
$request, $parameters
|
|
|
43 |
);
|
|
|
44 |
}
|
|
|
45 |
|
|
|
46 |
return $this->replaceDefaults($parameters);
|
|
|
47 |
}
|
|
|
48 |
|
|
|
49 |
/**
|
|
|
50 |
* Get the parameter matches for the path portion of the URI.
|
|
|
51 |
*
|
|
|
52 |
* @param \Illuminate\Http\Request $request
|
|
|
53 |
* @return array
|
|
|
54 |
*/
|
|
|
55 |
protected function bindPathParameters($request)
|
|
|
56 |
{
|
|
|
57 |
$path = '/'.ltrim($request->decodedPath(), '/');
|
|
|
58 |
|
|
|
59 |
preg_match($this->route->compiled->getRegex(), $path, $matches);
|
|
|
60 |
|
|
|
61 |
return $this->matchToKeys(array_slice($matches, 1));
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
/**
|
|
|
65 |
* Extract the parameter list from the host part of the request.
|
|
|
66 |
*
|
|
|
67 |
* @param \Illuminate\Http\Request $request
|
|
|
68 |
* @param array $parameters
|
|
|
69 |
* @return array
|
|
|
70 |
*/
|
|
|
71 |
protected function bindHostParameters($request, $parameters)
|
|
|
72 |
{
|
|
|
73 |
preg_match($this->route->compiled->getHostRegex(), $request->getHost(), $matches);
|
|
|
74 |
|
|
|
75 |
return array_merge($this->matchToKeys(array_slice($matches, 1)), $parameters);
|
|
|
76 |
}
|
|
|
77 |
|
|
|
78 |
/**
|
|
|
79 |
* Combine a set of parameter matches with the route's keys.
|
|
|
80 |
*
|
|
|
81 |
* @param array $matches
|
|
|
82 |
* @return array
|
|
|
83 |
*/
|
|
|
84 |
protected function matchToKeys(array $matches)
|
|
|
85 |
{
|
|
|
86 |
if (empty($parameterNames = $this->route->parameterNames())) {
|
|
|
87 |
return [];
|
|
|
88 |
}
|
|
|
89 |
|
|
|
90 |
$parameters = array_intersect_key($matches, array_flip($parameterNames));
|
|
|
91 |
|
|
|
92 |
return array_filter($parameters, function ($value) {
|
|
|
93 |
return is_string($value) && strlen($value) > 0;
|
|
|
94 |
});
|
|
|
95 |
}
|
|
|
96 |
|
|
|
97 |
/**
|
|
|
98 |
* Replace null parameters with their defaults.
|
|
|
99 |
*
|
|
|
100 |
* @param array $parameters
|
|
|
101 |
* @return array
|
|
|
102 |
*/
|
|
|
103 |
protected function replaceDefaults(array $parameters)
|
|
|
104 |
{
|
|
|
105 |
foreach ($parameters as $key => $value) {
|
|
|
106 |
$parameters[$key] = $value ?? Arr::get($this->route->defaults, $key);
|
|
|
107 |
}
|
|
|
108 |
|
|
|
109 |
foreach ($this->route->defaults as $key => $value) {
|
|
|
110 |
if (! isset($parameters[$key])) {
|
|
|
111 |
$parameters[$key] = $value;
|
|
|
112 |
}
|
|
|
113 |
}
|
|
|
114 |
|
|
|
115 |
return $parameters;
|
|
|
116 |
}
|
|
|
117 |
}
|