PocketMine-MP 5.21.2 git-a6534ecbbbcf369264567d27e5ed70f7f5be9816
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
38trait RegistryTrait{
43 private static $members = null;
44
45 private static function verifyName(string $name) : void{
46 if(preg_match('/^(?!\d)[A-Za-z\d_]+$/u', $name) === 0){
47 throw new \InvalidArgumentException("Invalid member name \"$name\", should only contain letters, numbers and underscores, and must not start with a number");
48 }
49 }
50
56 private static function _registryRegister(string $name, object $member) : void{
57 if(self::$members === null){
58 throw new AssumptionFailedError("Cannot register members outside of " . self::class . "::setup()");
59 }
60 self::verifyName($name);
61 $upperName = mb_strtoupper($name);
62 if(isset(self::$members[$upperName])){
63 throw new \InvalidArgumentException("\"$upperName\" is already reserved");
64 }
65 self::$members[$upperName] = $member;
66 }
67
73 abstract protected static function setup() : void;
74
80 protected static function checkInit() : void{
81 if(self::$members === null){
82 self::$members = [];
84 }
85 }
86
90 private static function _registryFromString(string $name) : object{
91 self::checkInit();
92 if(self::$members === null){
93 throw new AssumptionFailedError(self::class . "::checkInit() did not initialize self::\$members correctly");
94 }
95 $upperName = mb_strtoupper($name);
96 if(!isset(self::$members[$upperName])){
97 throw new \InvalidArgumentException("No such registry member: " . self::class . "::" . $upperName);
98 }
99 return self::preprocessMember(self::$members[$upperName]);
100 }
101
102 protected static function preprocessMember(object $member) : object{
103 return $member;
104 }
105
110 public static function __callStatic(string $name, array $arguments) : object{
111 if(count($arguments) > 0){
112 throw new \ArgumentCountError("Expected exactly 0 arguments, " . count($arguments) . " passed");
113 }
114
115 //fast path
116 if(self::$members !== null && isset(self::$members[$name])){
117 return self::preprocessMember(self::$members[$name]);
118 }
119
120 //fallback
121 try{
122 return self::_registryFromString($name);
123 }catch(\InvalidArgumentException $e){
124 throw new \Error($e->getMessage(), 0, $e);
125 }
126 }
127
132 private static function _registryGetAll() : array{
133 self::checkInit();
134 return array_map(self::preprocessMember(...), self::$members ?? throw new AssumptionFailedError(self::class . "::checkInit() did not initialize self::\$members correctly"));
135 }
136}
static __callStatic(string $name, array $arguments)