-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathAbstractDriver.php
More file actions
199 lines (172 loc) Β· 6.08 KB
/
AbstractDriver.php
File metadata and controls
199 lines (172 loc) Β· 6.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php
declare(strict_types=1);
namespace Intervention\Image\Drivers;
use Intervention\Image\Config;
use Intervention\Image\Exceptions\ColorDecoderException;
use Intervention\Image\Exceptions\DriverException;
use Intervention\Image\Exceptions\ImageDecoderException;
use Intervention\Image\Exceptions\InvalidArgumentException;
use Intervention\Image\Exceptions\MissingDependencyException;
use Intervention\Image\Exceptions\NotSupportedException;
use Intervention\Image\InputHandler;
use Intervention\Image\Interfaces\AnalyzerInterface;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\DecoderInterface;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\EncoderInterface;
use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Interfaces\ModifierInterface;
use Intervention\Image\Interfaces\SpecializableInterface;
use Intervention\Image\Interfaces\SpecializedInterface;
abstract class AbstractDriver implements DriverInterface
{
/**
* @throws MissingDependencyException
*/
public function __construct(protected Config $config = new Config())
{
$this->checkHealth();
}
/**
* {@inheritdoc}
*
* @see DriverInterface::config()
*/
public function config(): Config
{
return $this->config;
}
/**
* {@inheritdoc}
*
* @see DriverInterface::decodeImage()
*
* @throws InvalidArgumentException
* @throws ImageDecoderException
* @throws DriverException
*/
public function decodeImage(mixed $input, ?array $decoders = null): ImageInterface
{
$decoders = $decoders === null ? InputHandler::IMAGE_DECODERS : $decoders;
if (count($decoders) === 0) {
throw new InvalidArgumentException('No decoders in array');
}
try {
$result = InputHandler::usingDecoders($decoders, $this)->handle($input);
} catch (NotSupportedException) {
$type = is_object($input) ? $input::class : gettype($input);
throw new InvalidArgumentException('Unsupported image source type "' . $type . '"');
}
if (!$result instanceof ImageInterface) {
throw new ImageDecoderException('Result must be instance of ' . ImageInterface::class);
}
return $result;
}
/**
* {@inheritdoc}
*
* @see DriverInterface::decodeColor()
*
* @throws InvalidArgumentException
* @throws ColorDecoderException
* @throws DriverException
*/
public function decodeColor(mixed $input, ?array $decoders = null): ColorInterface
{
$decoders = $decoders === null ? InputHandler::COLOR_DECODERS : $decoders;
if (count($decoders) === 0) {
throw new InvalidArgumentException('No decoders in array');
}
try {
$result = InputHandler::usingDecoders($decoders, $this)->handle($input);
} catch (NotSupportedException) {
throw new ColorDecoderException('Unknown color format');
}
if (!$result instanceof ColorInterface) {
throw new ColorDecoderException('Result must be instance of ' . ColorInterface::class);
}
return $result;
}
/**
* {@inheritdoc}
*
* @see DriverInterface::specializeModifier()
*
* @throws NotSupportedException
*/
public function specializeModifier(ModifierInterface $modifier): ModifierInterface
{
return $this->specialize($modifier);
}
/**
* {@inheritdoc}
*
* @see DriverInterface::specializeAnalyzer()
*
* @throws NotSupportedException
*/
public function specializeAnalyzer(AnalyzerInterface $analyzer): AnalyzerInterface
{
return $this->specialize($analyzer);
}
/**
* {@inheritdoc}
*
* @see DriverInterface::specializeEncoder()
*
* @throws NotSupportedException
*/
public function specializeEncoder(EncoderInterface $encoder): EncoderInterface
{
return $this->specialize($encoder);
}
/**
* {@inheritdoc}
*
* @see DriverInterface::specializeDecoder()
*
* @throws NotSupportedException
*/
public function specializeDecoder(DecoderInterface $decoder): DecoderInterface
{
return $this->specialize($decoder);
}
/**
* @throws NotSupportedException
*/
private function specialize(
ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface $object
): ModifierInterface|AnalyzerInterface|EncoderInterface|DecoderInterface {
// return object directly if no specializing is possible
if (!$object instanceof SpecializableInterface) {
return $object;
}
// return directly and only attach driver if object is already specialized
if ($object instanceof SpecializedInterface) {
$object->setDriver($this);
return $object;
}
// resolve classname for specializable object
$objectShortname = substr($object::class, (int) strrpos($object::class, '\\') + 1);
$specializedClassname = implode("\\", [
substr($this::class, 0, (int) strrpos($this::class, '\\')), // driver's namespace
match (true) {
$object instanceof ModifierInterface => 'Modifiers',
$object instanceof AnalyzerInterface => 'Analyzers',
$object instanceof EncoderInterface => 'Encoders',
$object instanceof DecoderInterface => 'Decoders',
},
$objectShortname,
]);
// fail if driver specialized classname does not exists
if (!class_exists($specializedClassname)) {
throw new NotSupportedException(
"Class '" . $objectShortname . "' is not supported by " . $this->id() . " driver"
);
}
// create a driver specialized object with the specializable properties of generic object
$specialized = new $specializedClassname(...$object->specializationArguments());
// attach driver
return $specialized->setDriver($this);
}
}