Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
141 additions
and
27 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
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
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
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
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
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
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
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
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,43 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace App\Service; | ||
|
||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
use Symfony\Component\Process\Process; | ||
|
||
/** | ||
* SVG to PNG rendering service. | ||
*/ | ||
class Renderer | ||
{ | ||
|
||
/** @var string */ | ||
protected $rsvgCommand; | ||
|
||
/** | ||
* @param string $rsvgCommand The command to execute to do the conversion. | ||
*/ | ||
public function __construct(string $rsvgCommand) | ||
{ | ||
$this->rsvgCommand = $rsvgCommand; | ||
} | ||
|
||
/** | ||
* @param string $file Full filesystem path to the SVG file to render. | ||
* @param string $lang Code of the language in which to render the image. | ||
* @throws ProcessFailedException If the PNG conversion failed. | ||
* @return string The PNG image contents. | ||
*/ | ||
public function render(string $file, string $lang) : string | ||
{ | ||
$process = new Process([$this->rsvgCommand, $file]); | ||
// Set the LANG environment variable, which will be interpreted as the SVG systemLanguage. | ||
$process->setEnv(['LANG' => $lang]); | ||
$process->run(); | ||
if (!$process->isSuccessful()) { | ||
throw new ProcessFailedException($process); | ||
} | ||
return $process->getOutput(); | ||
} | ||
} |
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
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
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
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,20 @@ | ||
<?php | ||
declare(strict_types = 1); | ||
|
||
namespace App\Tests\Service; | ||
|
||
use App\Service\Renderer; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Process\Exception\ProcessFailedException; | ||
|
||
class RendererTest extends TestCase | ||
{ | ||
|
||
public function testInvalidCommand() : void | ||
{ | ||
$renderer = new Renderer('foo'); | ||
static::expectException(ProcessFailedException::class); | ||
static::expectExceptionMessage('foo: not found'); | ||
$renderer->render('foo.svg', 'fr'); | ||
} | ||
} |