PocketMine-MP 5.19.1 git-5cc1068cd43264d3363295eb8d6901e02f467897
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
26use function count;
27use function spl_object_id;
28
30 private static ?self $instance = null;
31
32 public static function getInstance() : PermissionManager{
33 if(self::$instance === null){
34 self::$instance = new self();
35 }
36
37 return self::$instance;
38 }
39
41 protected array $permissions = [];
43 protected array $permSubs = [];
44
45 public function getPermission(string $name) : ?Permission{
46 return $this->permissions[$name] ?? null;
47 }
48
49 public function addPermission(Permission $permission) : bool{
50 if(!isset($this->permissions[$permission->getName()])){
51 $this->permissions[$permission->getName()] = $permission;
52
53 return true;
54 }
55
56 return false;
57 }
58
59 public function removePermission(Permission|string $permission) : void{
60 if($permission instanceof Permission){
61 unset($this->permissions[$permission->getName()]);
62 }else{
63 unset($this->permissions[$permission]);
64 }
65 }
66
67 public function subscribeToPermission(string $permission, PermissibleInternal $permissible) : void{
68 if(!isset($this->permSubs[$permission])){
69 $this->permSubs[$permission] = [];
70 }
71 $this->permSubs[$permission][spl_object_id($permissible)] = $permissible;
72 }
73
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]);
78 }else{
79 unset($this->permSubs[$permission][spl_object_id($permissible)]);
80 }
81 }
82 }
83
84 public function unsubscribeFromAllPermissions(PermissibleInternal $permissible) : void{
85 foreach($this->permSubs as $permission => $subs){
86 if(count($subs) === 1 && isset($subs[spl_object_id($permissible)])){
87 unset($this->permSubs[$permission]);
88 }else{
89 unset($this->permSubs[$permission][spl_object_id($permissible)]);
90 }
91 }
92 }
93
97 public function getPermissionSubscriptions(string $permission) : array{
98 return $this->permSubs[$permission] ?? [];
99 }
100
104 public function getPermissions() : array{
105 return $this->permissions;
106 }
107
108 public function clearPermissions() : void{
109 $this->permissions = [];
110 }
111}