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:
@@ -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);
|
||||
|
@@ -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,
|
||||
];
|
||||
}
|
||||
|
@@ -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
|
||||
];
|
||||
}
|
||||
|
Reference in New Issue
Block a user