mirror of
https://github.com/Intervention/image.git
synced 2025-08-30 17:19:50 +02:00
Add interface for driver specializing objects
This commit is contained in:
@@ -23,7 +23,7 @@ abstract class AbstractDriver implements DriverInterface
|
||||
* @return object
|
||||
* @throws NotSupportedException
|
||||
*/
|
||||
public function resolve(object $input): object
|
||||
public function specialize(object $input): object
|
||||
{
|
||||
if ($this->isExternal($input)) {
|
||||
return $input;
|
||||
@@ -31,15 +31,18 @@ abstract class AbstractDriver implements DriverInterface
|
||||
|
||||
$driver_namespace = (new ReflectionClass($this))->getNamespaceName();
|
||||
$class_path = substr(get_class($input), strlen("Intervention\\Image\\"));
|
||||
$specialized = $driver_namespace . "\\" . $class_path;
|
||||
$classname = $driver_namespace . "\\" . $class_path;
|
||||
|
||||
if (! class_exists($specialized)) {
|
||||
if (!class_exists($classname)) {
|
||||
throw new NotSupportedException(
|
||||
"Class '" . $class_path . "' is not supported by " . $this->id() . " driver."
|
||||
);
|
||||
}
|
||||
|
||||
return new $specialized($input, $this);
|
||||
return forward_static_call([
|
||||
$classname,
|
||||
'buildSpecialized'
|
||||
], $input, $this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
54
src/Drivers/DriverSpecialized.php
Normal file
54
src/Drivers/DriverSpecialized.php
Normal file
@@ -0,0 +1,54 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Drivers;
|
||||
|
||||
use Intervention\Image\Interfaces\DriverInterface;
|
||||
use Intervention\Image\Interfaces\SpecializedInterface;
|
||||
|
||||
class DriverSpecialized implements SpecializedInterface
|
||||
{
|
||||
protected DriverInterface $driver;
|
||||
protected object $generic;
|
||||
|
||||
public static function buildSpecialized(object $generic, DriverInterface $driver): SpecializedInterface
|
||||
{
|
||||
$specialized = new static();
|
||||
$specialized->generic = $generic;
|
||||
$specialized->driver = $driver;
|
||||
|
||||
return $specialized;
|
||||
}
|
||||
|
||||
public function driver(): DriverInterface
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
public function generic(): object
|
||||
{
|
||||
return $this->generic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to read attributes of underlying modifier
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
return $this->generic->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to call methods of underlying modifier
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
return $this->generic->$name(...$arguments);
|
||||
}
|
||||
}
|
@@ -3,29 +3,7 @@
|
||||
namespace Intervention\Image\Drivers;
|
||||
|
||||
use Intervention\Image\Interfaces\AnalyzerInterface;
|
||||
use Intervention\Image\Interfaces\DriverInterface;
|
||||
|
||||
abstract class DriverSpecializedAnalyzer implements AnalyzerInterface
|
||||
abstract class DriverSpecializedAnalyzer extends DriverSpecialized implements AnalyzerInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected AnalyzerInterface $analyzer,
|
||||
protected DriverInterface $driver
|
||||
) {
|
||||
}
|
||||
|
||||
public function driver(): DriverInterface
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to read attributes of underlying analyzer
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
return $this->analyzer->$name;
|
||||
}
|
||||
}
|
||||
|
@@ -2,33 +2,10 @@
|
||||
|
||||
namespace Intervention\Image\Drivers;
|
||||
|
||||
use Intervention\Image\Interfaces\DriverInterface;
|
||||
use Intervention\Image\Interfaces\EncoderInterface;
|
||||
|
||||
abstract class DriverSpecializedEncoder implements EncoderInterface
|
||||
abstract class DriverSpecializedEncoder extends DriverSpecialized implements EncoderInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected EncoderInterface $encoder,
|
||||
protected DriverInterface $driver
|
||||
) {
|
||||
}
|
||||
|
||||
public function driver(): DriverInterface
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to read attributes of underlying encoder
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
return $this->encoder->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get return value of callback through output buffer
|
||||
*
|
||||
|
@@ -2,42 +2,8 @@
|
||||
|
||||
namespace Intervention\Image\Drivers;
|
||||
|
||||
use Intervention\Image\Interfaces\DriverInterface;
|
||||
use Intervention\Image\Interfaces\ModifierInterface;
|
||||
|
||||
abstract class DriverSpecializedModifier implements ModifierInterface
|
||||
abstract class DriverSpecializedModifier extends DriverSpecialized implements ModifierInterface
|
||||
{
|
||||
public function __construct(
|
||||
protected ModifierInterface $modifier,
|
||||
protected DriverInterface $driver
|
||||
) {
|
||||
}
|
||||
|
||||
public function driver(): DriverInterface
|
||||
{
|
||||
return $this->driver;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to read attributes of underlying modifier
|
||||
*
|
||||
* @param string $name
|
||||
* @return mixed
|
||||
*/
|
||||
public function __get(string $name): mixed
|
||||
{
|
||||
return $this->modifier->$name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic method to call methods of underlying modifier
|
||||
*
|
||||
* @param string $name
|
||||
* @param array $arguments
|
||||
* @return mixed
|
||||
*/
|
||||
public function __call(string $name, array $arguments): mixed
|
||||
{
|
||||
return $this->modifier->$name(...$arguments);
|
||||
}
|
||||
}
|
||||
|
@@ -229,7 +229,7 @@ final class Image implements ImageInterface
|
||||
*/
|
||||
public function modify(ModifierInterface $modifier): ImageInterface
|
||||
{
|
||||
return $this->driver->resolve($modifier)->apply($this);
|
||||
return $this->driver->specialize($modifier)->apply($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -239,7 +239,7 @@ final class Image implements ImageInterface
|
||||
*/
|
||||
public function analyze(AnalyzerInterface $analyzer): mixed
|
||||
{
|
||||
return $this->driver->resolve($analyzer)->analyze($this);
|
||||
return $this->driver->specialize($analyzer)->analyze($this);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -249,7 +249,7 @@ final class Image implements ImageInterface
|
||||
*/
|
||||
public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedImage
|
||||
{
|
||||
return $this->driver->resolve($encoder)->encode($this);
|
||||
return $this->driver->specialize($encoder)->encode($this);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@@ -17,7 +17,7 @@ interface DriverInterface
|
||||
* @param object $input
|
||||
* @return object
|
||||
*/
|
||||
public function resolve(object $input): object;
|
||||
public function specialize(object $input): object;
|
||||
|
||||
/**
|
||||
* Create new image instance with the current driver in given dimensions
|
||||
|
29
src/Interfaces/SpecializedInterface.php
Normal file
29
src/Interfaces/SpecializedInterface.php
Normal file
@@ -0,0 +1,29 @@
|
||||
<?php
|
||||
|
||||
namespace Intervention\Image\Interfaces;
|
||||
|
||||
interface SpecializedInterface
|
||||
{
|
||||
/**
|
||||
* Return current driver instance
|
||||
*
|
||||
* @return DriverInterface
|
||||
*/
|
||||
public function driver(): DriverInterface;
|
||||
|
||||
/**
|
||||
* Return driverless generic object
|
||||
*
|
||||
* @return object
|
||||
*/
|
||||
public function generic(): object;
|
||||
|
||||
/**
|
||||
* Build driver specialized version of given generic object for given driver
|
||||
*
|
||||
* @param object $generic
|
||||
* @param DriverInterface $driver
|
||||
* @return SpecializedInterface
|
||||
*/
|
||||
public static function buildSpecialized(object $generic, DriverInterface $driver): SpecializedInterface;
|
||||
}
|
Reference in New Issue
Block a user