|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace AmaTeam\Image\Projection\Type; |
|
4
|
|
|
|
|
5
|
|
|
use AmaTeam\Image\Projection\Image\Adapter\Discovery; |
|
6
|
|
|
use AmaTeam\Image\Projection\API\Image\ImageFactoryInterface; |
|
7
|
|
|
use AmaTeam\Image\Projection\Filesystem\Factory; |
|
8
|
|
|
use AmaTeam\Image\Projection\Image\Manager; |
|
9
|
|
|
use AmaTeam\Image\Projection\API\Type\HandlerInterface; |
|
10
|
|
|
use AmaTeam\Image\Projection\Type\CubeMap\Handler as CubeMapHandler; |
|
11
|
|
|
use AmaTeam\Image\Projection\Type\Equirectangular\Handler as EquirectHandler; |
|
12
|
|
|
use League\Flysystem\FilesystemInterface; |
|
13
|
|
|
use Psr\Log\LoggerInterface; |
|
14
|
|
|
use Psr\Log\NullLogger; |
|
15
|
|
|
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException; |
|
16
|
|
|
|
|
17
|
|
|
class Registry |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var FilesystemInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
private $filesystem; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @var Manager |
|
26
|
|
|
*/ |
|
27
|
|
|
private $imageManager; |
|
28
|
|
|
/** |
|
29
|
|
|
* @var HandlerInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private $registry = []; |
|
32
|
|
|
/** |
|
33
|
|
|
* @var LoggerInterface |
|
34
|
|
|
*/ |
|
35
|
|
|
private $logger; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param FilesystemInterface $filesystem |
|
39
|
|
|
* @param ImageFactoryInterface $imageFactory |
|
40
|
|
|
* @param LoggerInterface|null $logger |
|
41
|
|
|
*/ |
|
42
|
|
|
public function __construct( |
|
43
|
|
|
FilesystemInterface $filesystem = null, |
|
44
|
|
|
ImageFactoryInterface $imageFactory = null, |
|
45
|
|
|
LoggerInterface $logger = null |
|
46
|
|
|
) { |
|
47
|
|
|
$this->filesystem = $filesystem ?: Factory::create(); |
|
48
|
|
|
$imageFactory = $imageFactory ?: Discovery::find(); |
|
49
|
|
|
$this->imageManager = new Manager($this->filesystem, $imageFactory); |
|
50
|
|
|
$this->logger = $logger ?: new NullLogger(); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @param string $type |
|
55
|
|
|
* @return HandlerInterface |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getHandler($type) |
|
58
|
|
|
{ |
|
59
|
|
|
$handler = $this->findHandler($type); |
|
60
|
|
|
if (!$handler) { |
|
61
|
|
|
throw new BadMethodCallException("Unknown projection type $type"); |
|
62
|
|
|
} |
|
63
|
|
|
return $handler; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* @param string $type |
|
68
|
|
|
* @return HandlerInterface|null |
|
69
|
|
|
*/ |
|
70
|
|
|
public function findHandler($type) |
|
71
|
|
|
{ |
|
72
|
|
|
if (!is_string($type)) { |
|
73
|
|
|
throw new BadMethodCallException('Non-string type provided'); |
|
74
|
|
|
} |
|
75
|
|
|
if (isset($this->registry[$type])) { |
|
76
|
|
|
return $this->registry[$type]; |
|
77
|
|
|
} |
|
78
|
|
|
$type = $this->findType($type); |
|
79
|
|
|
return $type ? $this->registry[$type] : null; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Checks if handler exists. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $type |
|
86
|
|
|
* @return bool |
|
87
|
|
|
*/ |
|
88
|
|
|
public function exists($type) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->findHandler($type) !== null; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Finds and returns correct type name by passed string or just null. |
|
95
|
|
|
* |
|
96
|
|
|
* @param string|$type |
|
97
|
|
|
* @return string|null |
|
98
|
|
|
*/ |
|
99
|
|
|
public function findType($type) |
|
100
|
|
|
{ |
|
101
|
|
|
if (isset($this->registry[$type])) { |
|
102
|
|
|
return $type; |
|
103
|
|
|
} |
|
104
|
|
|
$type = self::normalizeType($type); |
|
105
|
|
|
foreach (array_keys($this->registry) as $key) { |
|
106
|
|
|
$normalized = self::normalizeType($key); |
|
107
|
|
|
if (strpos($normalized, $type) === 0) { |
|
108
|
|
|
return $key; |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
return null; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @param string $name |
|
116
|
|
|
* @param HandlerInterface $handler |
|
117
|
|
|
* @return HandlerInterface |
|
118
|
|
|
*/ |
|
119
|
|
|
public function register($name, HandlerInterface $handler) |
|
120
|
|
|
{ |
|
121
|
|
|
$this->registry[$name] = $handler; |
|
122
|
|
|
return $handler; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* @return string[] |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getRegisteredTypes() |
|
129
|
|
|
{ |
|
130
|
|
|
return array_keys($this->registry); |
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* Registers bundled in types. |
|
135
|
|
|
*/ |
|
136
|
|
|
public function registerDefaultTypes() |
|
137
|
|
|
{ |
|
138
|
|
|
$this->register( |
|
139
|
|
|
EquirectHandler::TYPE, |
|
140
|
|
|
new EquirectHandler($this->filesystem, $this->imageManager) |
|
141
|
|
|
); |
|
142
|
|
|
$this->register( |
|
143
|
|
|
CubeMapHandler::TYPE, |
|
144
|
|
|
new CubeMapHandler($this->filesystem, $this->imageManager) |
|
145
|
|
|
); |
|
146
|
|
|
return $this; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return FilesystemInterface |
|
151
|
|
|
*/ |
|
152
|
|
|
public function getFilesystem() |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->filesystem; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return Manager |
|
159
|
|
|
*/ |
|
160
|
|
|
public function getImageManager() |
|
161
|
|
|
{ |
|
162
|
|
|
return $this->imageManager; |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
/** |
|
166
|
|
|
* Normalizes type name, removing non-word characters and casting |
|
167
|
|
|
* to lower case. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $type |
|
170
|
|
|
* @return string |
|
171
|
|
|
*/ |
|
172
|
|
|
public static function normalizeType($type) |
|
173
|
|
|
{ |
|
174
|
|
|
return preg_replace('/\W/', '', strtolower($type)); |
|
175
|
|
|
} |
|
176
|
|
|
} |
|
177
|
|
|
|