PocketMine-MP 5.23.3 git-fbaa125d0ce21ffef98fc1630881a92bedfbaa73
Loading...
Searching...
No Matches
PermissionManager.php
1<?php
2
3/*
4 *
5 * ____ _ _ __ __ _ __ __ ____
6 * | _ \ ___ ___| | _____| |_| \/ (_)_ __ ___ | \/ | _ \
7 * | |_) / _ \ / __| |/ / _ \ __| |\/| | | '_ \ / _ \_____| |\/| | |_) |
8 * | __/ (_) | (__| < __/ |_| | | | | | | | __/_____| | | | __/
9 * |_| \___/ \___|_|\_\___|\__|_| |_|_|_| |_|\___| |_| |_|_|
10 *
11 * This program is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License as published by
13 * the Free Software Foundation, either version 3 of the License, or
14 * (at your option) any later version.
15 *
16 * @author PocketMine Team
17 * @link http://www.pocketmine.net/
18 *
19 *
20 */
21
22declare(strict_types=1);
23
24namespace pocketmine\permission;
25
28use function count;
29use function spl_object_id;
30
32 private static ?self $instance = null;
33
34 public static function getInstance() : PermissionManager{
35 if(self::$instance === null){
36 self::$instance = new self();
37 }
38
39 return self::$instance;
40 }
41
46 protected array $permissions = [];
51 protected array $permSubs = [];
52
53 public function getPermission(string $name) : ?Permission{
54 return $this->permissions[$name] ?? null;
55 }
56
57 public function addPermission(Permission $permission) : bool{
58 if(!isset($this->permissions[$permission->getName()])){
59 $this->permissions[$permission->getName()] = $permission;
60
61 return true;
62 }
63
64 return false;
65 }
66
67 public function removePermission(Permission|string $permission) : void{
68 if($permission instanceof Permission){
69 unset($this->permissions[$permission->getName()]);
70 }else{
71 unset($this->permissions[$permission]);
72 }
73 }
74
79 public function subscribeToPermission(string $permission, PermissibleInternal $permissible) : void{
80 if(!isset($this->permSubs[$permission])){
81 $this->permSubs[$permission] = [];
82 }
83 $this->permSubs[$permission][spl_object_id($permissible)] = $permissible;
84 }
85
90 public function unsubscribeFromPermission(string $permission, PermissibleInternal $permissible) : void{
91 if(isset($this->permSubs[$permission][spl_object_id($permissible)])){
92 if(count($this->permSubs[$permission]) === 1){
93 unset($this->permSubs[$permission]);
94 }else{
95 unset($this->permSubs[$permission][spl_object_id($permissible)]);
96 }
97 }
98 }
99
104 public function unsubscribeFromAllPermissions(PermissibleInternal $permissible) : void{
105 foreach(Utils::promoteKeys($this->permSubs) as $permission => $subs){
106 if(count($subs) === 1 && isset($subs[spl_object_id($permissible)])){
107 unset($this->permSubs[$permission]);
108 }else{
109 unset($this->permSubs[$permission][spl_object_id($permissible)]);
110 }
111 }
112 }
113
119 public function getPermissionSubscriptions(string $permission) : array{
120 return $this->permSubs[$permission] ?? [];
121 }
122
126 public function getPermissions() : array{
127 return $this->permissions;
128 }
129
130 public function clearPermissions() : void{
131 $this->permissions = [];
132 }
133}
subscribeToPermission(string $permission, PermissibleInternal $permissible)
unsubscribeFromPermission(string $permission, PermissibleInternal $permissible)
unsubscribeFromAllPermissions(PermissibleInternal $permissible)