generated from glhd/laravel-package-template
-
Notifications
You must be signed in to change notification settings - Fork 15
/
ParentResolver.php
97 lines (78 loc) · 2.66 KB
/
ParentResolver.php
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
<?php
namespace Glhd\Gretel\Resolvers;
use Arr;
use Closure;
use Glhd\Gretel\Exceptions\UnmatchedRouteException;
use Glhd\Gretel\Exceptions\UnresolvableParentException;
use Glhd\Gretel\Registry;
use Glhd\Gretel\Routing\RouteBreadcrumb;
use Illuminate\Http\Request;
use Illuminate\Support\Str;
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class ParentResolver extends Resolver
{
public static function make($value, string $name, ?Closure $relation = null): Resolver
{
$value = static::wrapClosure($value);
$relation = static::wrapNullableClosure($relation);
$callback = static function() use ($value, $name, $relation) {
return [$value, $name, $relation];
};
return new static($callback);
}
public function resolve(array $parameters, Registry $registry)
{
[$callback, $name, $relation] = parent::resolve($parameters, $registry);
$result = $callback($parameters);
if (null === $result) {
return null;
}
if ($relation) {
$parameters = Arr::wrap($relation($parameters));
}
// Handle parent shorthand
if (is_string($result) && 0 === strpos($result, '.')) {
$result = Str::beforeLast($name, '.').$result;
}
// If we get back a route name, we'll load it from the registry and pass
// on any custom parameters that were provided
if (is_string($result) && $registry->has($result)) {
$result = $registry->getOrFail($result);
}
// If we get back a URL, we'll try to resolve the parent via the Router
// This may not last in the API — use at your own risk
if (is_string($result) && filter_var($result, FILTER_VALIDATE_URL)) {
return $this->findParentByUrl($result, $registry);
}
if (! ($result instanceof RouteBreadcrumb)) {
throw new UnresolvableParentException($result, $name);
}
if (! empty($parameters)) {
$result = clone $result;
$result->setParameters($parameters);
}
return $result;
}
/**
* Please note that this behavior is intentionally undocumented and may be
* removed at any time. Use at your own risk.
*/
protected function findParentByUrl(string $url, Registry $registry): RouteBreadcrumb
{
$router = app('router');
$request = Request::createFromBase(SymfonyRequest::create($url));
try {
$route = $router->getRoutes()->match($request);
} catch (NotFoundHttpException $exception) {
throw new UnmatchedRouteException($url);
}
if ($route->hasParameters()) {
$router->substituteBindings($route);
$router->substituteImplicitBindings($route);
}
$breadcrumb = $registry->getOrFail($route);
$breadcrumb->setParameters($route->parameters());
return $breadcrumb;
}
}