-
Notifications
You must be signed in to change notification settings - Fork 438
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): implement frontends for all backends
- Loading branch information
1 parent
3ed9af8
commit 4094a45
Showing
15 changed files
with
465 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":1,"defects":{"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testLoadEmptyConfiguration":8,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigure":5,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigureTmpDirectory":7,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testDompdfBackendConfiguration":8,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileIsAutomaticalyRemoved":8},"times":{"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testLoadEmptyConfiguration":0.008,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigure":0,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigureTmpDirectory":0,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testDompdfBackendConfiguration":0.015,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileStream":0.005,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileStreamCreateTemporaryFile":0.003,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileStreamReadTheFile":0.004,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileIsAutomaticalyRemoved":0.003}} | ||
{"version":1,"defects":{"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testLoadEmptyConfiguration":8,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigure":5,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigureTmpDirectory":7,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testDompdfBackendConfiguration":8,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileIsAutomaticalyRemoved":8,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithHtmlToPdf":5,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithHtmlFileToPdf":5,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithStreamToPdf":5},"times":{"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testLoadEmptyConfiguration":0.016,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigure":0,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testConfigureTmpDirectory":0,"KNPLabs\\Snappy\\Framework\\Symfony\\Tests\\DependencyInjection\\SnappyExtensionTest::testDompdfBackendConfiguration":0.012,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileStream":0.005,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileStreamCreateTemporaryFile":0.002,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileStreamReadTheFile":0.002,"KNPLabs\\Snappy\\Core\\Tests\\Stream\\FileStreamTest::testTmpFileIsAutomaticalyRemoved":0.002,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithDOMDocumentToPdf":0.009,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithHtmlToPdf":0.004,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithHtmlFileToPdf":0.005,"KNPLabs\\Snappy\\Core\\Tests\\Frontend\\DOMDocumentToPdfTest::testWithStreamToPdf":0.006}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 $DOMDocument): StreamInterface | ||
{ | ||
if ($this->adapter instanceof Adapter\DOMDocumentToPdf) { | ||
return $this->adapter->generateFromDOMDocument($DOMDocument); | ||
} | ||
|
||
$html = $DOMDocument->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): self | ||
{ | ||
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) { | ||
$DOMDocument = new \DOMDocument; | ||
$DOMDocument->loadHTMLFile($file->getPathname()); | ||
|
||
return $this->adapter->generateFromDOMDocument($DOMDocument); | ||
} | ||
|
||
throw new \Exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): self | ||
{ | ||
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) { | ||
$DOMDocument = new \DOMDocument; | ||
$DOMDocument->loadHTML($html); | ||
|
||
return $this->adapter->generateFromDOMDocument($DOMDocument); | ||
} | ||
|
||
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; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): self | ||
{ | ||
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) { | ||
$DOMDocument = new \DOMDocument; | ||
$DOMDocument->loadHTML((string) $stream); | ||
|
||
return $this->adapter->generateFromDOMDocument($DOMDocument); | ||
} | ||
|
||
throw new \Exception; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.