PocketMine-MP 5.15.1 git-5ef247620a7c6301a849b54e5ef1009217729fc8
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
28use function spl_object_id;
29
32 private array $permissions = [];
33
38 private array $subscribers = [];
39
43 public function __construct(
44 private Plugin $plugin
45 ){
46 if(!$plugin->isEnabled()){
47 throw new PluginException("Plugin " . $plugin->getDescription()->getName() . " is disabled");
48 }
49 }
50
51 public function getPlugin() : Plugin{
52 return $this->plugin;
53 }
54
59 public function getSubscribers() : array{ return $this->subscribers; }
60
64 public function getPermissions() : array{
65 return $this->permissions;
66 }
67
68 private function recalculatePermissibles() : void{
69 foreach($this->subscribers as $permissible){
70 $permissible->recalculatePermissions();
71 }
72 }
73
74 public function clearPermissions() : void{
75 $this->permissions = [];
76 $this->recalculatePermissibles();
77 }
78
82 public function setPermissions(array $permissions) : void{
83 foreach($permissions as $key => $value){
84 $this->permissions[$key] = $value;
85 }
86 $this->recalculatePermissibles();
87 }
88
92 public function unsetPermissions(array $permissions) : void{
93 foreach($permissions as $node){
94 unset($this->permissions[$node]);
95 }
96 $this->recalculatePermissibles();
97 }
98
99 public function setPermission(Permission|string $name, bool $value) : void{
100 $name = $name instanceof Permission ? $name->getName() : $name;
101 if(isset($this->permissions[$name])){
102 if($this->permissions[$name] === $value){
103 return;
104 }
105 /* Because of the way child permissions are calculated, permissions which were set later in time are
106 * preferred over earlier ones when conflicts in inherited permission values occur.
107 * Here's the kicker: This behaviour depends on PHP's internal array ordering, which maintains insertion
108 * order -- BUT -- assigning to an existing index replaces the old value WITHOUT changing the order.
109 * (what crazy person thought relying on this this was a good idea?!?!?!?!?!)
110 *
111 * This removes the old value so that the new value will be added at the end of the array's internal order
112 * instead of directly taking the place of the older value.
113 */
114 unset($this->permissions[$name]);
115 }
116 $this->permissions[$name] = $value;
117 $this->recalculatePermissibles();
118 }
119
120 public function unsetPermission(Permission|string $name) : void{
121 $name = $name instanceof Permission ? $name->getName() : $name;
122 if(isset($this->permissions[$name])){
123 unset($this->permissions[$name]);
124 $this->recalculatePermissibles();
125 }
126 }
127
131 public function subscribePermissible(PermissibleInternal $permissible) : void{
132 $this->subscribers[spl_object_id($permissible)] = $permissible;
133 }
134
138 public function unsubscribePermissible(PermissibleInternal $permissible) : void{
139 unset($this->subscribers[spl_object_id($permissible)]);
140 }
141}