diff --git a/src/Database/Document.php b/src/Database/Document.php index d50a957ca..6a0449bdf 100644 --- a/src/Database/Document.php +++ b/src/Database/Document.php @@ -15,6 +15,9 @@ class Document extends ArrayObject public const SET_TYPE_PREPEND = 'prepend'; public const SET_TYPE_APPEND = 'append'; + /** @var array> */ + private array $permissionCache = []; + /** * Construct. * @@ -144,16 +147,21 @@ public function getWrite(): array */ public function getPermissionsByType(string $type): array { - $typePermissions = []; + if (isset($this->permissionCache[$type])) { + return $this->permissionCache[$type]; + } + + $prefix = $type . '("'; + $prefixLen = \strlen($prefix); + $result = []; foreach ($this->getPermissions() as $permission) { - if (!\str_starts_with($permission, $type)) { - continue; + if (\str_starts_with($permission, $prefix)) { + $result[] = \substr($permission, $prefixLen, -2); } - $typePermissions[] = \str_replace([$type . '(', ')', '"', ' '], '', $permission); } - return \array_unique($typePermissions); + return $this->permissionCache[$type] = \array_unique($result); } /** @@ -243,6 +251,10 @@ public function getAttribute(string $name, mixed $default = null): mixed */ public function setAttribute(string $key, mixed $value, string $type = self::SET_TYPE_ASSIGN): static { + if ($key === '$permissions') { + $this->permissionCache = []; + } + switch ($type) { case self::SET_TYPE_ASSIGN: $this[$key] = $value;