-
Notifications
You must be signed in to change notification settings - Fork 7
/
Rasterizer.php
46 lines (33 loc) · 1.09 KB
/
Rasterizer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
<?php
namespace Padam87\RasterizeBundle;
use Symfony\Component\Process\InputStream;
use Symfony\Component\Stopwatch\Stopwatch;
class Rasterizer
{
protected ConfigHelper $configHelper;
protected ?Stopwatch $stopwatch;
public function __construct(ConfigHelper $configHelper, Stopwatch $stopwatch = null)
{
$this->configHelper = $configHelper;
$this->stopwatch = $stopwatch;
}
public function rasterize(string $html, array $arguments = [], array $env = [], callable $callback = null): string
{
if ($this->stopwatch instanceof Stopwatch) {
$this->stopwatch->start('rasterizer');
}
$input = new InputStream();
$process = $this->configHelper->buildProcess($input, $arguments, $env);
if ($callback) {
$callback($process);
}
$process->start(null, []);
$input->write($html);
$input->close();
$process->wait();
if ($this->stopwatch instanceof Stopwatch) {
$this->stopwatch->stop('rasterizer');
}
return $process->getOutput();
}
}