-
Notifications
You must be signed in to change notification settings - Fork 199
Expand file tree
/
Copy pathRelationFinder.php
More file actions
109 lines (90 loc) · 3.63 KB
/
Copy pathRelationFinder.php
File metadata and controls
109 lines (90 loc) · 3.63 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
<?php
namespace BeyondCode\ErdGenerator;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasOneOrMany;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Collection;
use ReflectionClass;
use ReflectionMethod;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;
class RelationFinder
{
/**
* Return all relations from a fully qualified model class name.
*
* @param string $model
* @return Collection
* @throws \ReflectionException
*/
public function getModelRelations(string $model)
{
$class = new ReflectionClass($model);
$traitMethods = Collection::make($class->getTraits())->map(function (ReflectionClass $trait) {
return Collection::make($trait->getMethods(ReflectionMethod::IS_PUBLIC));
})->flatten();
$methods = Collection::make($class->getMethods(ReflectionMethod::IS_PUBLIC))
->merge($traitMethods)
->reject(function (ReflectionMethod $method) use ($model) {
return $method->class !== $model || $method->getNumberOfParameters() > 0 || $method->isStatic();
});
$relations = Collection::make();
$methods->map(function (ReflectionMethod $method) use ($model, &$relations) {
$relations = $relations->merge($this->getRelationshipFromMethodAndModel($method, $model));
});
$relations = $relations->filter();
if ($ignoreRelations = Arr::get(config('erd-generator.ignore', []),$model))
{
$relations = $relations->diffKeys(array_flip($ignoreRelations));
}
return $relations;
}
/**
* @param string $qualifiedKeyName
* @return mixed
*/
protected function getParentKey(string $qualifiedKeyName)
{
$segments = explode('.', $qualifiedKeyName);
return end($segments);
}
/**
* @param ReflectionMethod $method
* @param string $model
* @return array|null
*/
protected function getRelationshipFromMethodAndModel(ReflectionMethod $method, string $model)
{
try {
$return = $method->invoke(app($model));
if ($return instanceof Relation) {
$localKey = null;
$foreignKey = null;
if ($return instanceof HasOneOrMany) {
$localKey = $this->getParentKey($return->getQualifiedParentKeyName());
$foreignKey = $return->getForeignKeyName();
}
if ($return instanceof BelongsTo) {
$foreignKey = $this->getParentKey($return->getQualifiedOwnerKeyName());
$localKey = method_exists($return, 'getForeignKeyName') ? $return->getForeignKeyName() : $return->getForeignKey();
}
return [
$method->getName() => new ModelRelation(
$method->getShortName(),
(new ReflectionClass($return))->getShortName(),
(new ReflectionClass($return->getRelated()))->getName(),
$localKey,
$foreignKey
)
];
}
} catch (\Throwable $e) {
// Non-relation methods may throw when invoked; this is expected.
// To enable debug logging, set 'debug_relations' => true in config/erd-generator.php
if (config('erd-generator.debug_relations', false)) {
Log::debug("Could not analyze method {$method->getName()} on {$model}: {$e->getMessage()}");
}
}
return null;
}
}