1 | <?php |
||
21 | class CommandRegistry |
||
22 | { |
||
23 | /** |
||
24 | * singleton |
||
25 | */ |
||
26 | private static $instance = null; |
||
27 | |||
28 | /** |
||
29 | * the actual commands |
||
30 | * |
||
31 | * @var array |
||
32 | */ |
||
33 | protected $commands = []; |
||
34 | |||
35 | /** |
||
36 | * private constructor |
||
37 | */ |
||
38 | 18 | private function __construct() |
|
41 | |||
42 | /** |
||
43 | * get singleton instance |
||
44 | * |
||
45 | * @return self |
||
46 | */ |
||
47 | 24 | public static function getInstance() |
|
48 | { |
||
49 | 24 | if (!self::$instance instanceof self) { |
|
50 | 18 | self::$instance = new self; |
|
51 | 18 | } |
|
52 | |||
53 | 24 | return self::$instance; |
|
54 | } |
||
55 | |||
56 | /** |
||
57 | * clear the singleton |
||
58 | * |
||
59 | * @return void |
||
60 | */ |
||
61 | 30 | public static function destroy() |
|
65 | |||
66 | /** |
||
67 | * get command registry list |
||
68 | * |
||
69 | * @return array |
||
70 | */ |
||
71 | 3 | public static function getCommands() |
|
72 | { |
||
73 | 3 | $instance = self::getInstance(); |
|
74 | |||
75 | 3 | return $instance->commands; |
|
76 | } |
||
77 | |||
78 | /** |
||
79 | * command registry has command |
||
80 | * |
||
81 | * @param string $command |
||
82 | * |
||
83 | * @return boolean |
||
84 | */ |
||
85 | 12 | public static function hasCommand($command) |
|
86 | { |
||
87 | 12 | if (!is_string($command) || strlen($command) < 1) { |
|
88 | 3 | throw new BadMethodCallException('Parameter command is not a valid string'); |
|
89 | } |
||
90 | 9 | $instance = self::getInstance(); |
|
91 | |||
92 | 9 | return isset($instance->commands[$command]); |
|
93 | } |
||
94 | |||
95 | /** |
||
96 | * get command callback |
||
97 | * |
||
98 | * @param string $command |
||
99 | * |
||
100 | * @return array |
||
101 | */ |
||
102 | 12 | public static function getCommand($command) |
|
115 | |||
116 | /** |
||
117 | * register a image command |
||
118 | * |
||
119 | * @param string $command |
||
120 | * @param callable $callback |
||
121 | * @return self |
||
122 | */ |
||
123 | 21 | public static function register($command, $callback) |
|
138 | } |
||
139 |