-
Notifications
You must be signed in to change notification settings - Fork 436
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
3770bea
commit c062001
Showing
30 changed files
with
1,006 additions
and
95 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,3 +1,4 @@ | ||
**/composer.lock | ||
**/vendor | ||
**cache** | ||
/.php-cs-fixer.cache | ||
/.phpunit.result.cache | ||
/composer.lock | ||
/vendor |
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
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
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
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 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
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,7 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KNPLabs\Snappy\Core; | ||
|
||
abstract class Exception extends \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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KNPLabs\Snappy\Core\Exception; | ||
|
||
use KNPLabs\Snappy\Core\Exception; | ||
|
||
final class DOMDocumentException extends 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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KNPLabs\Snappy\Core\Exception; | ||
|
||
use KNPLabs\Snappy\Core\Exception; | ||
|
||
final class FileNotFoundException extends 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,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
21
src/Core/Exception/FrontendUnsupportedBackendException.php
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\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, | ||
) | ||
); | ||
} | ||
} |
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,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.'); | ||
} | ||
} |
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 | ||
{ | ||
/** | ||
* @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()); | ||
} | ||
} |
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 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, | ||
); | ||
} | ||
} |
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 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, | ||
); | ||
} | ||
} |
Oops, something went wrong.