Skip to content

Commit

Permalink
Merge pull request #17 from pionl/16-interface-version-check
Browse files Browse the repository at this point in the history
Add interface for Ghostcript and move version check to device process
  • Loading branch information
schroedan authored Feb 27, 2023
2 parents c97c82b + d0deaa4 commit 2eb27b8
Show file tree
Hide file tree
Showing 16 changed files with 317 additions and 173 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ $ curl -s https://getcomposer.org/installer | php
Require the package via Composer:

```bash
$ php composer.phar require gravitymedia/ghostscript:v2.2
$ php composer.phar require gravitymedia/ghostscript:v2.3
```

## Usage
Expand Down
25 changes: 15 additions & 10 deletions src/Device/AbstractDevice.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace GravityMedia\Ghostscript\Device;

use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\GhostscriptInterface;
use GravityMedia\Ghostscript\Input;
use GravityMedia\Ghostscript\Process\Argument;
use GravityMedia\Ghostscript\Process\Arguments;
Expand Down Expand Up @@ -72,25 +73,18 @@ abstract class AbstractDevice

/**
* The Ghostscript object.
*
* @var Ghostscript
*/
private $ghostscript;
protected GhostscriptInterface $ghostscript;

/**
* The arguments object.
*
* @var Arguments
*/
private $arguments;
private Arguments $arguments;

/**
* Create abstract device object.
*
* @param Ghostscript $ghostscript
* @param Arguments $arguments
*/
public function __construct(Ghostscript $ghostscript, Arguments $arguments)
public function __construct(GhostscriptInterface $ghostscript, Arguments $arguments)
{
$this->ghostscript = $ghostscript;
$this->arguments = $arguments;
Expand Down Expand Up @@ -227,6 +221,12 @@ protected function createProcessArguments(Input $input)
*/
public function createProcess($input = null)
{
$requiredVersion = $this->getRequiredVersion();
$version = $this->ghostscript->getVersion();
if (version_compare($requiredVersion, $version) > 0) {
throw new \RuntimeException("Ghostscript version {$requiredVersion} or higher is required");
}

$input = $this->sanitizeInput($input);

$arguments = $this->createProcessArguments($input);
Expand All @@ -243,4 +243,9 @@ public function createProcess($input = null)
$this->ghostscript->getOption('timeout', 60)
);
}

protected function getRequiredVersion(): string
{
return '9.00';
}
}
3 changes: 2 additions & 1 deletion src/Device/BoundingBoxInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace GravityMedia\Ghostscript\Device;

use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\GhostscriptInterface;
use GravityMedia\Ghostscript\Process\Arguments;

/**
Expand All @@ -25,7 +26,7 @@ class BoundingBoxInfo extends AbstractDevice
* @param Ghostscript $ghostscript
* @param Arguments $arguments
*/
public function __construct(Ghostscript $ghostscript, Arguments $arguments)
public function __construct(GhostscriptInterface $ghostscript, Arguments $arguments)
{
parent::__construct($ghostscript, $arguments->setArgument('-sDEVICE=bbox'));
}
Expand Down
5 changes: 2 additions & 3 deletions src/Device/Inkcov.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace GravityMedia\Ghostscript\Device;

use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\GhostscriptInterface;
use GravityMedia\Ghostscript\Process\Arguments as ProcessArguments;
use GravityMedia\Ghostscript\Process\Arguments;

Expand All @@ -14,13 +15,11 @@
*/
class Inkcov extends AbstractDevice
{
const POSTSCRIPT_COMMANDS = '';

/**
* @param Ghostscript $ghostscript
* @param ProcessArguments $arguments
*/
public function __construct(Ghostscript $ghostscript, Arguments $arguments)
public function __construct(GhostscriptInterface $ghostscript, Arguments $arguments)
{
parent::__construct($ghostscript, $arguments->setArgument('-sDEVICE=inkcov'));
}
Expand Down
3 changes: 2 additions & 1 deletion src/Device/NoDisplay.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace GravityMedia\Ghostscript\Device;

use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\GhostscriptInterface;
use GravityMedia\Ghostscript\Process\Arguments;

/**
Expand All @@ -23,7 +24,7 @@ class NoDisplay extends AbstractDevice
* @param Ghostscript $ghostscript
* @param Arguments $arguments
*/
public function __construct(Ghostscript $ghostscript, Arguments $arguments)
public function __construct(GhostscriptInterface $ghostscript, Arguments $arguments)
{
parent::__construct($ghostscript, $arguments->setArgument('-dNODISPLAY'));
}
Expand Down
7 changes: 3 additions & 4 deletions src/Device/PdfInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace GravityMedia\Ghostscript\Device;

use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\GhostscriptInterface;
use GravityMedia\Ghostscript\Process\Arguments;

/**
Expand All @@ -23,10 +24,8 @@ class PdfInfo extends NoDisplay
{
/**
* The PDF info path.
*
* @var string
*/
private $pdfInfoPath;
private string $pdfInfoPath;

/**
* Create PDF info device object.
Expand All @@ -35,7 +34,7 @@ class PdfInfo extends NoDisplay
* @param Arguments $arguments
* @param string $pdfInfoPath Path to toolbin/pdf_info.ps
*/
public function __construct(Ghostscript $ghostscript, Arguments $arguments, $pdfInfoPath)
public function __construct(GhostscriptInterface $ghostscript, Arguments $arguments, $pdfInfoPath)
{
parent::__construct($ghostscript, $arguments);

Expand Down
13 changes: 3 additions & 10 deletions src/Device/PdfWrite.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use GravityMedia\Ghostscript\Enum\PdfSettings;
use GravityMedia\Ghostscript\Enum\ProcessColorModel;
use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\GhostscriptInterface;
use GravityMedia\Ghostscript\Input;
use GravityMedia\Ghostscript\Process\Arguments;

Expand Down Expand Up @@ -60,24 +61,16 @@ class PdfWrite extends AbstractDevice
*/
use DistillerParameters\AdvancedTrait;

/**
* The Ghostscript binary version.
*
* @var string
*/
protected $ghostscriptVersion;

/**
* Create PDF write device object.
*
* @param Ghostscript $ghostscript
* @param Arguments $arguments
*/
public function __construct(Ghostscript $ghostscript, Arguments $arguments)
public function __construct(GhostscriptInterface $ghostscript, Arguments $arguments)
{
parent::__construct($ghostscript, $arguments->setArgument('-sDEVICE=pdfwrite'));

$this->ghostscriptVersion = $ghostscript->getVersion();
$this->setPdfSettings(PdfSettings::__DEFAULT);
}

Expand Down Expand Up @@ -184,7 +177,7 @@ protected function createProcessArguments(Input $input)
* the pdfwrite device.
* @link https://ghostscript.com/doc/current/Language.htm#.setpdfwrite
*/
if (version_compare($this->ghostscriptVersion, '9.50', '<') && false === strstr($code, '.setpdfwrite')) {
if (version_compare($this->ghostscript->getVersion(), '9.50', '<') && false === strstr($code, '.setpdfwrite')) {
$input->setPostScriptCode(ltrim($code . ' .setpdfwrite', ' '));
}

Expand Down
9 changes: 3 additions & 6 deletions src/Ghostscript.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
*
* @package GravityMedia\Ghostscript
*/
class Ghostscript
class Ghostscript implements GhostscriptInterface
{
/**
* The default binary.
Expand All @@ -39,7 +39,8 @@ class Ghostscript
*
* @var array
*/
protected $options;
protected $options = [];


/**
* Create Ghostscript object.
Expand All @@ -51,10 +52,6 @@ class Ghostscript
public function __construct(array $options = [])
{
$this->options = $options;

if (version_compare('9.00', $this->getVersion()) > 0) {
throw new \RuntimeException('Ghostscript version 9.00 or higher is required');
}
}

/**
Expand Down
78 changes: 78 additions & 0 deletions src/GhostscriptInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php
/**
* This file is part of the Ghostscript package.
*
* @author Daniel Schröder <daniel.schroeder@gravitymedia.de>
*/

namespace GravityMedia\Ghostscript;

use GravityMedia\Ghostscript\Device\BoundingBoxInfo;
use GravityMedia\Ghostscript\Device\Inkcov;
use GravityMedia\Ghostscript\Device\NoDisplay;
use GravityMedia\Ghostscript\Device\PdfInfo;
use GravityMedia\Ghostscript\Device\PdfWrite;

/**
* @package GravityMedia\Ghostscript
*/
interface GhostscriptInterface
{
/**
* Get option.
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getOption($name, $default = null);

/**
* Get version.
*
* @return string
* @throws \RuntimeException
*
*/
public function getVersion();

/**
* Create PDF device object.
*
* @param null|string $outputFile
*
* @return PdfWrite
*/
public function createPdfDevice($outputFile = null);

/**
* Create no display device object.
*
* @return NoDisplay
*/
public function createNoDisplayDevice();

/**
* Create PDF info device object.
*
* @param string $pdfInfoPath Path to toolbin/pdf_info.ps
*
* @return PdfInfo
*/
public function createPdfInfoDevice($pdfInfoPath);

/**
* Create bounding box info device object.
*
* @return BoundingBoxInfo
*/
public function createBoundingBoxInfoDevice();

/**
* Create inkcov device object
*
* @return Inkcov
*/
public function createInkcovDevice();
}
15 changes: 9 additions & 6 deletions tests/src/Device/BoundingBoxInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace GravityMedia\GhostscriptTest\Device;

use GravityMedia\Ghostscript\Device\AbstractDevice;
use GravityMedia\Ghostscript\Device\BoundingBoxInfo;
use GravityMedia\Ghostscript\Ghostscript;
use GravityMedia\Ghostscript\Process\Argument;
Expand All @@ -25,18 +26,20 @@
* @uses \GravityMedia\Ghostscript\Process\Argument
* @uses \GravityMedia\Ghostscript\Process\Arguments
*/
class BoundingBoxInfoTest extends TestCase
class BoundingBoxInfoTest extends DeviceTestCase
{
public function testDeviceCreation()
protected function createDevice(?string $version = null): AbstractDevice
{
$ghostscript = new Ghostscript();
$arguments = new Arguments();
return new BoundingBoxInfo($this->getGhostscript($version), $this->arguments);
}

$device = new BoundingBoxInfo($ghostscript, $arguments);
public function testDeviceCreation()
{
$device = $this->createDevice();

$this->assertInstanceOf(BoundingBoxInfo::class, $device);

$argument = $arguments->getArgument('-sDEVICE');
$argument = $this->arguments->getArgument('-sDEVICE');

$this->assertInstanceOf(Argument::class, $argument);
$this->assertEquals('bbox', $argument->getValue());
Expand Down
Loading

0 comments on commit 2eb27b8

Please sign in to comment.