1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-26 07:14:31 +02:00

Refactor driver instantiation in ImageManager

This commit is contained in:
Oliver Vogel
2023-11-12 14:46:25 +01:00
parent b1eee25bfb
commit d5a7099d53

View File

@@ -4,14 +4,9 @@ namespace Intervention\Image;
use Intervention\Image\Exceptions\ConfigurationException; use Intervention\Image\Exceptions\ConfigurationException;
use Intervention\Image\Interfaces\ImageInterface; use Intervention\Image\Interfaces\ImageInterface;
use Intervention\Image\Traits\CanResolveDriverClass;
class ImageManager class ImageManager
{ {
use CanResolveDriverClass;
protected const AVAILABLE_DRIVERS = ['gd', 'imagick'];
/** /**
* Create new ImageManager instance * Create new ImageManager instance
* *
@@ -21,10 +16,8 @@ class ImageManager
*/ */
public function __construct(protected string $driver = 'gd') public function __construct(protected string $driver = 'gd')
{ {
if (! in_array(strtolower($driver), self::AVAILABLE_DRIVERS)) { if (!$this->driverExists()) {
throw new ConfigurationException( throw new ConfigurationException('Driver ' . $driver . ' is not available.');
'Driver ' . $driver . ' not available.'
);
} }
} }
@@ -94,12 +87,39 @@ class ImageManager
} }
/** /**
* Return id of current driver * Resolve given classname with configured driver
* *
* @return object
*/
private function resolveDriverClass(string $classname): object
{
$classname = $this->driverClassname($classname);
return new $classname();
}
/**
* Build full namespaced classname of given class for configured driver
*
* @param string $classname
* @return string * @return string
*/ */
protected function getCurrentDriver(): string private function driverClassname(string $classname): string
{ {
return strtolower($this->driver); return sprintf(
"Intervention\Image\Drivers\%s\%s",
ucfirst($this->driver),
$classname
);
}
/**
* Determine if configured driver exists
*
* @return bool
*/
private function driverExists(): bool
{
return class_exists($this->driverClassname('Image'));
} }
} }