diff --git a/src/Binary/Loader/ChainLoader.php b/src/Binary/Loader/ChainLoader.php index ab5517335..e1b254594 100644 --- a/src/Binary/Loader/ChainLoader.php +++ b/src/Binary/Loader/ChainLoader.php @@ -11,17 +11,19 @@ namespace Liip\ImagineBundle\Binary\Loader; +use Liip\ImagineBundle\Exception\Binary\Loader\ChainAttemptNotLoadableException; +use Liip\ImagineBundle\Exception\Binary\Loader\ChainNotLoadableException; use Liip\ImagineBundle\Exception\Binary\Loader\NotLoadableException; class ChainLoader implements LoaderInterface { /** - * @var LoaderInterface[] + * @var array */ private array $loaders; /** - * @param LoaderInterface[] $loaders + * @param array $loaders */ public function __construct(array $loaders) { @@ -37,36 +39,14 @@ public function find($path) { $exceptions = []; - foreach ($this->loaders as $loader) { + foreach ($this->loaders as $configName => $loader) { try { return $loader->find($path); - } catch (\Exception $e) { - $exceptions[$e->getMessage()] = $loader; + } catch (NotLoadableException $loaderException) { + $exceptions[] = new ChainAttemptNotLoadableException($configName, $loader, $loaderException); } } - throw new NotLoadableException(self::getLoaderExceptionMessage($path, $exceptions, $this->loaders)); - } - - /** - * @param array $exceptions - * @param array $loaders - */ - private static function getLoaderExceptionMessage(string $path, array $exceptions, array $loaders): string - { - $loaderMessages = array_map(static function (string $name, LoaderInterface $loader) { - return sprintf('%s=[%s]', (new \ReflectionObject($loader))->getShortName(), $name); - }, array_keys($loaders), $loaders); - - $exceptionMessages = array_map(static function (string $message, LoaderInterface $loader) { - return sprintf('%s=[%s]', (new \ReflectionObject($loader))->getShortName(), $message); - }, array_keys($exceptions), $exceptions); - - return vsprintf('Source image not resolvable "%s" using "%s" %d loaders (internal exceptions: %s).', [ - $path, - implode(', ', $loaderMessages), - \count($loaders), - implode(', ', $exceptionMessages), - ]); + throw new ChainNotLoadableException($path, ...$exceptions); } } diff --git a/src/Exception/Binary/Loader/ChainAttemptNotLoadableException.php b/src/Exception/Binary/Loader/ChainAttemptNotLoadableException.php new file mode 100644 index 000000000..ed17bd80b --- /dev/null +++ b/src/Exception/Binary/Loader/ChainAttemptNotLoadableException.php @@ -0,0 +1,53 @@ +configName = $configName; + $this->loaderInst = $loaderInst; + + parent::__construct($this->compileFailureText(), 0, $loaderException); + } + + public function getLoaderConfigName(): string + { + return $this->configName; + } + + public function getLoaderObjectInst(): LoaderInterface + { + return $this->loaderInst; + } + + public function getLoaderObjectName(): string + { + return (new \ReflectionObject($this->getLoaderObjectInst()))->getShortName(); + } + + public function getLoaderPriorError(): string + { + return $this->getPrevious()->getMessage(); + } + + private function compileFailureText(): string + { + return sprintf('%s=[%s]', $this->getLoaderObjectName(), $this->getLoaderConfigName()); + } +} diff --git a/src/Exception/Binary/Loader/ChainNotLoadableException.php b/src/Exception/Binary/Loader/ChainNotLoadableException.php new file mode 100644 index 000000000..60a203d6b --- /dev/null +++ b/src/Exception/Binary/Loader/ChainNotLoadableException.php @@ -0,0 +1,46 @@ +getMessage(); + }, ...$exceptions); + } + + private static function compileLoaderErrorsList(ChainAttemptNotLoadableException ...$exceptions): string + { + return self::implodeArrayMappedExceptions(static function (ChainAttemptNotLoadableException $e): string { + return sprintf('%s=[%s]', $e->getLoaderObjectName(), $e->getLoaderPriorError()); + }, ...$exceptions); + } + + private static function implodeArrayMappedExceptions(\Closure $mapper, ChainAttemptNotLoadableException ...$exceptions): string + { + return implode(', ', array_map($mapper, $exceptions)); + } +} diff --git a/tests/Binary/Loader/ChainLoaderTest.php b/tests/Binary/Loader/ChainLoaderTest.php index 0040698e1..ca10d32cb 100644 --- a/tests/Binary/Loader/ChainLoaderTest.php +++ b/tests/Binary/Loader/ChainLoaderTest.php @@ -23,6 +23,8 @@ /** * @covers \Liip\ImagineBundle\Binary\Loader\ChainLoader + * @covers \Liip\ImagineBundle\Exception\Binary\Loader\ChainAttemptNotLoadableException + * @covers \Liip\ImagineBundle\Exception\Binary\Loader\ChainNotLoadableException */ class ChainLoaderTest extends AbstractTest {