|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Rexlabs\Laravel\Smokescreen\Console; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use ReflectionMethod; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* The includes listing. |
|
10
|
|
|
* |
|
11
|
|
|
*/ |
|
12
|
|
|
class IncludesListing |
|
13
|
|
|
{ |
|
14
|
|
|
/** |
|
15
|
|
|
* The map of available relations. |
|
16
|
|
|
* |
|
17
|
|
|
* @var array |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $relationsMap = [ |
|
20
|
|
|
'hasOne' => 'item', |
|
21
|
|
|
'morphOne' => 'item', |
|
22
|
|
|
'belongsTo' => 'item', |
|
23
|
|
|
'morphTo' => 'item', |
|
24
|
|
|
'hasMany' => 'collection', |
|
25
|
|
|
'hasManyThrough' => 'collection', |
|
26
|
|
|
'morphMany' => 'collection', |
|
27
|
|
|
'belongsToMany' => 'collection', |
|
28
|
|
|
'morphToMany' => 'collection', |
|
29
|
|
|
'morphedByMany' => 'collection', |
|
30
|
|
|
]; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* List the includes of the given Eloquent model. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string $class |
|
36
|
|
|
* @return array |
|
37
|
|
|
*/ |
|
38
|
|
|
public function listForEloquent(string $class) : array |
|
39
|
|
|
{ |
|
40
|
|
|
$list = []; |
|
41
|
|
|
$methods = (new ReflectionClass($class))->getMethods(ReflectionMethod::IS_PUBLIC); |
|
42
|
|
|
$declaredMethods = array_filter($methods, function ($method) use ($class) { |
|
43
|
|
|
return $method->getDeclaringClass()->getName() == $class; |
|
44
|
|
|
}); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($declaredMethods as $method) { |
|
47
|
|
|
if ($type = $this->getResourceTypeByMethodReturnType($method)) { |
|
48
|
|
|
$list[$method->getName()] = "relation|{$type}"; |
|
49
|
|
|
} elseif ($type = $this->getResourceTypeByMethodDefinition($method)) { |
|
50
|
|
|
$list[$method->getName()] = "relation|{$type}"; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $list; |
|
55
|
|
|
|
|
56
|
|
|
// implement getResourceTypeByMethodDefinition |
|
57
|
|
|
// investigate custom relation names with actual relation name |
|
58
|
|
|
// ^ if different may not relate on $method->getName() |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Retrieve the type of the resource based on the given method return type. |
|
63
|
|
|
* |
|
64
|
|
|
* @param ReflectionMethod $method |
|
65
|
|
|
* @return ?string |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function getResourceTypeByMethodReturnType(ReflectionMethod $method) : ?string |
|
68
|
|
|
{ |
|
69
|
|
|
$returnType = (string) $method->getReturnType(); |
|
70
|
|
|
$namespace = 'Illuminate\Database\Eloquent\Relations'; |
|
71
|
|
|
|
|
72
|
|
|
if (!starts_with($returnType, $namespace)) { |
|
73
|
|
|
return null; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
$relation = lcfirst(class_basename($returnType)); |
|
77
|
|
|
|
|
78
|
|
|
return $this->relationsMap[$relation] ?? null; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Retrieve the type of the resource based on the given method definition. |
|
83
|
|
|
* |
|
84
|
|
|
* @param ReflectionMethod $method |
|
85
|
|
|
* @return ?string |
|
86
|
|
|
*/ |
|
87
|
|
|
protected function getResourceTypeByMethodDefinition(ReflectionMethod $method) : ?string |
|
|
|
|
|
|
88
|
|
|
{ |
|
89
|
|
|
return null; |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.