From ba9ad2f73021061326e40b157c5031d9b1c31b2e Mon Sep 17 00:00:00 2001 From: Reyo Stallenberg Date: Mon, 2 Jul 2018 14:19:45 +0200 Subject: [PATCH 1/2] Dispatch events in DompdfWrapper --- README.md | 6 ++++++ src/Events.php | 18 ++++++++++++++++++ src/Resources/config/services.xml | 3 +++ src/Wrapper/DompdfWrapper.php | 28 ++++++++++++++++++++++++++++ 4 files changed, 55 insertions(+) create mode 100755 src/Events.php diff --git a/README.md b/README.md index 1c65ad73..b1ab4b21 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,12 @@ core23_dompdf: ... ``` +### Events + +The dompdf wrapper dispatches events to convenient get the inner dompdf instance when creating the pdf. +- `dompdf.output` is dispatched in getPdf +- `dompdf.stream` is dispatched in streamHtml + ## License This bundle is under the [MIT license](LICENSE.md). diff --git a/src/Events.php b/src/Events.php new file mode 100755 index 00000000..9da1c95c --- /dev/null +++ b/src/Events.php @@ -0,0 +1,18 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Core23\DompdfBundle; + +final class Events +{ + const OUTPUT = 'dompdf.output'; + const STREAM = 'dompdf.stream'; +} diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 0b320e85..7b6e16ca 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -5,6 +5,9 @@ + + + %core23_dompdf.options% diff --git a/src/Wrapper/DompdfWrapper.php b/src/Wrapper/DompdfWrapper.php index ca63f63f..5f50f026 100644 --- a/src/Wrapper/DompdfWrapper.php +++ b/src/Wrapper/DompdfWrapper.php @@ -11,7 +11,10 @@ namespace Core23\DompdfBundle\Wrapper; +use Core23\DompdfBundle\Events; use Core23\DompdfBundle\Factory\DompdfFactoryInterface; +use Symfony\Component\EventDispatcher\EventDispatcherInterface; +use Symfony\Component\EventDispatcher\GenericEvent; final class DompdfWrapper implements DompdfWrapperInterface { @@ -20,6 +23,11 @@ final class DompdfWrapper implements DompdfWrapperInterface */ private $dompdfFactory; + /** + * @var EventDispatcherInterface + */ + private $eventDispatcher; + /** * @param DompdfFactoryInterface $dompdfFactory */ @@ -28,6 +36,15 @@ public function __construct(DompdfFactoryInterface $dompdfFactory) $this->dompdfFactory = $dompdfFactory; } + /** + * Set the event dispatcher. + * @param EventDispatcherInterface $eventDispatcher + */ + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) + { + $this->eventDispatcher = $eventDispatcher; + } + /** * {@inheritdoc} */ @@ -36,6 +53,12 @@ public function streamHtml(string $html, string $filename, array $options = []): $pdf = $this->dompdfFactory->create($options); $pdf->loadHtml($html); $pdf->render(); + + if ($this->eventDispatcher instanceof EventDispatcherInterface) { + $event = new GenericEvent($pdf, ['filename' => $filename, 'html' => $html]); + $this->eventDispatcher->dispatch(Events::STREAM, $event); + } + $pdf->stream($filename); } @@ -48,6 +71,11 @@ public function getPdf(string $html, array $options = []): string $pdf->loadHtml($html); $pdf->render(); + if ($this->eventDispatcher instanceof EventDispatcherInterface) { + $event = new GenericEvent($pdf, ['html' => $html]); + $this->eventDispatcher->dispatch(Events::OUTPUT, $event); + } + return $pdf->output(); } } From 3cfd5749394448f6de3a2066db2fc7d5b02d81bf Mon Sep 17 00:00:00 2001 From: Reyo Stallenberg Date: Mon, 2 Jul 2018 14:33:19 +0200 Subject: [PATCH 2/2] Corrections --- README.md | 2 ++ src/Resources/config/services.xml | 2 +- src/Wrapper/DompdfWrapper.php | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b1ab4b21..a07b7e40 100644 --- a/README.md +++ b/README.md @@ -72,6 +72,8 @@ The dompdf wrapper dispatches events to convenient get the inner dompdf instance - `dompdf.output` is dispatched in getPdf - `dompdf.stream` is dispatched in streamHtml +See [Symfony event dispatcher documentation](https://symfony.com/doc/current/event_dispatcher.html) for more info. + ## License This bundle is under the [MIT license](LICENSE.md). diff --git a/src/Resources/config/services.xml b/src/Resources/config/services.xml index 7b6e16ca..cef0acc9 100644 --- a/src/Resources/config/services.xml +++ b/src/Resources/config/services.xml @@ -6,7 +6,7 @@ - + diff --git a/src/Wrapper/DompdfWrapper.php b/src/Wrapper/DompdfWrapper.php index 5f50f026..98a93e38 100644 --- a/src/Wrapper/DompdfWrapper.php +++ b/src/Wrapper/DompdfWrapper.php @@ -38,9 +38,10 @@ public function __construct(DompdfFactoryInterface $dompdfFactory) /** * Set the event dispatcher. + * * @param EventDispatcherInterface $eventDispatcher */ - public function setEventDispatcher(EventDispatcherInterface $eventDispatcher) + public function setEventDispatcher(EventDispatcherInterface $eventDispatcher): void { $this->eventDispatcher = $eventDispatcher; }