-
-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathPendingRouteAction.php
More file actions
117 lines (95 loc) · 3.03 KB
/
Copy pathPendingRouteAction.php
File metadata and controls
117 lines (95 loc) · 3.03 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
<?php
namespace Spatie\RouteDiscovery\PendingRoutes;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
use ReflectionAttribute;
use ReflectionMethod;
use ReflectionParameter;
use Spatie\RouteDiscovery\Attributes\DiscoveryAttribute;
use Spatie\RouteDiscovery\Attributes\Route;
class PendingRouteAction
{
public ReflectionMethod $method;
public string $uri;
/** @var array<int, string> */
public array $methods = [];
/** @var array{class-string, string} */
public array $action;
/** @var array<int, class-string> */
public array $middleware = [];
/** @var array<int, \Spatie\RouteDiscovery\Attributes\WhereAttribute> */
public array $wheres = [];
public ?string $name = null;
/**
* @param ReflectionMethod $method
* @param class-string $controllerClass
*/
public function __construct(ReflectionMethod $method, string $controllerClass)
{
$this->method = $method;
$this->uri = $this->relativeUri();
$this->methods = $this->discoverHttpMethods();
$this->action = [$controllerClass, $method->name];
}
public function relativeUri(): string
{
/** @var ReflectionParameter $modelParameter */
$modelParameter = collect($this->method->getParameters())->first(function (ReflectionParameter $parameter) {
/** @phpstan-ignore-next-line */
return is_a($parameter->getType()?->getName(), Model::class, true);
});
$uri = '';
if (! in_array($this->method->getName(), $this->commonControllerMethodNames())) {
$uri = Str::kebab($this->method->getName());
}
/** @phpstan-ignore-next-line */
if ($modelParameter) {
if ($uri !== '') {
$uri .= '/';
}
$uri .= "{{$modelParameter->getName()}}";
}
return $uri;
}
/**
* @return array<int, string>
*/
protected function discoverHttpMethods(): array
{
return match ($this->method->name) {
'index', 'create', 'show', 'edit' => ['GET'],
'store' => ['POST'],
'update' => ['PUT', 'PATCH'],
'destroy', 'delete' => ['DELETE'],
default => ['GET'],
};
}
/** @return array<int, string> */
protected function commonControllerMethodNames(): array
{
return [
'index', '__invoke', 'get',
'show', 'store', 'update',
'destroy', 'delete',
];
}
public function getRouteAttribute(): ?Route
{
return $this->getAttribute(Route::class);
}
/**
* @template T of DiscoveryAttribute
*
* @param class-string<T> $attributeClass
*
* @return ?T
*/
public function getAttribute(string $attributeClass): ?DiscoveryAttribute
{
$attributes = $this->method->getAttributes($attributeClass, ReflectionAttribute::IS_INSTANCEOF);
if (! count($attributes)) {
return null;
}
return $attributes[0]->newInstance();
}
}