From c41a59ad1d3fc5fe6c52fce237d9b1fbb4c46f8e Mon Sep 17 00:00:00 2001 From: Dennis Eichhorn Date: Sat, 7 Sep 2019 08:45:24 +0200 Subject: [PATCH] try to improve performance --- Account/PermissionAbstract.php | 37 ++++++++++++++++++++++++++++- Account/PermissionHandlingTrait.php | 25 +++++++++++-------- Uri/UriFactory.php | 16 +++++-------- 3 files changed, 57 insertions(+), 21 deletions(-) diff --git a/Account/PermissionAbstract.php b/Account/PermissionAbstract.php index d56ef19c1..17cb5ad26 100644 --- a/Account/PermissionAbstract.php +++ b/Account/PermissionAbstract.php @@ -375,11 +375,46 @@ class PermissionAbstract implements \JsonSerializable * * @since 1.0.0 */ - public function hasPermission(int $permission) : bool + public function hasPermissionFlags(int $permission) : bool { return ($this->permission | $permission) === $this->permission; } + /** + * Has permissions. + * + * Checks if the permission is defined + * + * @param int $permission Permission to check + * @param null|int $unit Unit Unit to check (null if all are acceptable) + * @param null|string $app App App to check (null if all are acceptable) + * @param null|string $module Module Module to check (null if all are acceptable) + * @param null|int $type Type (e.g. customer) (null if all are acceptable) + * @param null|int $element (e.g. customer id) (null if all are acceptable) + * @param null|int $component (e.g. address) (null if all are acceptable) + * + * @return bool Returns true if the permission is set, false otherwise + * + * @since 1.0.0 + */ + public function hasPermission( + int $permission, + int $unit = null, + string $app = null, + string $module = null, + int $type = null, + int $element = null, + int $component = null + ) { + return ($unit === null || $this->unit === null || $this->unit === $unit) + && ($app === null || $this->app === null || $this->app === $app) + && ($module === null || $this->module === null || $this->module === $module) + && ($type === null || $this->type === null || $this->type === $type) + && ($element === null || $this->element === null || $this->element === $element) + && ($component === null || $this->component === null || $this->component === $component) + && ($this->permission | $permission) === $this->permission; + } + /** * {@inheritdoc} */ diff --git a/Account/PermissionHandlingTrait.php b/Account/PermissionHandlingTrait.php index 2485e6554..9803822da 100644 --- a/Account/PermissionHandlingTrait.php +++ b/Account/PermissionHandlingTrait.php @@ -32,6 +32,14 @@ trait PermissionHandlingTrait */ protected array $permissions = []; + /** + * Amount of permissions. + * + * @var int + * @since 1.0.0 + */ + private int $pLength = 0; + /** * Set permissions. * @@ -46,6 +54,7 @@ trait PermissionHandlingTrait public function setPermissions(array $permissions) : void { $this->permissions = $permissions; + $this->pLength = \count($this->permissions); } /** @@ -68,6 +77,8 @@ trait PermissionHandlingTrait $this->permissions[] = $permission; } } + + $this->pLength = \count($this->permissions); } /** @@ -84,6 +95,7 @@ trait PermissionHandlingTrait public function addPermission(PermissionAbstract $permission) : void { $this->permissions[] = $permission; + ++$this->permission; } /** @@ -124,17 +136,10 @@ trait PermissionHandlingTrait int $element = null, int $component = null ) : bool { - $app = $app !== null ? \strtolower($app) : $app; + $app = $app !== null ? \strtolower($app) : $app; // @todo: maybe don't do this because this function get's called so often. - foreach ($this->permissions as $p) { - if (($unit === null || $p->getUnit() === $unit || $p->getUnit() === null) - && ($app === null || $p->getApp() === $app || $p->getApp() === null) - && ($module === null || $p->getModule() === $module || $p->getModule() === null) - && ($type === null || $p->getType() === $type || $p->getType() === null) - && ($element === null || $p->getElement() === $element || $p->getElement() === null) - && ($component === null || $p->getComponent() === $component || $p->getComponent() === null) - && ($p->getPermission() | $permission) === $p->getPermission() - ) { + for ($i = 0; $i < $this->pLength; ++$i) { + if ($this->permissions[$i]->hasPermission($permission, $unit, $app, $module, $type, $element, $component)) { return true; } } diff --git a/Uri/UriFactory.php b/Uri/UriFactory.php index 4293c53cf..b287929e1 100644 --- a/Uri/UriFactory.php +++ b/Uri/UriFactory.php @@ -195,25 +195,21 @@ final class UriFactory { $parts = \explode('&', \str_replace('?', '&', $url)); - if (\count($parts) >= 2) { + if (\count($parts) > 1) { $pars = \array_slice($parts, 1); - $comps = []; $length = \count($pars); + $url = $parts[0]; + $first = true; for ($i = 0; $i < $length; ++$i) { $spl = \explode('=', $pars[$i]); if (isset($spl[1])) { - $comps[$spl[0]] = $spl[1]; + $url .= $first ? '?' : '&'; + $url .= $spl[0] . '=' . $spl[1]; + $first = false; } } - - $pars = []; - foreach ($comps as $key => $value) { - $pars[] = $key . '=' . $value; - } - - $url = $parts[0] . (empty($pars) ? '' : '?' . \implode('&', $pars)); } return $url;