22declare(strict_types=1);
27use
function spl_object_id;
30 private static ?
self $instance =
null;
33 if(self::$instance ===
null){
34 self::$instance =
new self();
37 return self::$instance;
41 protected array $permissions = [];
43 protected array $permSubs = [];
45 public function getPermission(
string $name) : ?
Permission{
46 return $this->permissions[$name] ??
null;
49 public function addPermission(
Permission $permission) :
bool{
50 if(!isset($this->permissions[$permission->getName()])){
51 $this->permissions[$permission->getName()] = $permission;
59 public function removePermission(
Permission|
string $permission) :
void{
61 unset($this->permissions[$permission->getName()]);
63 unset($this->permissions[$permission]);
67 public function subscribeToPermission(
string $permission,
PermissibleInternal $permissible) :
void{
68 if(!isset($this->permSubs[$permission])){
69 $this->permSubs[$permission] = [];
71 $this->permSubs[$permission][spl_object_id($permissible)] = $permissible;
74 public function unsubscribeFromPermission(
string $permission,
PermissibleInternal $permissible) :
void{
75 if(isset($this->permSubs[$permission][spl_object_id($permissible)])){
76 if(count($this->permSubs[$permission]) === 1){
77 unset($this->permSubs[$permission]);
79 unset($this->permSubs[$permission][spl_object_id($permissible)]);
85 foreach($this->permSubs as $permission => $subs){
86 if(count($subs) === 1 && isset($subs[spl_object_id($permissible)])){
87 unset($this->permSubs[$permission]);
89 unset($this->permSubs[$permission][spl_object_id($permissible)]);
98 return $this->permSubs[$permission] ?? [];
105 return $this->permissions;
108 public function clearPermissions() : void{
109 $this->permissions = [];
getPermissionSubscriptions(string $permission)