1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-30 01:00:06 +02:00

Refactorin input handler chain building process

This commit is contained in:
Oliver Vogel
2023-09-30 10:19:01 +02:00
parent cdee873d8a
commit 78b968a28c
3 changed files with 58 additions and 47 deletions

View File

@@ -3,13 +3,46 @@
namespace Intervention\Image\Drivers\Abstract;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
use Intervention\Image\Exceptions\DecoderException;
use Intervention\Image\Interfaces\ColorInterface;
use Intervention\Image\Interfaces\ImageInterface;
abstract class AbstractInputHandler
{
abstract protected function chain(): AbstractDecoder;
/**
* Array of decoders which will be stacked into to the input handler chain
*/
protected $decoders = [];
/**
* Stack the decoder array into a nested decoder object
*
* @return AbstractDecoder
*/
protected function chain(): AbstractDecoder
{
if (count($this->decoders) == 0) {
throw new DecoderException('No decoders found in ' . get_class($this));
}
// get instance of last decoder in stack
list($classname) = array_slice(array_reverse($this->decoders), 0, 1);
$chain = new $classname();
// build decoder chain
foreach (array_slice(array_reverse($this->decoders), 1) as $classname) {
$chain = new $classname($chain);
}
return $chain;
}
/**
* Try to decode the given input with each decoder of the the handler chain
*
* @param mixed $var
* @return ImageInterface|ColorInterface
*/
public function handle($input): ImageInterface|ColorInterface
{
return $this->chain()->handle($input);

View File

@@ -3,30 +3,19 @@
namespace Intervention\Image\Drivers\Gd;
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
class InputHandler extends AbstractInputHandler
{
protected function chain(): AbstractDecoder
{
return new Decoders\ImageObjectDecoder(
new Decoders\ArrayColorDecoder(
new Decoders\HtmlColorNameDecoder(
new Decoders\RgbStringColorDecoder(
new Decoders\HexColorDecoder(
new Decoders\TransparentColorDecoder(
new Decoders\FilePathImageDecoder(
new Decoders\BinaryImageDecoder(
new Decoders\DataUriImageDecoder(
new Decoders\Base64ImageDecoder()
)
)
)
)
)
)
)
)
);
}
protected $decoders = [
Decoders\ImageObjectDecoder::class,
Decoders\ArrayColorDecoder::class,
Decoders\HtmlColorNameDecoder::class,
Decoders\RgbStringColorDecoder::class,
Decoders\HexColorDecoder::class,
Decoders\TransparentColorDecoder::class,
Decoders\FilePathImageDecoder::class,
Decoders\BinaryImageDecoder::class,
Decoders\DataUriImageDecoder::class,
Decoders\Base64ImageDecoder::class,
];
}

View File

@@ -3,30 +3,19 @@
namespace Intervention\Image\Drivers\Imagick;
use Intervention\Image\Drivers\Abstract\AbstractInputHandler;
use Intervention\Image\Drivers\Abstract\Decoders\AbstractDecoder;
class InputHandler extends AbstractInputHandler
{
protected function chain(): AbstractDecoder
{
return new Decoders\ImageObjectDecoder(
new Decoders\ArrayColorDecoder(
new Decoders\HexColorDecoder(
new Decoders\HtmlColorNameDecoder(
new Decoders\RgbStringColorDecoder(
new Decoders\TransparentColorDecoder(
new Decoders\FilePathImageDecoder(
new Decoders\BinaryImageDecoder(
new Decoders\DataUriImageDecoder(
new Decoders\Base64ImageDecoder()
)
)
)
)
)
)
)
)
);
}
protected $decoders = [
Decoders\ImageObjectDecoder::class,
Decoders\ArrayColorDecoder::class,
Decoders\HexColorDecoder::class,
Decoders\HtmlColorNameDecoder::class,
Decoders\RgbStringColorDecoder::class,
Decoders\TransparentColorDecoder::class,
Decoders\FilePathImageDecoder::class,
Decoders\BinaryImageDecoder::class,
Decoders\DataUriImageDecoder::class,
Decoders\Base64ImageDecoder::class
];
}