-
Notifications
You must be signed in to change notification settings - Fork 528
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid driver info code duplication, avoid bitwise feature flags
- Loading branch information
Showing
7 changed files
with
346 additions
and
487 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.