-
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.
Merge pull request #501 from KnpLabs/proto/v2-dompdf
- Loading branch information
Showing
49 changed files
with
1,496 additions
and
468 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 |
---|---|---|
@@ -0,0 +1,65 @@ | ||
name: Tests | ||
|
||
on: | ||
pull_request: ~ | ||
push: ~ | ||
|
||
jobs: | ||
packages: | ||
runs-on: ubuntu-20.04 | ||
outputs: | ||
packages: ${{ steps.script.outputs.result }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
- uses: actions/github-script@v7 | ||
id: script | ||
with: | ||
script: | | ||
const fs = require('fs'); | ||
const composer = JSON.parse( | ||
fs.readFileSync('./composer.json') | ||
); | ||
const packages = Object.keys(composer.autoload['psr-4']).map( | ||
(namespace) => { | ||
const path = composer.autoload['psr-4'][namespace]; | ||
const name = JSON.parse(fs.readFileSync(path + 'composer.json')).name; | ||
return { | ||
name: name, | ||
path: './' + path, | ||
}; | ||
} | ||
); | ||
console.log(packages); | ||
return packages; | ||
package: | ||
runs-on: ubuntu-20.04 | ||
needs: packages | ||
strategy: | ||
matrix: | ||
name: | ||
- knplabs/snappy | ||
path: | ||
- ./ | ||
# include: ${{ fromJson(needs.packages.outputs.packages) }} | ||
defaults: | ||
run: | ||
working-directory: ${{ matrix.path }} | ||
steps: | ||
- uses: actions/checkout@v4 | ||
with: | ||
sparse-checkout: | | ||
src/Core/ | ||
${{ matrix.path }} | ||
- uses: shivammathur/setup-php@v2 | ||
- name: composer install | ||
run: | | ||
composer install | ||
- name: vendor/bin/phpunit | ||
run: | | ||
vendor/bin/phpunit |
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 @@ | ||
{"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}} |
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,56 @@ | ||
<?php | ||
|
||
$global = json_decode( | ||
(string) file_get_contents(__DIR__ . '/../composer.json'), | ||
true, | ||
flags: JSON_THROW_ON_ERROR, | ||
); | ||
|
||
$dependencies = [ | ||
...$global['require'], | ||
...$global['require-dev'], | ||
]; | ||
|
||
$replace = $global['replace']; | ||
|
||
foreach($global['autoload']['psr-4'] as $path) { | ||
$content = file_get_contents($path . '/composer.json'); | ||
|
||
if (false === $content) { | ||
throw new \Exception("File $path/composer.json not found."); | ||
} | ||
|
||
$json = json_decode( | ||
$content, | ||
true, | ||
flags: JSON_THROW_ON_ERROR, | ||
); | ||
|
||
foreach (['require', 'require-dev'] as $part) { | ||
foreach ($json[$part] ?? [] as $name => $constraint) { | ||
if (isset($replace[$name])) { | ||
continue; | ||
} | ||
|
||
if (false === isset($dependencies[$name])) { | ||
throw new \Exception( | ||
sprintf( | ||
'Dependency "%s" not found in %s/composer.json.', | ||
$name, | ||
__DIR__, | ||
) | ||
); | ||
} | ||
|
||
$json[$part][$name] = $dependencies[$name]; | ||
} | ||
} | ||
|
||
$content = file_put_contents( | ||
$path . '/composer.json', | ||
json_encode( | ||
$json, | ||
flags: JSON_PRETTY_PRINT|JSON_THROW_ON_ERROR|JSON_UNESCAPED_SLASHES, | ||
), | ||
); | ||
} |
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,4 @@ | ||
parameters: | ||
level: max | ||
paths: | ||
- src |
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,29 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/9.3/phpunit.xsd" | ||
backupGlobals="false" | ||
colors="true" | ||
bootstrap="vendor/autoload.php" | ||
failOnRisky="true" | ||
failOnWarning="true" | ||
> | ||
<php> | ||
<ini name="error_reporting" value="-1" /> | ||
</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> | ||
</testsuite> | ||
<testsuite name="Snappy Symfony framework integration"> | ||
<directory>./src/Framework/Symfony/</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |
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,104 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KNPLabs\Snappy\Backend\Dompdf; | ||
|
||
use ArrayAccess; | ||
use DOMDocument; | ||
use Dompdf; | ||
use KNPLabs\Snappy\Core\Backend\Adapter\DOMDocumentToPdf; | ||
use KNPLabs\Snappy\Core\Backend\Adapter\HtmlFileToPdf; | ||
use KNPLabs\Snappy\Core\Backend\Adapter\HtmlToPdf; | ||
use KNPLabs\Snappy\Core\Backend\Adapter\Reconfigurable; | ||
use KNPLabs\Snappy\Core\Backend\Options; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
use Psr\Http\Message\StreamInterface; | ||
use SplFileInfo; | ||
|
||
final readonly class DompdfAdapter implements DOMDocumentToPdf, HtmlFileToPdf, HtmlToPdf | ||
{ | ||
/** | ||
* @use Reconfigurable<self> | ||
*/ | ||
use Reconfigurable; | ||
|
||
public function __construct( | ||
DompdfFactory $factory, | ||
Options $options, | ||
private readonly StreamFactoryInterface $streamFactory | ||
) { | ||
$this->factory = $factory; | ||
$this->options = $options; | ||
} | ||
|
||
public function generateFromDOMDocument(DOMDocument $DOMDocument): StreamInterface | ||
{ | ||
$dompdf = $this->buildDompdf(); | ||
$dompdf->loadDOM($DOMDocument); | ||
|
||
return $this->createStream($dompdf); | ||
} | ||
|
||
public function generateFromHtmlFile(SplFileInfo $file): StreamInterface | ||
{ | ||
$dompdf = $this->buildDompdf(); | ||
$dompdf->loadHtmlFile($file->getPath()); | ||
|
||
return $this->createStream($dompdf); | ||
} | ||
|
||
public function generateFromHtml(string $html): StreamInterface | ||
{ | ||
$dompdf = $this->buildDompdf(); | ||
$dompdf->loadHtml($html); | ||
|
||
return $this->createStream($dompdf); | ||
} | ||
|
||
private function buildDompdf(): Dompdf\Dompdf | ||
{ | ||
return new Dompdf\Dompdf( $this->compileConstructOptions()); | ||
} | ||
|
||
private function compileConstructOptions(): Dompdf\Options | ||
{ | ||
$options = new Dompdf\Options( | ||
is_array($this->options->extraOptions['construct']) | ||
? $this->options->extraOptions['construct'] | ||
: null | ||
); | ||
|
||
if (null !== $this->options->pageOrientation) { | ||
$options->setDefaultPaperOrientation( | ||
$this->options->pageOrientation->value | ||
); | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
/** | ||
* @return array<mixed, mixed> | ||
*/ | ||
private function compileOutputOptions(): array | ||
{ | ||
$options = $this->options->extraOptions['output']; | ||
|
||
if (false === is_array($options)) { | ||
$options = []; | ||
} | ||
|
||
return $options; | ||
} | ||
|
||
private function createStream(Dompdf\Dompdf $dompdf): StreamInterface | ||
{ | ||
$output = $dompdf->output($this->compileOutputOptions()); | ||
|
||
return $this | ||
->streamFactory | ||
->createStream($output ?: '') | ||
; | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace KNPLabs\Snappy\Backend\Dompdf; | ||
|
||
use KNPLabs\Snappy\Core\Backend\Adapter; | ||
use KNPLabs\Snappy\Core\Backend\Factory; | ||
use KNPLabs\Snappy\Core\Backend\Option; | ||
use KNPLabs\Snappy\Core\Backend\Options; | ||
use Psr\Http\Message\StreamFactoryInterface; | ||
|
||
/** | ||
* @implements Factory<DompdfAdapter> | ||
*/ | ||
final readonly class DompdfFactory implements Factory | ||
{ | ||
public function __construct(private readonly StreamFactoryInterface $streamFactory) | ||
{ | ||
} | ||
|
||
public function create(Options $options): DompdfAdapter | ||
{ | ||
return new DompdfAdapter( | ||
factory: $this, | ||
options: $options, | ||
streamFactory: $this->streamFactory, | ||
); | ||
} | ||
} |
Oops, something went wrong.