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 Nov 12, 2024
1 parent 3770bea commit c062001
Show file tree
Hide file tree
Showing 30 changed files with 1,006 additions and 95 deletions.
7 changes: 4 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
**/composer.lock
**/vendor
**cache**
/.php-cs-fixer.cache
/.phpunit.result.cache
/composer.lock
/vendor
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 @@ -26,10 +26,10 @@ public function __construct(DompdfFactory $factory, Options $options, private St
$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
2 changes: 1 addition & 1 deletion src/Core/Backend/Adapter/DOMDocumentToPdf.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

interface DOMDocumentToPdf extends Adapter
{
public function generateFromDOMDocument(\DOMDocument $DOMDocument): StreamInterface;
public function generateFromDOMDocument(\DOMDocument $document): StreamInterface;
}
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;
}
5 changes: 4 additions & 1 deletion src/Core/Backend/Options.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ final class Options
/**
* @param array<mixed> $extraOptions
*/
public function __construct(public readonly ?PageOrientation $pageOrientation, public readonly array $extraOptions) {}
public function __construct(
public readonly ?PageOrientation $pageOrientation,
public readonly array $extraOptions
) {}

public static function create(): self
{
Expand Down
31 changes: 0 additions & 31 deletions src/Core/Bridge/FromHtmlFileToHtmlToPdf.php

This file was deleted.

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

This file was deleted.

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

declare(strict_types=1);

namespace KNPLabs\Snappy\Core;

abstract class Exception extends \Exception {}
9 changes: 9 additions & 0 deletions src/Core/Exception/DOMDocumentException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Exception;

use KNPLabs\Snappy\Core\Exception;

final class DOMDocumentException extends Exception {}
9 changes: 9 additions & 0 deletions src/Core/Exception/FileNotFoundException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Exception;

use KNPLabs\Snappy\Core\Exception;

final class FileNotFoundException extends Exception {}
17 changes: 17 additions & 0 deletions src/Core/Exception/FileReadException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Exception;

use KNPLabs\Snappy\Core\Exception;

final class FileReadException extends Exception
{
public function __construct(\SplFileInfo $file)
{
file_exists($file->getPathname())
? parent::__construct("File {$file->getPathname()} can't be read.")
: parent::__construct("File {$file->getPathname()} not found.");
}
}
21 changes: 21 additions & 0 deletions src/Core/Exception/FrontendUnsupportedBackendException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Exception;

use KNPLabs\Snappy\Core\Exception;

final class FrontendUnsupportedBackendException extends Exception
{
public function __construct(string $frontendClass, string $backendClass)
{
parent::__construct(
\sprintf(
'Snappy frontend "%s" does not support backend "%s"',
$frontendClass,
$backendClass,
)
);
}
}
15 changes: 15 additions & 0 deletions src/Core/Exception/StreamDetachedException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace KNPLabs\Snappy\Core\Exception;

use KNPLabs\Snappy\Core\Exception;

final class StreamDetachedException extends Exception
{
public function __construct()
{
parent::__construct('Stream is detached.');
}
}
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
{
/**
* @param resource $resource
*/
public function __construct(public readonly mixed $resource)
{
parent::__construct(stream_get_meta_data($this->resource)['uri']);
}

public static function fromTmpFile(): self
{
return new self(tmpfile());
}
}
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 KNPLabs\Snappy\Core\Backend\Adapter;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Exception\DOMDocumentException;
use KNPLabs\Snappy\Core\Exception\FrontendUnsupportedBackendException;
use KNPLabs\Snappy\Core\Filesystem\SplResourceInfo;
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(callable|Options $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 DOMDocumentException('Unable to read HTML from DOMDocument.');
}

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 FrontendUnsupportedBackendException(
self::class,
$this->adapter::class,
);
}
}
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 KNPLabs\Snappy\Core\Backend\Adapter;
use KNPLabs\Snappy\Core\Backend\Options;
use KNPLabs\Snappy\Core\Exception\FileReadException;
use KNPLabs\Snappy\Core\Exception\FrontendUnsupportedBackendException;
use KNPLabs\Snappy\Core\Stream\FileStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;

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

public function withOptions(callable|Options $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 FileReadException($file);
}

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 FrontendUnsupportedBackendException(
self::class,
$this->adapter::class,
);
}
}
Loading

0 comments on commit c062001

Please sign in to comment.