PocketMine-MP 5.39.3 git-407163d9b7f8a53b180867cc017daf92cf563f44
Loading...
Searching...
No Matches
RegistryTrait.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\utils;
25
26use function array_map;
27use function count;
28use function mb_strtoupper;
29use function preg_match;
30
45 private static $members = null;
46
47 private static function verifyName(string $name) : void{
48 if(preg_match('/^(?!\d)[A-Za-z\d_]+$/u', $name) === 0){
49 throw new \InvalidArgumentException("Invalid member name \"$name\", should only contain letters, numbers and underscores, and must not start with a number");
50 }
51 }
52
58 private static function _registryRegister(string $name, object $member) : void{
59 if(self::$members === null){
60 throw new AssumptionFailedError("Cannot register members outside of " . self::class . "::setup()");
61 }
62 self::verifyName($name);
63 $upperName = mb_strtoupper($name);
64 if(isset(self::$members[$upperName])){
65 throw new \InvalidArgumentException("\"$upperName\" is already reserved");
66 }
67 self::$members[$upperName] = $member;
68 }
69
75 abstract protected static function setup() : void;
76
82 protected static function checkInit() : void{
83 if(self::$members === null){
84 self::$members = [];
86 }
87 }
88
92 private static function _registryFromString(string $name) : object{
93 self::checkInit();
94 if(self::$members === null){
95 throw new AssumptionFailedError(self::class . "::checkInit() did not initialize self::\$members correctly");
96 }
97 $upperName = mb_strtoupper($name);
98 if(!isset(self::$members[$upperName])){
99 throw new \InvalidArgumentException("No such registry member: " . self::class . "::" . $upperName);
100 }
101 return self::preprocessMember(self::$members[$upperName]);
102 }
103
104 protected static function preprocessMember(object $member) : object{
105 return $member;
106 }
107
112 public static function __callStatic(string $name, array $arguments) : object{
113 if(count($arguments) > 0){
114 throw new \ArgumentCountError("Expected exactly 0 arguments, " . count($arguments) . " passed");
115 }
116
117 //fast path
118 if(self::$members !== null && isset(self::$members[$name])){
119 return self::preprocessMember(self::$members[$name]);
120 }
121
122 //fallback
123 try{
124 return self::_registryFromString($name);
125 }catch(\InvalidArgumentException $e){
126 throw new \Error($e->getMessage(), 0, $e);
127 }
128 }
129
134 private static function _registryGetAll() : array{
135 self::checkInit();
136 return array_map(self::preprocessMember(...), self::$members ?? throw new AssumptionFailedError(self::class . "::checkInit() did not initialize self::\$members correctly"));
137 }
138}
static __callStatic(string $name, array $arguments)