Skip to content

Commit

Permalink
Avoid driver info code duplication, avoid bitwise feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
mlocati committed Oct 18, 2021
1 parent 1cd089c commit dd2e831
Show file tree
Hide file tree
Showing 7 changed files with 346 additions and 487 deletions.
208 changes: 208 additions & 0 deletions src/Driver/AbstractInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
<?php

namespace Imagine\Driver;

use Imagine\Exception\NotSupportedException;
use Imagine\Image\Palette\PaletteInterface;

/**
* Base class for the default DriverInfo classes.
*
* @since 1.3.0
*/
abstract class AbstractInfo implements Info
{
/**
* @var static|null|false
*/
private static $instance = false;

/**
* @var string
*/
private $driverRawVersion;

/**
* @var string
*/
private $driverSemverVersion;

/**
* @var string
*/
private $engineRawVersion;

/**
* @var string
*/
private $engineSemverVersion;

/**
* @var \Imagine\Image\FormatList|null
*/
private $supportedFormats = null;

/**
* @param string $driverRawVersion
* @param string $driverSemverVersion
* @param string $engineRawVersion
* @param string $engineSemverVersion
*/
protected function __construct($driverRawVersion, $driverSemverVersion, $engineRawVersion, $engineSemverVersion)
{
$this->driverRawVersion = $driverRawVersion;
$this->driverSemverVersion = $driverSemverVersion;
$this->engineRawVersion = $engineRawVersion;
$this->engineSemverVersion = $engineSemverVersion;
}

/**
* Create an instance of this Driver info.
*
* @return static|null return NULL if the driver is not available
*/
abstract protected static function buildInstance();

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::get()
*/
public static function get($required = true)
{
if (self::$instance === false) {
self::$instance = static::buildInstance();
}
if (self::$instance === null && $required) {
throw new NotSupportedException('Gd not installed');
}

return self::$instance;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::getDriverVersion()
*/
public function getDriverVersion($raw = false)
{
return $raw ? $this->driverRawVersion : $this->driverSemverVersion;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::getEngineVersion()
*/
public function getEngineVersion($raw = false)
{
return $raw ? $this->engineRawVersion : $this->engineSemverVersion;
}

/**
* Check if the driver has a specific feature.
*
* @param int $feature The feature to be checked (see the Info::FEATURE_... constants)
*
* @throws \Imagine\Exception\NotSupportedException if any of the requested features is not supported
*/
abstract protected function checkFeature($feature);

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::checkVersionIsSupported()
*/
public function checkVersionIsSupported()
{
if (!defined('PHP_VERSION_ID') || PHP_VERSION_ID < 50300) {
throw new NotSupportedException('Imagine requires PHP 5.3 or later');
}
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::requireFeature()
*/
public function requireFeature($features)
{
$features = array_map('intval', is_array($features) ? $features : array($features));
foreach ($features as $feature) {
$this->checkFeature($feature);
}
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::hasFeature()
*/
public function hasFeature($features)
{
try {
$this->requireFeature($features);
} catch (NotSupportedException $x) {
return false;
}

return true;
}

/**
* Build the list of supported file formats.
*
* @return \Imagine\Image\FormatList
*/
abstract protected function buildSupportedFormats();

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::getSupportedFormats()
*/
public function getSupportedFormats()
{
if ($this->supportedFormats === null) {
$this->supportedFormats = $this->buildSupportedFormats();
}

return $this->supportedFormats;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::isFormatSupported()
*/
public function isFormatSupported($format)
{
return $this->getSupportedFormats()->find($format) !== null;
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::requirePaletteSupport()
*/
public function requirePaletteSupport(PaletteInterface $palette)
{
}

/**
* {@inheritdoc}
*
* @see \Imagine\Driver\Info::isPaletteSupported()
*/
public function isPaletteSupported(PaletteInterface $palette)
{
try {
$this->requirePaletteSupport($palette);
} catch (NotSupportedException $x) {
return false;
}

return true;
}
}
30 changes: 15 additions & 15 deletions src/Driver/Info.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ interface Info
*
* @var int
*/
const FEATURE_COLORPROFILES = 0x1;
const FEATURE_COLORPROFILES = 1;

/**
* Affected functions: Imagine\Image\ImageInterface::usePalette(), opening images with particular colorspaces.
*
* See also the checkPaletteSupport/isPaletteSupported driver info methods.
* See also the requirePaletteSupport/isPaletteSupported driver info methods.
*
* @var int
*/
const FEATURE_COLORSPACECONVERSION = 0x2;
const FEATURE_COLORSPACECONVERSION = 2;

/**
* Affected functions: Imagine\Effects\EffectsInterface::grayscale().
*
* @var int
*/
const FEATURE_GRAYSCALEEFFECT = 0x4;
const FEATURE_GRAYSCALEEFFECT = 3;

/**
* Affected functions: Imagine\Image\LayersInterface::coalesce().
Expand All @@ -41,56 +41,56 @@ interface Info
*
* @var int
*/
const FEATURE_COALESCELAYERS = 0x8;
const FEATURE_COALESCELAYERS = 4;

/**
* Affected functions: Imagine\Effects\EffectsInterface::negative().
*
* @var int
*/
const FEATURE_NEGATEIMAGE = 0x10;
const FEATURE_NEGATEIMAGE = 5;

/**
* Affected functions: Imagine\Effects\EffectsInterface::colorize().
*
* @var int
*/
const FEATURE_COLORIZEIMAGE = 0x20;
const FEATURE_COLORIZEIMAGE = 6;

/**
* Affected functions: Imagine\Effects\EffectsInterface::sharpen().
*
* @var int
*/
const FEATURE_SHARPENIMAGE = 0x40;
const FEATURE_SHARPENIMAGE = 7;

/**
* Affected functions: Imagine\Effects\EffectsInterface::convolve().
*
* @var int
*/
const FEATURE_CONVOLVEIMAGE = 0x80;
const FEATURE_CONVOLVEIMAGE = 8;

/**
* Affected functions: Imagine\Draw\DrawerInterface::text() and Imagine\Image\FontInterface::box().
*
* @var int
*/
const FEATURE_TEXTFUNCTIONS = 0x100;
const FEATURE_TEXTFUNCTIONS = 9;

/**
* Affected functions: Imagine\Image\LayersInterface methods that would create more that 1 layer or 0 layers.
*
* @var int
*/
const FEATURE_MULTIPLELAYERS = 0x200;
const FEATURE_MULTIPLELAYERS = 10;

/**
* Affected functions: none at the moment.
*
* @var int
*/
const FEATURE_CUSTOMRESOLUTION = 0x400;
const FEATURE_CUSTOMRESOLUTION = 11;

/**
* Get the Info instance for a specific driver.
Expand Down Expand Up @@ -135,7 +135,7 @@ public function getEngineVersion($raw = false);
/**
* Check if the driver the features requested.
*
* @param int $features A combination of one or more of the FEATURE_... values
* @param int|int[] $features The features to be checked (see the Info::FEATURE_... constants)
*
* @return bool returns TRUE if the driver supports all the specified features, FALSE otherwise
*/
Expand All @@ -144,7 +144,7 @@ public function hasFeature($features);
/**
* Check if the driver has the features requested.
*
* @param int $features A combination of one or more of the FEATURE_... values
* @param int|int[] $features The features to be checked (see the Info::FEATURE_... constants)
*
* @throws \Imagine\Exception\NotSupportedException if any of the requested features is not supported
*/
Expand Down Expand Up @@ -173,7 +173,7 @@ public function isFormatSupported($format);
*
* @throws \Imagine\Exception\NotSupportedException if the palette is not supported
*/
public function checkPaletteSupport(PaletteInterface $palette);
public function requirePaletteSupport(PaletteInterface $palette);

/**
* Check if a palette is supported.
Expand Down
Loading

0 comments on commit dd2e831

Please sign in to comment.