| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Illuminate\Routing;
|
|
|
4 |
|
|
|
5 |
use Closure;
|
|
|
6 |
|
|
|
7 |
class MiddlewareNameResolver
|
|
|
8 |
{
|
|
|
9 |
/**
|
|
|
10 |
* Resolve the middleware name to a class name(s) preserving passed parameters.
|
|
|
11 |
*
|
|
|
12 |
* @param \Closure|string $name
|
|
|
13 |
* @param array $map
|
|
|
14 |
* @param array $middlewareGroups
|
|
|
15 |
* @return \Closure|string|array
|
|
|
16 |
*/
|
|
|
17 |
public static function resolve($name, $map, $middlewareGroups)
|
|
|
18 |
{
|
|
|
19 |
// When the middleware is simply a Closure, we will return this Closure instance
|
|
|
20 |
// directly so that Closures can be registered as middleware inline, which is
|
|
|
21 |
// convenient on occasions when the developers are experimenting with them.
|
|
|
22 |
if ($name instanceof Closure) {
|
|
|
23 |
return $name;
|
|
|
24 |
}
|
|
|
25 |
|
|
|
26 |
if (isset($map[$name]) && $map[$name] instanceof Closure) {
|
|
|
27 |
return $map[$name];
|
|
|
28 |
}
|
|
|
29 |
|
|
|
30 |
// If the middleware is the name of a middleware group, we will return the array
|
|
|
31 |
// of middlewares that belong to the group. This allows developers to group a
|
|
|
32 |
// set of middleware under single keys that can be conveniently referenced.
|
|
|
33 |
if (isset($middlewareGroups[$name])) {
|
|
|
34 |
return static::parseMiddlewareGroup($name, $map, $middlewareGroups);
|
|
|
35 |
}
|
|
|
36 |
|
|
|
37 |
// Finally, when the middleware is simply a string mapped to a class name the
|
|
|
38 |
// middleware name will get parsed into the full class name and parameters
|
|
|
39 |
// which may be run using the Pipeline which accepts this string format.
|
|
|
40 |
[$name, $parameters] = array_pad(explode(':', $name, 2), 2, null);
|
|
|
41 |
|
|
|
42 |
return ($map[$name] ?? $name).(! is_null($parameters) ? ':'.$parameters : '');
|
|
|
43 |
}
|
|
|
44 |
|
|
|
45 |
/**
|
|
|
46 |
* Parse the middleware group and format it for usage.
|
|
|
47 |
*
|
|
|
48 |
* @param string $name
|
|
|
49 |
* @param array $map
|
|
|
50 |
* @param array $middlewareGroups
|
|
|
51 |
* @return array
|
|
|
52 |
*/
|
|
|
53 |
protected static function parseMiddlewareGroup($name, $map, $middlewareGroups)
|
|
|
54 |
{
|
|
|
55 |
$results = [];
|
|
|
56 |
|
|
|
57 |
foreach ($middlewareGroups[$name] as $middleware) {
|
|
|
58 |
// If the middleware is another middleware group we will pull in the group and
|
|
|
59 |
// merge its middleware into the results. This allows groups to conveniently
|
|
|
60 |
// reference other groups without needing to repeat all their middlewares.
|
|
|
61 |
if (isset($middlewareGroups[$middleware])) {
|
|
|
62 |
$results = array_merge($results, static::parseMiddlewareGroup(
|
|
|
63 |
$middleware, $map, $middlewareGroups
|
|
|
64 |
));
|
|
|
65 |
|
|
|
66 |
continue;
|
|
|
67 |
}
|
|
|
68 |
|
|
|
69 |
[$middleware, $parameters] = array_pad(
|
|
|
70 |
explode(':', $middleware, 2), 2, null
|
|
|
71 |
);
|
|
|
72 |
|
|
|
73 |
// If this middleware is actually a route middleware, we will extract the full
|
|
|
74 |
// class name out of the middleware list now. Then we'll add the parameters
|
|
|
75 |
// back onto this class' name so the pipeline will properly extract them.
|
|
|
76 |
if (isset($map[$middleware])) {
|
|
|
77 |
$middleware = $map[$middleware];
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
$results[] = $middleware.($parameters ? ':'.$parameters : '');
|
|
|
81 |
}
|
|
|
82 |
|
|
|
83 |
return $results;
|
|
|
84 |
}
|
|
|
85 |
}
|