1
0
mirror of https://github.com/Intervention/image.git synced 2025-08-31 17:41:58 +02:00

Add interface for driver specializing objects

This commit is contained in:
Oliver Vogel
2023-12-31 17:26:41 +01:00
parent 3f96a8f752
commit 1148c84147
8 changed files with 97 additions and 90 deletions

View File

@@ -23,7 +23,7 @@ abstract class AbstractDriver implements DriverInterface
* @return object * @return object
* @throws NotSupportedException * @throws NotSupportedException
*/ */
public function resolve(object $input): object public function specialize(object $input): object
{ {
if ($this->isExternal($input)) { if ($this->isExternal($input)) {
return $input; return $input;
@@ -31,15 +31,18 @@ abstract class AbstractDriver implements DriverInterface
$driver_namespace = (new ReflectionClass($this))->getNamespaceName(); $driver_namespace = (new ReflectionClass($this))->getNamespaceName();
$class_path = substr(get_class($input), strlen("Intervention\\Image\\")); $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( throw new NotSupportedException(
"Class '" . $class_path . "' is not supported by " . $this->id() . " driver." "Class '" . $class_path . "' is not supported by " . $this->id() . " driver."
); );
} }
return new $specialized($input, $this); return forward_static_call([
$classname,
'buildSpecialized'
], $input, $this);
} }
/** /**

View 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);
}
}

View File

@@ -3,29 +3,7 @@
namespace Intervention\Image\Drivers; namespace Intervention\Image\Drivers;
use Intervention\Image\Interfaces\AnalyzerInterface; 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;
}
} }

View File

@@ -2,33 +2,10 @@
namespace Intervention\Image\Drivers; namespace Intervention\Image\Drivers;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\EncoderInterface; 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 * Get return value of callback through output buffer
* *

View File

@@ -2,42 +2,8 @@
namespace Intervention\Image\Drivers; namespace Intervention\Image\Drivers;
use Intervention\Image\Interfaces\DriverInterface;
use Intervention\Image\Interfaces\ModifierInterface; 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);
}
} }

View File

@@ -229,7 +229,7 @@ final class Image implements ImageInterface
*/ */
public function modify(ModifierInterface $modifier): 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 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 public function encode(EncoderInterface $encoder = new AutoEncoder()): EncodedImage
{ {
return $this->driver->resolve($encoder)->encode($this); return $this->driver->specialize($encoder)->encode($this);
} }
/** /**

View File

@@ -17,7 +17,7 @@ interface DriverInterface
* @param object $input * @param object $input
* @return object * @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 * Create new image instance with the current driver in given dimensions

View 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;
}