1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\EloquentInspector; |
4
|
|
|
|
5
|
|
|
use Cerbero\EloquentInspector\Components\Properties; |
6
|
|
|
use Cerbero\EloquentInspector\Components\Relationships; |
7
|
|
|
use Cerbero\EloquentInspector\Components\UseStatements; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The Eloquent inspector. |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
class Inspector |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The inspector instances. |
17
|
|
|
* |
18
|
|
|
* @var array |
19
|
|
|
*/ |
20
|
|
|
protected static $instances = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The `use` statements cache. |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
protected $useStatements; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The properties cache. |
31
|
|
|
* |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $properties; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The relationships cache. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $relationships; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Instantiate the class. |
45
|
|
|
* |
46
|
|
|
* @param string $model |
47
|
|
|
*/ |
48
|
3 |
|
protected function __construct(protected string $model) |
49
|
|
|
{ |
50
|
3 |
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Statically instantiate the class |
54
|
|
|
* |
55
|
|
|
* @param string $model |
56
|
|
|
* @return static |
57
|
|
|
*/ |
58
|
10 |
|
public static function inspect(string $model): static |
59
|
|
|
{ |
60
|
10 |
|
return static::$instances[$model] ??= new static($model); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Clean up the given model information |
65
|
|
|
* |
66
|
|
|
* @param string|null $model |
67
|
|
|
* @return void |
68
|
|
|
*/ |
69
|
2 |
|
public static function flush(string $model = null): void |
70
|
|
|
{ |
71
|
2 |
|
if ($model) { |
72
|
1 |
|
unset(static::$instances[$model]); |
73
|
|
|
} else { |
74
|
1 |
|
static::$instances = []; |
75
|
|
|
} |
76
|
2 |
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Clean up information of the current instance |
80
|
|
|
* |
81
|
|
|
* @return void |
82
|
|
|
*/ |
83
|
1 |
|
public function forget(): void |
84
|
|
|
{ |
85
|
1 |
|
unset(static::$instances[$this->model]); |
86
|
1 |
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Retrieve the inspected model class |
90
|
|
|
* |
91
|
|
|
* @return string |
92
|
|
|
*/ |
93
|
1 |
|
public function getModel(): string |
94
|
|
|
{ |
95
|
1 |
|
return $this->model; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Retrieve the `use` statements |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
2 |
|
public function getUseStatements(): array |
104
|
|
|
{ |
105
|
2 |
|
return $this->useStatements ??= UseStatements::of($this->model)->get(); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Retrieve the properties |
110
|
|
|
* |
111
|
|
|
* @return array |
112
|
|
|
*/ |
113
|
1 |
|
public function getProperties(): array |
114
|
|
|
{ |
115
|
1 |
|
return $this->properties ??= Properties::of($this->model)->get(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retrieve the relationships |
120
|
|
|
* |
121
|
|
|
* @return array |
122
|
|
|
*/ |
123
|
1 |
|
public function getRelationships(): array |
124
|
|
|
{ |
125
|
1 |
|
return $this->relationships ??= Relationships::of($this->model)->get(); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|