Skip to content

Commit 34d30e8

Browse files
committed
Merge 4.0
2 parents c552e1b + decf305 commit 34d30e8

File tree

2 files changed

+22
-28
lines changed

2 files changed

+22
-28
lines changed

ApiPlatformProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -250,6 +250,7 @@ public function register(): void
250250
);
251251
});
252252

253+
$this->app->singleton(ModelMetadata::class);
253254
$this->app->bind(LoaderInterface::class, AttributeLoader::class);
254255
$this->app->bind(ClassMetadataFactoryInterface::class, ClassMetadataFactory::class);
255256
$this->app->singleton(ClassMetadataFactory::class, function (Application $app) {

Eloquent/Metadata/ModelMetadata.php

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use Illuminate\Database\Eloquent\Model;
1717
use Illuminate\Database\Eloquent\Relations\Relation;
1818
use Illuminate\Support\Collection;
19-
use Illuminate\Support\Facades\Gate;
2019
use Illuminate\Support\Str;
2120

2221
/**
@@ -26,6 +25,16 @@
2625
*/
2726
final class ModelMetadata
2827
{
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+
2938
/**
3039
* The methods that can be called in a model to indicate a relation.
3140
*
@@ -45,31 +54,25 @@ final class ModelMetadata
4554
'morphedByMany',
4655
];
4756

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-
5857
/**
5958
* Gets the column attributes for the given model.
6059
*
6160
* @return Collection<string, mixed>
6261
*/
6362
public function getAttributes(Model $model): Collection
6463
{
64+
if (isset($this->attributesLocalCache[$model::class])) {
65+
return $this->attributesLocalCache[$model::class];
66+
}
67+
6568
$connection = $model->getConnection();
6669
$schema = $connection->getSchemaBuilder();
6770
$table = $model->getTable();
6871
$columns = $schema->getColumns($table);
6972
$indexes = $schema->getIndexes($table);
7073
$relations = $this->getRelations($model);
7174

72-
return collect($columns)
75+
return $this->attributesLocalCache[$model::class] = collect($columns)
7376
->reject(
7477
fn ($column) => $relations->contains(
7578
fn ($relation) => $relation['foreign_key'] === $column['name']
@@ -112,7 +115,7 @@ private function isColumnPrimaryKey(array $indexes, string $column): bool
112115
*
113116
* @return Collection<int, mixed>
114117
*/
115-
public function getVirtualAttributes(Model $model, array $columns): Collection
118+
private function getVirtualAttributes(Model $model, array $columns): Collection
116119
{
117120
$class = new \ReflectionClass($model);
118121

@@ -155,7 +158,11 @@ public function getVirtualAttributes(Model $model, array $columns): Collection
155158
*/
156159
public function getRelations(Model $model): Collection
157160
{
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))
159166
->map(fn ($method) => new \ReflectionMethod($model, $method))
160167
->reject(
161168
fn (\ReflectionMethod $method) => $method->isStatic()
@@ -207,20 +214,6 @@ public function getRelations(Model $model): Collection
207214
->values();
208215
}
209216

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-
224217
/**
225218
* Gets the cast type for the given column.
226219
*/

0 commit comments

Comments
 (0)