16
16
use Illuminate \Database \Eloquent \Model ;
17
17
use Illuminate \Database \Eloquent \Relations \Relation ;
18
18
use Illuminate \Support \Collection ;
19
- use Illuminate \Support \Facades \Gate ;
20
19
use Illuminate \Support \Str ;
21
20
22
21
/**
26
25
*/
27
26
final class ModelMetadata
28
27
{
28
+ /**
29
+ * @var array<class-string, Collection<string, mixed>>
30
+ */
31
+ private $ attributesLocalCache = [];
32
+
33
+ /**
34
+ * @var array<class-string, Collection<int, mixed>>
35
+ */
36
+ private $ relationsLocalCache = [];
37
+
29
38
/**
30
39
* The methods that can be called in a model to indicate a relation.
31
40
*
@@ -45,31 +54,25 @@ final class ModelMetadata
45
54
'morphedByMany ' ,
46
55
];
47
56
48
- /**
49
- * Gets the first policy associated with this model.
50
- */
51
- public function getPolicy (Model $ model ): ?string
52
- {
53
- $ policy = Gate::getPolicyFor ($ model ::class);
54
-
55
- return $ policy ? $ policy ::class : null ;
56
- }
57
-
58
57
/**
59
58
* Gets the column attributes for the given model.
60
59
*
61
60
* @return Collection<string, mixed>
62
61
*/
63
62
public function getAttributes (Model $ model ): Collection
64
63
{
64
+ if (isset ($ this ->attributesLocalCache [$ model ::class])) {
65
+ return $ this ->attributesLocalCache [$ model ::class];
66
+ }
67
+
65
68
$ connection = $ model ->getConnection ();
66
69
$ schema = $ connection ->getSchemaBuilder ();
67
70
$ table = $ model ->getTable ();
68
71
$ columns = $ schema ->getColumns ($ table );
69
72
$ indexes = $ schema ->getIndexes ($ table );
70
73
$ relations = $ this ->getRelations ($ model );
71
74
72
- return collect ($ columns )
75
+ return $ this -> attributesLocalCache [ $ model ::class] = collect ($ columns )
73
76
->reject (
74
77
fn ($ column ) => $ relations ->contains (
75
78
fn ($ relation ) => $ relation ['foreign_key ' ] === $ column ['name ' ]
@@ -112,7 +115,7 @@ private function isColumnPrimaryKey(array $indexes, string $column): bool
112
115
*
113
116
* @return Collection<int, mixed>
114
117
*/
115
- public function getVirtualAttributes (Model $ model , array $ columns ): Collection
118
+ private function getVirtualAttributes (Model $ model , array $ columns ): Collection
116
119
{
117
120
$ class = new \ReflectionClass ($ model );
118
121
@@ -155,7 +158,11 @@ public function getVirtualAttributes(Model $model, array $columns): Collection
155
158
*/
156
159
public function getRelations (Model $ model ): Collection
157
160
{
158
- return collect (get_class_methods ($ model ))
161
+ if (isset ($ this ->relationsLocalCache [$ model ::class])) {
162
+ return $ this ->relationsLocalCache [$ model ::class];
163
+ }
164
+
165
+ return $ this ->relationsLocalCache [$ model ::class] = collect (get_class_methods ($ model ))
159
166
->map (fn ($ method ) => new \ReflectionMethod ($ model , $ method ))
160
167
->reject (
161
168
fn (\ReflectionMethod $ method ) => $ method ->isStatic ()
@@ -207,20 +214,6 @@ public function getRelations(Model $model): Collection
207
214
->values ();
208
215
}
209
216
210
- /**
211
- * Gets the Events that the model dispatches.
212
- *
213
- * @return Collection<int, mixed>
214
- */
215
- public function getEvents (Model $ model ): Collection
216
- {
217
- return collect ($ model ->dispatchesEvents ())
218
- ->map (fn (string $ class , string $ event ) => [
219
- 'event ' => $ event ,
220
- 'class ' => $ class ,
221
- ])->values ();
222
- }
223
-
224
217
/**
225
218
* Gets the cast type for the given column.
226
219
*/
0 commit comments