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 0772833
Show file tree
Hide file tree
Showing 22 changed files with 843 additions and 107 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/composer.lock
/vendor
/.phpunit.result.cache
1 change: 1 addition & 0 deletions .php-cs-fixer.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"php":"8.3.9","version":"3.61.1:v3.61.1#94a87189f55814e6cabca2d9a33b06de384a2ab8","indent":" ","lineEnding":"\n","rules":{"binary_operator_spaces":{"default":"at_least_single_space"},"blank_line_after_opening_tag":true,"blank_line_between_import_groups":true,"blank_lines_before_namespace":true,"braces_position":{"allow_single_line_empty_anonymous_classes":true},"class_definition":{"inline_constructor_arguments":false,"space_before_parenthesis":true},"compact_nullable_type_declaration":true,"declare_equal_normalize":true,"lowercase_cast":true,"lowercase_static_reference":true,"new_with_parentheses":true,"no_blank_lines_after_class_opening":true,"no_leading_import_slash":true,"no_whitespace_in_blank_line":true,"ordered_class_elements":{"order":["use_trait"]},"ordered_imports":{"imports_order":["class","function","const"],"sort_algorithm":"none"},"return_type_declaration":true,"short_scalar_cast":true,"single_import_per_statement":{"group_to_single_imports":false},"single_trait_insert_per_statement":true,"ternary_operator_spaces":true,"unary_operator_spaces":{"only_dec_inc":true},"visibility_required":true,"blank_line_after_namespace":true,"constant_case":true,"control_structure_braces":true,"control_structure_continuation_position":true,"elseif":true,"function_declaration":true,"indentation_type":true,"line_ending":true,"lowercase_keywords":true,"method_argument_space":{"attribute_placement":"ignore","on_multiline":"ensure_fully_multiline"},"no_break_comment":true,"no_closing_tag":true,"no_multiple_statements_per_line":true,"no_space_around_double_colon":true,"no_spaces_after_function_name":true,"no_trailing_whitespace":true,"no_trailing_whitespace_in_comment":true,"single_blank_line_at_eof":true,"single_class_element_per_statement":{"elements":["property"]},"single_line_after_imports":true,"spaces_inside_parentheses":true,"statement_indentation":true,"switch_case_semicolon_to_colon":true,"switch_case_space":true,"encoding":true,"full_opening_tag":true},"hashes":{"src\/Core\/Backend\/Adapter\/DOMDocumentToPdf.php":"231cf6ab2471850f90dad303afc15c9d","src\/Core\/Backend\/Adapter\/UriToPdf.php":"44bc7c5cb162fd3a46ada10d48a62442","src\/Core\/Backend\/Adapter\/HtmlFileToPdf.php":"c68577e12fa89bc13e79432dbc2d5bab","src\/Core\/Backend\/Adapter\/HtmlToPdf.php":"9ac7be66223788ff45aa41657eaecbe6","src\/Core\/Backend\/Adapter\/Reconfigurable.php":"c5e83191bac43220b54b7690a64a86f3","src\/Core\/Backend\/Adapter\/StreamToPdf.php":"cb2e83d74f2856c6d3f788bb4938da0b","src\/Core\/Backend\/Options.php":"1f469d1bb957efdd9f06ab876224a634","src\/Core\/Backend\/Options\/PageOrientation.php":"a593a9348d93147ce406c36dfe95d464","src\/Core\/Backend\/Adapter.php":"4577131301ee60675a4ed39986e7487d","src\/Core\/Backend\/Factory.php":"be067053558f5dda75e1f75c06d98bd7","src\/Core\/Frontend\/DOMDocumentToPdf.php":"f7b2406965f93730510421b5f565a063","src\/Core\/Frontend\/HtmlFileToPdf.php":"a05854c8ff083520ed9beb3d173eec23","src\/Core\/Frontend\/HtmlToPdf.php":"558bca4bf4c321eace74a237af74d809","src\/Core\/Frontend\/StreamToPdf.php":"1855b160a2d07a28ebd3f2a411a7affa","src\/Core\/Stream\/FileStream.php":"5374fac372d049e924a7fddb67426c0b","src\/Core\/Stream\/StreamWrapper.php":"0d0df135b32e90ee375835facab7c5c6","src\/Core\/Tests\/Frontend\/DOMDocumentToPdfTest.php":"ed1148f31772212f68f106284eba0ac7","src\/Core\/Tests\/Frontend\/HtmlFileToPdfTest.php":"d765646f47d1cadcaa5cad59fc44a322","src\/Core\/Tests\/Frontend\/HtmlToPdfTest.php":"48bba6256f54ef69359385da8d79371c","src\/Core\/Tests\/Frontend\/StreamToPdfTest.php":"6a7ee5a9a694ea54f0aaa63592235441","src\/Core\/Tests\/Stream\/FileStreamTest.php":"e761168ca9f96276b7e9bf24cb303ecc","src\/Core\/Filesystem\/SplResourceInfo.php":"fdd3387037df9359d97842aebe4ec42e"}}
1 change: 0 additions & 1 deletion .phpunit.result.cache

This file was deleted.

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();
}

}
64 changes: 64 additions & 0 deletions src/Core/Frontend/StreamToPdf.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\Filesystem\SplResourceInfo;
use KNPLabs\Snappy\Core\Stream\FileStream;
use Psr\Http\Message\StreamFactoryInterface;
use Psr\Http\Message\StreamInterface;
use SplFileInfo;

final class StreamToPdf implements Adapter\StreamToPdf
{
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 generateFromStream(StreamInterface $stream): StreamInterface
{
if ($this->adapter instanceof Adapter\StreamToPdf) {
return $this->adapter->generateFromStream($stream);
}

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

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

$input = $stream->detach();

if (null === $input) {
throw new \Exception('Unable to get resource from stream.');
}

stream_copy_to_stream($input, $file->resource);

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

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

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

throw new \Exception();
}
}
Loading

0 comments on commit 0772833

Please sign in to comment.