Skip to content

Commit

Permalink
Improve caching of reflection attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
WendellAdriel committed May 6, 2024
1 parent ae98c5e commit 639b496
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/Models/Concerns/Virtue.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ trait Virtue
{
use HasRelations;

private ?Collection $classAttributes = null;
/** @var array<class-string,Collection>|null */
private static ?array $classAttributes = [];

public function initializeVirtue(): void
{
Expand Down Expand Up @@ -164,11 +165,12 @@ private function resolveMultipleAttributes(string $attributeClass): Collection

private function classAttributes(): Collection
{
if (is_null($this->classAttributes)) {
$class = static::class;
if (! array_key_exists($class, self::$classAttributes) || is_null(self::$classAttributes[$class])) {
$reflectionClass = new ReflectionClass(static::class);
$this->classAttributes = Collection::make($reflectionClass->getAttributes());
self::$classAttributes[$class] = Collection::make($reflectionClass->getAttributes());
}

return $this->classAttributes;
return self::$classAttributes[$class];
}
}

0 comments on commit 639b496

Please sign in to comment.