Improve Acl\Actions

This commit is contained in:
Matias Griese
2020-01-28 12:34:10 +02:00
parent 2136dc34fe
commit 7913edd34b
3 changed files with 85 additions and 1 deletions

View File

@@ -89,6 +89,28 @@ class Action implements \IteratorAggregate, \Countable
$this->parent = $parent;
}
public function getScope(): string
{
if (($pos = strpos($this->name, '.')) > 0) {
return substr($this->name, 0, $pos);
}
return $this->name;
}
public function getLevels(): int
{
return substr_count($this->name, '.');
}
/**
* @return bool
*/
public function hasChildren(): bool
{
return !empty($this->children);
}
/**
* @return Action[]
*/

View File

@@ -23,7 +23,10 @@ class Permissions implements \ArrayAccess, \Countable, \IteratorAggregate
*/
public function getInstances(): array
{
return $this->instances;
$iterator = new RecursiveActionIterator($this->actions);
$recursive = new \RecursiveIteratorIterator($iterator, \RecursiveIteratorIterator::SELF_FIRST);
return iterator_to_array($recursive);
}
/**

View File

@@ -0,0 +1,59 @@
<?php
/**
* @package Grav\Framework\Acl
*
* @copyright Copyright (C) 2015 - 2020 Trilby Media, LLC. All rights reserved.
* @license MIT License; see LICENSE file for details.
*/
namespace Grav\Framework\Acl;
use RocketTheme\Toolbox\ArrayTraits\Constructor;
use RocketTheme\Toolbox\ArrayTraits\Countable;
use RocketTheme\Toolbox\ArrayTraits\Iterator;
/**
* Class Action
* @package Grav\Framework\Acl
*/
class RecursiveActionIterator implements \RecursiveIterator, \Countable
{
use Constructor, Iterator, Countable;
/**
* @see \Iterator::key()
* @return string
*/
public function key()
{
/** @var Action $current */
$current = $this->current();
return $current->name;
}
/**
* @see \RecursiveIterator::hasChildren()
* @return bool
*/
public function hasChildren(): bool
{
/** @var Action $current */
$current = $this->current();
return $current->hasChildren();
}
/**
* @see \RecursiveIterator::getChildren()
* @return RecursiveActionIterator
*/
public function getChildren(): self
{
/** @var Action $current */
$current = $this->current();
return new static($current->getChildren());
}
}