PocketMine-MP 5.23.3 git-f7687af337d001ddbcc47b8e773f014a33faa662
Loading...
Searching...
No Matches
PermissionAttachment.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
29use function spl_object_id;
30
36 private array $permissions = [];
37
42 private array $subscribers = [];
43
47 public function __construct(
48 private Plugin $plugin
49 ){
50 if(!$plugin->isEnabled()){
51 throw new PluginException("Plugin " . $plugin->getDescription()->getName() . " is disabled");
52 }
53 }
54
55 public function getPlugin() : Plugin{
56 return $this->plugin;
57 }
58
63 public function getSubscribers() : array{ return $this->subscribers; }
64
69 public function getPermissions() : array{
70 return $this->permissions;
71 }
72
73 private function recalculatePermissibles() : void{
74 foreach($this->subscribers as $permissible){
75 $permissible->recalculatePermissions();
76 }
77 }
78
79 public function clearPermissions() : void{
80 $this->permissions = [];
81 $this->recalculatePermissibles();
82 }
83
88 public function setPermissions(array $permissions) : void{
89 foreach(Utils::stringifyKeys($permissions) as $key => $value){
90 $this->permissions[$key] = $value;
91 }
92 $this->recalculatePermissibles();
93 }
94
98 public function unsetPermissions(array $permissions) : void{
99 foreach($permissions as $node){
100 unset($this->permissions[$node]);
101 }
102 $this->recalculatePermissibles();
103 }
104
105 public function setPermission(Permission|string $name, bool $value) : void{
106 $name = $name instanceof Permission ? $name->getName() : $name;
107 if(isset($this->permissions[$name])){
108 if($this->permissions[$name] === $value){
109 return;
110 }
111 /* Because of the way child permissions are calculated, permissions which were set later in time are
112 * preferred over earlier ones when conflicts in inherited permission values occur.
113 * Here's the kicker: This behaviour depends on PHP's internal array ordering, which maintains insertion
114 * order -- BUT -- assigning to an existing index replaces the old value WITHOUT changing the order.
115 * (what crazy person thought relying on this this was a good idea?!?!?!?!?!)
116 *
117 * This removes the old value so that the new value will be added at the end of the array's internal order
118 * instead of directly taking the place of the older value.
119 */
120 unset($this->permissions[$name]);
121 }
122 $this->permissions[$name] = $value;
123 $this->recalculatePermissibles();
124 }
125
126 public function unsetPermission(Permission|string $name) : void{
127 $name = $name instanceof Permission ? $name->getName() : $name;
128 if(isset($this->permissions[$name])){
129 unset($this->permissions[$name]);
130 $this->recalculatePermissibles();
131 }
132 }
133
137 public function subscribePermissible(PermissibleInternal $permissible) : void{
138 $this->subscribers[spl_object_id($permissible)] = $permissible;
139 }
140
144 public function unsubscribePermissible(PermissibleInternal $permissible) : void{
145 unset($this->subscribers[spl_object_id($permissible)]);
146 }
147}