Skip to content

Commit a5ac2c0

Browse files
committed
feat(core): implement frontends for all backends
1 parent 3ed9af8 commit a5ac2c0

19 files changed

+836
-102
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
/composer.lock
22
/vendor
3+
/.phpunit.result.cache

.phpunit.result.cache

Lines changed: 0 additions & 1 deletion
This file was deleted.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Core\Backend\Adapter;
6+
7+
use KNPLabs\Snappy\Core\Backend\Adapter;
8+
use Psr\Http\Message\StreamInterface;
9+
10+
interface StreamToPdf extends Adapter
11+
{
12+
public function generateFromStream(StreamInterface $stream): StreamInterface;
13+
}

src/Core/Bridge/FromHtmlFileToHtmlToPdf.php

Lines changed: 0 additions & 36 deletions
This file was deleted.

src/Core/Bridge/FromHtmlToHtmlFileToPdf.php

Lines changed: 0 additions & 39 deletions
This file was deleted.
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Core\Filesystem;
6+
7+
final class SplResourceInfo extends \SplFileInfo
8+
{
9+
public static function fromTmpFile(): self
10+
{
11+
return new self(tmpfile());
12+
}
13+
14+
/**
15+
* @param resource $resource
16+
*/
17+
public function __construct(public readonly mixed $resource)
18+
{
19+
parent::__construct(stream_get_meta_data($this->resource)['uri']);
20+
}
21+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Core\Frontend;
6+
7+
use DOMDocument;
8+
use KNPLabs\Snappy\Core\Backend\Adapter;
9+
use KNPLabs\Snappy\Core\Backend\Options;
10+
use KNPLabs\Snappy\Core\Filesystem\SplFileInfo;
11+
use KNPLabs\Snappy\Core\Filesystem\SplResourceInfo;
12+
use KNPLabs\Snappy\Core\Stream\FileStream;
13+
use Psr\Http\Message\StreamFactoryInterface;
14+
use Psr\Http\Message\StreamInterface;
15+
16+
final class DOMDocumentToPdf implements Adapter\DOMDocumentToPdf
17+
{
18+
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
19+
{
20+
}
21+
22+
public function withOptions(Options|callable $options): static
23+
{
24+
return new self(
25+
$this->adapter->withOptions($options),
26+
$this->streamFactory,
27+
);
28+
}
29+
30+
public function generateFromDOMDocument(DOMDocument $document): StreamInterface
31+
{
32+
if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
33+
return $this->adapter->generateFromDOMDocument($document);
34+
}
35+
36+
$html = $document->saveHTML();
37+
38+
if (false === $html) {
39+
throw new \Exception;
40+
}
41+
42+
if ($this->adapter instanceof Adapter\HtmlToPdf) {
43+
return $this->adapter->generateFromHtml($html);
44+
}
45+
46+
if ($this->adapter instanceof Adapter\StreamToPdf) {
47+
return $this->adapter->generateFromStream(
48+
$this->streamFactory->createStream($html)
49+
);
50+
}
51+
52+
if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
53+
$file = SplResourceInfo::fromTmpFile();
54+
55+
fwrite($file->resource, $html);
56+
57+
return $this->adapter->generateFromHtmlFile($file);
58+
}
59+
60+
throw new \Exception;
61+
}
62+
}

src/Core/Frontend/HtmlFileToPdf.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Core\Frontend;
6+
7+
use DOMDocument;
8+
use KNPLabs\Snappy\Core\Backend\Adapter;
9+
use KNPLabs\Snappy\Core\Backend\Options;
10+
use KNPLabs\Snappy\Core\Stream\FileStream;
11+
use Psr\Http\Message\StreamFactoryInterface;
12+
use Psr\Http\Message\StreamInterface;
13+
use SplFileInfo;
14+
15+
final class HtmlFileToPdf implements Adapter\HtmlFileToPdf
16+
{
17+
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
18+
{
19+
20+
}
21+
22+
public function withOptions(Options|callable $options): static
23+
{
24+
return new self(
25+
$this->adapter->withOptions($options),
26+
$this->streamFactory
27+
);
28+
}
29+
30+
public function generateFromHtmlFile(SplFileInfo $file): StreamInterface
31+
{
32+
if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
33+
return $this->adapter->generateFromHtmlFile($file);
34+
}
35+
36+
if ($this->adapter instanceof Adapter\StreamToPdf) {
37+
return $this->adapter->generateFromStream(
38+
new FileStream(
39+
$file,
40+
$this->streamFactory->createStreamFromFile($file->getPathname()),
41+
),
42+
);
43+
}
44+
45+
if ($this->adapter instanceof Adapter\HtmlToPdf) {
46+
$html = file_get_contents($file->getPathname());
47+
48+
if (false === $html) {
49+
throw new \Exception("Unable to read content of {$file->getPathname()}.");
50+
}
51+
52+
return $this->adapter->generateFromHtml($html);
53+
}
54+
55+
if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
56+
$document = new \DOMDocument;
57+
$document->loadHTMLFile($file->getPathname());
58+
59+
return $this->adapter->generateFromDOMDocument($document);
60+
}
61+
62+
throw new \Exception;
63+
}
64+
}

src/Core/Frontend/HtmlToPdf.php

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Core\Frontend;
6+
7+
use DOMDocument;
8+
use KNPLabs\Snappy\Core\Backend\Adapter;
9+
use KNPLabs\Snappy\Core\Backend\Options;
10+
use KNPLabs\Snappy\Core\Filesystem\SplResourceInfo;
11+
use KNPLabs\Snappy\Core\Stream\FileStream;
12+
use Psr\Http\Message\StreamFactoryInterface;
13+
use Psr\Http\Message\StreamInterface;
14+
15+
final class HtmlToPdf implements Adapter\HtmlToPdf
16+
{
17+
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
18+
{
19+
20+
}
21+
22+
public function withOptions(Options|callable $options): static
23+
{
24+
return new self(
25+
$this->adapter->withOptions($options),
26+
$this->streamFactory
27+
);
28+
}
29+
30+
public function generateFromHtml(string $html): StreamInterface
31+
{
32+
if ($this->adapter instanceof Adapter\HtmlToPdf) {
33+
return $this->adapter->generateFromHtml($html);
34+
}
35+
36+
if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
37+
$document = new \DOMDocument;
38+
$document->loadHTML($html);
39+
40+
return $this->adapter->generateFromDOMDocument($document);
41+
}
42+
43+
if ($this->adapter instanceof Adapter\StreamToPdf) {
44+
return $this->adapter->generateFromStream(
45+
$this->streamFactory->createStream($html),
46+
);
47+
}
48+
49+
if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
50+
$file = SplResourceInfo::fromTmpFile();
51+
52+
fwrite($file->resource, $html);
53+
54+
return $this->adapter->generateFromHtmlFile($file);
55+
}
56+
57+
throw new \Exception;
58+
}
59+
60+
}

src/Core/Frontend/StreamToPdf.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace KNPLabs\Snappy\Core\Frontend;
6+
7+
use DOMDocument;
8+
use KNPLabs\Snappy\Core\Backend\Adapter;
9+
use KNPLabs\Snappy\Core\Backend\Options;
10+
use KNPLabs\Snappy\Core\Filesystem\SplResourceInfo;
11+
use KNPLabs\Snappy\Core\Stream\FileStream;
12+
use Psr\Http\Message\StreamFactoryInterface;
13+
use Psr\Http\Message\StreamInterface;
14+
use SplFileInfo;
15+
16+
final class StreamToPdf implements Adapter\StreamToPdf
17+
{
18+
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
19+
{
20+
21+
}
22+
23+
public function withOptions(Options|callable $options): static
24+
{
25+
return new self(
26+
$this->adapter->withOptions($options),
27+
$this->streamFactory
28+
);
29+
}
30+
31+
public function generateFromStream(StreamInterface $stream): StreamInterface
32+
{
33+
if ($this->adapter instanceof Adapter\StreamToPdf) {
34+
return $this->adapter->generateFromStream($stream);
35+
}
36+
37+
if ($this->adapter instanceof Adapter\HtmlToPdf) {
38+
return $this->adapter->generateFromHtml((string) $stream);
39+
}
40+
41+
if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
42+
$file = SplResourceInfo::fromTmpFile();
43+
44+
$input = $stream->detach();
45+
46+
if (null === $input) {
47+
throw new \Exception('Unable to get resource from stream.');
48+
}
49+
50+
stream_copy_to_stream($input, $file->resource);
51+
52+
return $this->adapter->generateFromHtmlFile($file);
53+
}
54+
55+
if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
56+
$document = new \DOMDocument;
57+
$document->loadHTML((string) $stream);
58+
59+
return $this->adapter->generateFromDOMDocument($document);
60+
}
61+
62+
throw new \Exception;
63+
}
64+
}

0 commit comments

Comments
 (0)