Skip to content

Commit

Permalink
feat(core): implement frontends for all backends
Browse files Browse the repository at this point in the history
  • Loading branch information
PedroTroller committed Oct 16, 2024
1 parent 3ed9af8 commit ae88dfa
Show file tree
Hide file tree
Showing 23 changed files with 884 additions and 114 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
/vendor
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

10 changes: 2 additions & 8 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,11 @@
</php>

<testsuites>
<testsuite name="Snappy Dompdf backend">
<directory>./src/Backend/Dompdf</directory>
</testsuite>
<testsuite name="Snappy WkHtmlToPdf backend">
<directory>./src/Backend/WkHtmlToPdf</directory>
</testsuite>
<testsuite name="Snappy core">
<directory>./src/Core/</directory>
<directory>./src/Core/Tests/</directory>
</testsuite>
<testsuite name="Snappy Symfony framework integration">
<directory>./src/Framework/Symfony/</directory>
<directory>./src/Framework/Symfony/Tests/</directory>
</testsuite>
</testsuites>
</phpunit>
4 changes: 2 additions & 2 deletions src/Backend/Dompdf/DompdfAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,10 @@ public function __construct(
$this->options = $options;
}

public function generateFromDOMDocument(DOMDocument $DOMDocument): StreamInterface
public function generateFromDOMDocument(DOMDocument $document): StreamInterface
{
$dompdf = $this->buildDompdf();
$dompdf->loadDOM($DOMDocument);
$dompdf->loadDOM($document);

return $this->createStream($dompdf);
}
Expand Down
13 changes: 13 additions & 0 deletions src/Core/Backend/Adapter/StreamToPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Backend\Adapter;

use KNPLabs\Snappy\Core\Backend\Adapter;
use Psr\Http\Message\StreamInterface;

interface StreamToPdf extends Adapter
{
public function generateFromStream(StreamInterface $stream): StreamInterface;
}
36 changes: 0 additions & 36 deletions src/Core/Bridge/FromHtmlFileToHtmlToPdf.php

This file was deleted.

39 changes: 0 additions & 39 deletions src/Core/Bridge/FromHtmlToHtmlFileToPdf.php

This file was deleted.

21 changes: 21 additions & 0 deletions src/Core/Filesystem/SplResourceInfo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Filesystem;

final class SplResourceInfo extends \SplFileInfo
{
public static function fromTmpFile(): self
{
return new self(tmpfile());
}

/**
* @param resource $resource
*/
public function __construct(public readonly mixed $resource)
{
parent::__construct(stream_get_meta_data($this->resource)['uri']);
}
}
62 changes: 62 additions & 0 deletions src/Core/Frontend/DOMDocumentToPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Frontend;

use DOMDocument;
use KNPLabs\Snappy\Core\Backend\Adapter;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Filesystem\SplFileInfo;
use KNPLabs\Snappy\Core\Filesystem\SplResourceInfo;
use KNPLabs\Snappy\Core\Stream\FileStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

final class DOMDocumentToPdf implements Adapter\DOMDocumentToPdf
{
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
{
}

public function withOptions(Options|callable $options): static
{
return new self(
$this->adapter->withOptions($options),
$this->streamFactory,
);
}

public function generateFromDOMDocument(DOMDocument $document): StreamInterface
{
if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
return $this->adapter->generateFromDOMDocument($document);
}

$html = $document->saveHTML();

if (false === $html) {
throw new \Exception();
}

if ($this->adapter instanceof Adapter\HtmlToPdf) {
return $this->adapter->generateFromHtml($html);
}

if ($this->adapter instanceof Adapter\StreamToPdf) {
return $this->adapter->generateFromStream(
$this->streamFactory->createStream($html)
);
}

if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
$file = SplResourceInfo::fromTmpFile();

fwrite($file->resource, $html);

return $this->adapter->generateFromHtmlFile($file);
}

throw new \Exception();
}
}
64 changes: 64 additions & 0 deletions src/Core/Frontend/HtmlFileToPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Frontend;

use DOMDocument;
use KNPLabs\Snappy\Core\Backend\Adapter;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Stream\FileStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use SplFileInfo;

final class HtmlFileToPdf implements Adapter\HtmlFileToPdf
{
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
{

}

public function withOptions(Options|callable $options): static
{
return new self(
$this->adapter->withOptions($options),
$this->streamFactory
);
}

public function generateFromHtmlFile(SplFileInfo $file): StreamInterface
{
if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
return $this->adapter->generateFromHtmlFile($file);
}

if ($this->adapter instanceof Adapter\StreamToPdf) {
return $this->adapter->generateFromStream(
new FileStream(
$file,
$this->streamFactory->createStreamFromFile($file->getPathname()),
),
);
}

if ($this->adapter instanceof Adapter\HtmlToPdf) {
$html = file_get_contents($file->getPathname());

if (false === $html) {
throw new \Exception("Unable to read content of {$file->getPathname()}.");
}

return $this->adapter->generateFromHtml($html);
}

if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
$document = new \DOMDocument();
$document->loadHTMLFile($file->getPathname());

return $this->adapter->generateFromDOMDocument($document);
}

throw new \Exception();
}
}
60 changes: 60 additions & 0 deletions src/Core/Frontend/HtmlToPdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Frontend;

use DOMDocument;
use KNPLabs\Snappy\Core\Backend\Adapter;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Filesystem\SplResourceInfo;
use KNPLabs\Snappy\Core\Stream\FileStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

final class HtmlToPdf implements Adapter\HtmlToPdf
{
public function __construct(private readonly Adapter $adapter, private readonly StreamFactoryInterface $streamFactory)
{

}

public function withOptions(Options|callable $options): static
{
return new self(
$this->adapter->withOptions($options),
$this->streamFactory
);
}

public function generateFromHtml(string $html): StreamInterface
{
if ($this->adapter instanceof Adapter\HtmlToPdf) {
return $this->adapter->generateFromHtml($html);
}

if ($this->adapter instanceof Adapter\DOMDocumentToPdf) {
$document = new \DOMDocument();
$document->loadHTML($html);

return $this->adapter->generateFromDOMDocument($document);
}

if ($this->adapter instanceof Adapter\StreamToPdf) {
return $this->adapter->generateFromStream(
$this->streamFactory->createStream($html),
);
}

if ($this->adapter instanceof Adapter\HtmlFileToPdf) {
$file = SplResourceInfo::fromTmpFile();

fwrite($file->resource, $html);

return $this->adapter->generateFromHtmlFile($file);
}

throw new \Exception();
}

}
Loading

0 comments on commit ae88dfa

Please sign in to comment.