39 lines
960 B
PHP
39 lines
960 B
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Services\Mjml;
|
|
|
|
use Symfony\Component\Process\Exception\ProcessFailedException;
|
|
use Symfony\Component\Process\Process;
|
|
|
|
class MjmlLocalBinaryRenderer implements MjmlRenderer
|
|
{
|
|
public function __construct(
|
|
private readonly string $binaryPath
|
|
) {
|
|
if (empty(mb_trim($binaryPath))) {
|
|
throw new MjmlException('no binary path provided');
|
|
}
|
|
}
|
|
|
|
public function toHtml(string $mjml): string
|
|
{
|
|
$process = new Process([$this->binaryPath, '-i', '-s']);
|
|
$process->setInput($mjml);
|
|
$process->setTimeout(30);
|
|
$process->run();
|
|
|
|
if (! $process->isSuccessful()) {
|
|
throw new ProcessFailedException($process);
|
|
}
|
|
|
|
$output = mb_trim($process->getOutput());
|
|
|
|
if (empty($output)) {
|
|
throw new MjmlException('MJML compilation returned empty output.');
|
|
}
|
|
|
|
return $output;
|
|
}
|
|
}
|