|
1 | 1 | <?php |
2 | 2 |
|
3 | | -declare(strict_types=1); |
| 3 | +declare(strict_types = 1); |
4 | 4 |
|
5 | 5 | namespace KNPLabs\Snappy\Backend\HeadlessChromium; |
6 | 6 |
|
7 | 7 | use KNPLabs\Snappy\Core\Backend\Adapter\UriToPdf; |
8 | | -use KNPLabs\Snappy\Core\Backend\Adapter\HtmlFileToPdf; |
9 | | -use KNPLabs\Snappy\Core\Backend\Adapter\HtmlToPdf; |
10 | 8 | use KNPLabs\Snappy\Core\Backend\Adapter\Reconfigurable; |
11 | 9 | use KNPLabs\Snappy\Core\Backend\Options; |
12 | 10 | use Psr\Http\Message\StreamFactoryInterface; |
13 | 11 | use Psr\Http\Message\StreamInterface; |
| 12 | +use Psr\Http\Message\UriFactoryInterface; |
14 | 13 | use Psr\Http\Message\UriInterface; |
15 | | -use SplFileInfo; |
16 | | -use Symfony\Component\Process\Exception\ProcessFailedException; |
17 | 14 | use Symfony\Component\Process\Process; |
| 15 | +use InvalidArgumentException; |
| 16 | +use RuntimeException; |
18 | 17 |
|
19 | | -final class HeadlessChromiumAdapter implements UriToPdf, HtmlFileToPdf, HtmlToPdf |
| 18 | +final class HeadlessChromiumAdapter implements UriToPdf |
20 | 19 | { |
21 | | - private string $tempDir; |
22 | | - |
23 | 20 | /** |
24 | 21 | * @use Reconfigurable<self> |
25 | 22 | */ |
26 | 23 | use Reconfigurable; |
27 | 24 |
|
| 25 | + private string $tempDir; |
| 26 | + |
28 | 27 | public function __construct( |
29 | | - private Options $options, |
30 | | - private StreamFactoryInterface $streamFactory |
31 | | - ) {} |
| 28 | + private string $binary, |
| 29 | + private int $timeout, |
| 30 | + HeadlessChromiumFactory $factory, |
| 31 | + Options $options, |
| 32 | + private readonly StreamFactoryInterface $streamFactory, |
| 33 | + private readonly UriFactoryInterface $uriFactory, |
| 34 | + ) { |
| 35 | + $this->tempDir = __DIR__; |
| 36 | + self::validateOptions($options); |
| 37 | + |
| 38 | + $this->factory = $factory; |
| 39 | + $this->options = $options; |
| 40 | + } |
32 | 41 |
|
33 | 42 | public function generateFromUri(UriInterface $url): StreamInterface |
34 | 43 | { |
35 | | - $this->tempDir = sys_get_temp_dir(); |
| 44 | + $process = new Process( |
| 45 | + command: [ |
| 46 | + $this->binary, |
| 47 | + ...$this->compileOptions(), |
| 48 | + (string) $url, |
| 49 | + ], |
| 50 | + timeout: $this->timeout |
| 51 | + ); |
36 | 52 |
|
37 | | - $command = $this->buildChromiumCommand((string) $url, $this->tempDir); |
38 | | - $this->runProcess($command); |
| 53 | + $process->run(); |
39 | 54 |
|
40 | | - return $this->createStreamFromFile($this->tempDir); |
| 55 | + return $this->streamFactory->createStream($this->getPrintToPdfFilePath()); |
41 | 56 | } |
42 | 57 |
|
43 | | - public function generateFromHtmlFile(SplFileInfo $file): StreamInterface |
| 58 | + public function getPrintToPdfFilePath(): string |
44 | 59 | { |
45 | | - $htmlContent = file_get_contents($file->getPathname()); |
46 | | - return $this->generateFromHtml($htmlContent); |
47 | | - } |
| 60 | + $printToPdfOption = \array_filter( |
| 61 | + $this->options->extraOptions, |
| 62 | + fn ($option) => $option instanceof ExtraOption\PrintToPdf |
| 63 | + ); |
48 | 64 |
|
49 | | - public function generateFromHtml(string $html): StreamInterface |
50 | | - { |
51 | | - $outputFile = $this->tempDir . '/pdf_output_'; |
52 | | - $htmlFile = $this->tempDir . '/html_input_'; |
53 | | - file_put_contents($htmlFile, $html); |
| 65 | + if (!empty($printToPdfOption)) { |
| 66 | + $printToPdfOption = \array_values($printToPdfOption)[0]; |
54 | 67 |
|
55 | | - $command = $this->buildChromiumCommand("file://$htmlFile", $outputFile); |
56 | | - $this->runProcess($command); |
| 68 | + return $printToPdfOption->getFile()->getPathname(); |
| 69 | + } |
57 | 70 |
|
58 | | - unlink($htmlFile); |
59 | | - return $this->createStreamFromFile($outputFile); |
| 71 | + throw new RuntimeException('Missing option print to pdf.'); |
60 | 72 | } |
61 | 73 |
|
62 | | - /** |
63 | | - * @return array<string> |
64 | | - */ |
65 | | - private function buildChromiumCommand(string $inputUri, string $outputPath): array |
| 74 | + private static function validateOptions(Options $options): void |
66 | 75 | { |
67 | | - $options = $this->compileConstructOptions(); |
68 | | - |
69 | | - return array_merge([ |
70 | | - 'chromium', |
71 | | - '--headless', |
72 | | - '--disable-gpu', |
73 | | - '--no-sandbox', |
74 | | - '--print-to-pdf=' . $outputPath, |
75 | | - ], $options, [$inputUri]); |
76 | | - } |
| 76 | + $optionTypes = []; |
77 | 77 |
|
78 | | - /** |
79 | | - * @return array<string> |
80 | | - */ |
81 | | - private function compileConstructOptions(): array |
82 | | - { |
83 | | - $constructOptions = $this->options->extraOptions['construct'] ?? []; |
84 | | - |
85 | | - $compiledOptions = []; |
86 | | - if (is_array($constructOptions)) { |
87 | | - foreach ($constructOptions as $key => $value) { |
88 | | - $compiledOptions[] = "--$key=$value"; |
| 78 | + foreach ($options->extraOptions as $option) { |
| 79 | + if (!$option instanceof ExtraOption) { |
| 80 | + throw new InvalidArgumentException(\sprintf('Invalid option type provided. Expected "%s", received "%s".', ExtraOption::class, \gettype($option) === 'object' ? \get_class($option) : \gettype($option), )); |
89 | 81 | } |
90 | | - } |
91 | | - |
92 | | - return $compiledOptions; |
93 | | - } |
94 | 82 |
|
95 | | - private function runProcess(array $command): void |
96 | | - { |
97 | | - $process = new Process($command); |
98 | | - $process->run(); |
| 83 | + if (\in_array($option::class, $optionTypes, true) && !$option->isRepeatable()) { |
| 84 | + throw new InvalidArgumentException(\sprintf('Duplicate option type provided: "%s".', $option::class, )); |
| 85 | + } |
99 | 86 |
|
100 | | - if (!$process->isSuccessful()) { |
101 | | - throw new ProcessFailedException($process); |
| 87 | + $optionTypes[] = $option::class; |
102 | 88 | } |
103 | 89 | } |
104 | 90 |
|
105 | | - private function createStreamFromFile(string $filePath): StreamInterface |
| 91 | + /** |
| 92 | + * @return array<float|int|string> |
| 93 | + */ |
| 94 | + private function compileOptions(): array |
106 | 95 | { |
107 | | - $output = file_get_contents($filePath); |
108 | | - unlink($filePath); |
109 | | - |
110 | | - return $this->streamFactory->createStream($output ?: ''); |
| 96 | + return \array_reduce( |
| 97 | + $this->options->extraOptions, |
| 98 | + fn (array $carry, ExtraOption $extraOption) => $this->options->pageOrientation !== null |
| 99 | + ?: [ |
| 100 | + ...$carry, |
| 101 | + ...$extraOption->compile(), |
| 102 | + ], |
| 103 | + [], |
| 104 | + ); |
111 | 105 | } |
112 | 106 | } |
0 commit comments