try to improve performance

This commit is contained in:
Dennis Eichhorn 2019-09-07 08:45:24 +02:00
parent 27035d8be7
commit c41a59ad1d
3 changed files with 57 additions and 21 deletions

View File

@ -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}
*/

View File

@ -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;
}
}

View File

@ -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;