diff --git a/src/Picea/Extension.php b/src/Picea/Extension.php index 1ebba43..5d8b492 100644 --- a/src/Picea/Extension.php +++ b/src/Picea/Extension.php @@ -19,6 +19,7 @@ class Extension implements \Picea\Extension\Extension public function register(Context $context) : void { $context->pushFunction("taxus", [ $this, 'taxus' ]); + $context->pushFunction("taxus_has", [ $this, 'taxusHas' ]); } public function parse(\Picea\Compiler\Context &$context, ?string $arguments, string $token, array $options = []) : string @@ -31,4 +32,8 @@ class Extension implements \Picea\Extension\Extension return $this->taxus->granted($name, ...$arguments); } + public function taxusHas(string $name) : bool + { + return $this->taxus->hasPrivilege($name); + } } diff --git a/src/Taxus.php b/src/Taxus.php index 068f001..ba438f7 100644 --- a/src/Taxus.php +++ b/src/Taxus.php @@ -4,14 +4,26 @@ namespace Taxus; class Taxus { - protected PermissionGrantInterface $gate; + # @ArrayOf Privilege + protected array $list = []; - public function __construct(PermissionGrantInterface $gate) + # @ArrayOf PermissionGrantInterface + protected array $gates = []; + + public function __construct(PermissionGrantInterface ... $gate,) { - $this->gate = $gate; + $this->pushGate(... $gate); } - protected array $list; + public function pushGate(PermissionGrantInterface ... $gates) : void + { + $this->gates = array_merge($this->gates, $gates); + } + + public function hasPrivilege(string $name) : bool + { + return isset($this->list[$name]); + } public function add(...$privileges) : self { @@ -44,6 +56,17 @@ class Taxus $callback = $this->list[$name][1] ?? 'default'; - return $this->gate->$callback(...$arguments); + foreach($this->gates as $gate) { + if ( method_exists($gate, $callback) ) { + return $gate->$callback(...$arguments); + } + } + + throw new \Exception( + sprintf( + "Method '$callback' could not be found from given gates list: %s", + implode(', ', array_map(fn($e) => $e::class, $this->gates)) + ) + ); } }