1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Smart image resizing (and manipulation) by url module for Zend Framework 3 |
4
|
|
|
* |
5
|
|
|
* @link http://github.com/tck/zf2-imageresizer for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2017 Tobias Knab |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace TckImageResizer\Service; |
13
|
|
|
|
14
|
|
|
use TckImageResizer\Exception\BadMethodCallException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* self defined command registry |
18
|
|
|
* |
19
|
|
|
* @package TckImageResizer |
20
|
|
|
*/ |
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() |
39
|
|
|
{ |
40
|
18 |
|
} |
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() |
62
|
|
|
{ |
63
|
30 |
|
self::$instance = null; |
64
|
30 |
|
} |
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) |
103
|
|
|
{ |
104
|
12 |
|
if (!is_string($command) || strlen($command) < 1) { |
105
|
3 |
|
throw new BadMethodCallException('Parameter command is not a valid string'); |
106
|
|
|
} |
107
|
9 |
|
$instance = self::getInstance(); |
108
|
|
|
|
109
|
9 |
|
if (!isset($instance->commands[$command])) { |
110
|
3 |
|
throw new BadMethodCallException('Command "' . $command . '" is not a registered command'); |
111
|
|
|
} |
112
|
|
|
|
113
|
6 |
|
return $instance->commands[$command]; |
114
|
|
|
} |
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) |
124
|
|
|
{ |
125
|
21 |
|
if (!is_string($command) || strlen($command) < 1) { |
126
|
3 |
|
throw new BadMethodCallException('Parameter command is not a valid string'); |
127
|
|
|
} |
128
|
18 |
|
if (!is_callable($callback)) { |
129
|
3 |
|
throw new BadMethodCallException('Parameter callback is not a valid callback'); |
130
|
|
|
} |
131
|
|
|
|
132
|
15 |
|
$instance = self::getInstance(); |
133
|
|
|
|
134
|
15 |
|
$instance->commands[$command] = $callback; |
135
|
|
|
|
136
|
15 |
|
return $instance; |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|