Skip to content

Commit

Permalink
Refactorin input handler chain building process
Browse files Browse the repository at this point in the history
  • Loading branch information
olivervogel committed Sep 30, 2023
1 parent cdee873 commit 78b968a
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 47 deletions.
35 changes: 34 additions & 1 deletion src/Drivers/Abstract/AbstractInputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
35 changes: 12 additions & 23 deletions src/Drivers/Gd/InputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
];
}
35 changes: 12 additions & 23 deletions src/Drivers/Imagick/InputHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
}

0 comments on commit 78b968a

Please sign in to comment.