Skip to content

Commit

Permalink
Use replacement placeholders in the render command (#767)
Browse files Browse the repository at this point in the history
* Switch to using process command placeholders rather than environment
variables in constructing the rsvg-convert command.
* Only pass required placeholders.
* Use Symfony's AcceptHeader class to reformat the lang value.

Bug: T365644
  • Loading branch information
samwilson authored May 23, 2024
1 parent da9c0e0 commit cc0aef7
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions src/Service/Renderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace App\Service;

use Symfony\Component\HttpFoundation\AcceptHeader;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;

Expand Down Expand Up @@ -35,19 +36,23 @@ public function __construct(string $rsvgCommand)
public function render(string $file, string $lang, ?string $outFile = null) : string
{
// Construct the command, using variables that will be escaped when it's run.
$command = $this->rsvgCommand.' "$SVG"';
$command = $this->rsvgCommand.' "${:SVG}"';
$env = ['SVG' => $file];
if ('fallback' !== $lang) {
// Set the language to use from the SVG systemLanguage.
// If the fallback language is being requested, the OS's default will be
// used instead (as is done in MediaWiki).
$command .= " --accept-language=$lang";
$command .= ' --accept-language="${:LANG}"';
// Reformat the lang as an accept header.
$env['LANG'] = (string)AcceptHeader::fromString($lang);
}
if ($outFile) {
// Redirect to output file if required.
$command .= ' > "$PNG"';
$command .= ' > "${:PNG}"';
$env['PNG'] = $outFile;
}
$process = Process::fromShellCommandline($command);
$process->mustRun(null, ['SVG' => $file, 'PNG' => $outFile]);
$process->mustRun(null, $env);
return $process->getOutput();
}
}

0 comments on commit cc0aef7

Please sign in to comment.