From 28fbecbed718c7adf3e2bfa1bd72ffab8b4cefb1 Mon Sep 17 00:00:00 2001 From: ArrayIterator <37867551+ArrayIterator@users.noreply.github.com> Date: Fri, 19 Jul 2024 02:20:36 +0700 Subject: [PATCH] Update PermissionWrapper.php --- src/Database/Wrapper/PermissionWrapper.php | 35 ++++++++-------------- 1 file changed, 13 insertions(+), 22 deletions(-) diff --git a/src/Database/Wrapper/PermissionWrapper.php b/src/Database/Wrapper/PermissionWrapper.php index 384da46..2483ae2 100644 --- a/src/Database/Wrapper/PermissionWrapper.php +++ b/src/Database/Wrapper/PermissionWrapper.php @@ -32,7 +32,7 @@ class PermissionWrapper implements PermissionInterface private array $cachedInvalidEntities = []; /** - * @var ?array + * @var ?array */ private ?array $availableIdentities = null; @@ -93,6 +93,7 @@ public function getCapabilityEntityFactory(): ?CapabilityEntityFactoryInterface public function setCapabilityEntityFactory(CapabilityEntityFactoryInterface $capabilityEntityFactory): void { if ($capabilityEntityFactory !== $this->capabilityEntityFactory) { + $this->cachedInvalidEntities = []; $this->availableIdentities = null; } $this->capabilityEntityFactory = $capabilityEntityFactory; @@ -181,24 +182,11 @@ public function get(CapabilityInterface|string $identity): ?CapabilityInterface if (isset($this->cachedInvalidEntities[$identity])) { return null; } - - $em = $this->getConnection()->getEntityManager(); - $this->availableIdentities ??= $this->getCapabilities(); - - if (!isset($this->availableIdentities[$identity])) { - return null; - } - - $entity = $factory->createEntity( - $this->getConnection()->getEntityManager(), - $identity - ); - if (!$entity) { + if (!isset($this->getCapabilities()[$identity])) { $this->cachedInvalidEntities[$identity] = false; return null; } - $this->availableIdentities[$identity] = true; - $this->add($entity); + } return $this->permission->get($identity); @@ -219,12 +207,15 @@ public function getCapabilities(): array { $factory = $this->getCapabilityEntityFactory(); if ($this->availableIdentities === null && $factory) { - IterableHelper::each( - $factory->getCapabilityIdentities($this->getConnection()->getEntityManager()), - function (&$key, $value) { - $key = strtolower($value); - $this->availableIdentities[$key] = $value; - return false; + $this->availableIdentities = []; + IterableHelper::every( + $this + ->getCapabilityEntityFactory() + ->all($this->getConnection()->getEntityManager()), + function ($e, CapabilityInterface $capability) { + $identity = $this->identify($capability); + $this->availableIdentities[$identity] = true; + $this->permission->add($capability); } ); }