Skip to content

Commit

Permalink
Merge pull request #16 from reyostallenberg/events
Browse files Browse the repository at this point in the history
Dispatch events in DompdfWrapper
  • Loading branch information
core23 committed Oct 20, 2018
2 parents be37ae6 + fcc9ac6 commit 1ed4300
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 7 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,14 @@ 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

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).
Expand Down
7 changes: 7 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,19 @@
"symfony/http-foundation": "^3.4 || ^4.0",
"symfony/http-kernel": "^3.4 || ^4.0"
},
"conflict": {
"symfony/event-dispatcher": "<3.4"
},
"require-dev": {
"bamarni/composer-bin-plugin": "^1.2",
"matthiasnoback/symfony-dependency-injection-test": "^2.3",
"sllh/composer-lint": "^1.0",
"symfony/event-dispatcher": "^3.4 || ^4.0",
"symfony/phpunit-bridge": "^4.0"
},
"suggest": {
"symfony/event-dispatcher": "If you need to modify the PDF rendering"
},
"config": {
"sort-packages": true
},
Expand Down
3 changes: 3 additions & 0 deletions phpstan.neon
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ parameters:
ignoreErrors:
# PHPUnit
- '#Parameter \#1 \$dompdfFactory of class Core23\\DompdfBundle\\Wrapper\\DompdfWrapper constructor expects Core23\\DompdfBundle\\Factory\\DompdfFactoryInterface, PHPUnit\\Framework\\MockObject\\MockObject given.#'
- '#Parameter \#2 \$eventDispatcher of class Core23\\DompdfBundle\\Wrapper\\DompdfWrapper constructor expects Symfony\\Component\\EventDispatcher\\EventDispatcherInterface|null, PHPUnit\\Framework\\MockObject\\MockObject given.#'
- '#Parameter \#1 \$pdf of class Core23\\DompdfBundle\\Event\\OutputEvent constructor expects Dompdf\\Dompdf, PHPUnit\\Framework\\MockObject\\MockObject given.#'
- '#Parameter \#1 \$pdf of class Core23\\DompdfBundle\\Event\\StreamEvent constructor expects Dompdf\\Dompdf, PHPUnit\\Framework\\MockObject\\MockObject given.#'

# Symfony DI
- '#Call to an undefined method Symfony\\Component\\Config\\Definition\\Builder\\NodeDefinition::children\(\).#'
18 changes: 18 additions & 0 deletions src/DompdfEvents.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\DompdfBundle;

final class DompdfEvents
{
const OUTPUT = 'dompdf.output';
const STREAM = 'dompdf.stream';
}
58 changes: 58 additions & 0 deletions src/Event/OutputEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\DompdfBundle\Event;

use Dompdf\Dompdf;
use Symfony\Component\EventDispatcher\Event;

final class OutputEvent extends Event
{
/**
* @var Dompdf
*/
private $pdf;

/**
* @var string
*/
private $html;

/**
* @param Dompdf $pdf
* @param string $html
*/
public function __construct(Dompdf $pdf, string $html)
{
$this->pdf = $pdf;
$this->html = $html;
}

/**
* Returns the dompdf instance.
*
* @return Dompdf
*/
public function getPdf(): Dompdf
{
return $this->pdf;
}

/**
* Returns the html.
*
* @return string
*/
public function getHtml(): string
{
return $this->html;
}
}
75 changes: 75 additions & 0 deletions src/Event/StreamEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\DompdfBundle\Event;

use Dompdf\Dompdf;
use Symfony\Component\EventDispatcher\Event;

final class StreamEvent extends Event
{
/**
* @var Dompdf
*/
private $pdf;

/**
* @var string
*/
private $filename;

/**
* @var string
*/
private $html;

/**
* @param Dompdf $pdf
* @param string $filename
* @param string $html
*/
public function __construct(Dompdf $pdf, string $filename, string $html)
{
$this->pdf = $pdf;
$this->filename = $filename;
$this->html = $html;
}

/**
* Returns the dompdf instance.
*
* @return Dompdf
*/
public function getPdf(): Dompdf
{
return $this->pdf;
}

/**
* Returns the filename.
*
* @return string
*/
public function getFilename(): string
{
return $this->filename;
}

/**
* Returns the html.
*
* @return string
*/
public function getHtml(): string
{
return $this->html;
}
}
1 change: 1 addition & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<service alias="Core23\DompdfBundle\Factory\DompdfFactory" id="Core23\DompdfBundle\Factory\DompdfFactoryInterface" public="true"/>
<service id="Core23\DompdfBundle\Wrapper\DompdfWrapper" public="false">
<argument type="service" id="dompdf.factory"/>
<argument type="service" id="event_dispatcher" on-invalid="null"/>
</service>
<service id="Core23\DompdfBundle\Factory\DompdfFactory" public="false">
<argument>%core23_dompdf.options%</argument>
Expand Down
28 changes: 25 additions & 3 deletions src/Wrapper/DompdfWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@

namespace Core23\DompdfBundle\Wrapper;

use Core23\DompdfBundle\DompdfEvents;
use Core23\DompdfBundle\Event\OutputEvent;
use Core23\DompdfBundle\Event\StreamEvent;
use Core23\DompdfBundle\Factory\DompdfFactoryInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\StreamedResponse;

final class DompdfWrapper implements DompdfWrapperInterface
Expand All @@ -22,11 +26,18 @@ final class DompdfWrapper implements DompdfWrapperInterface
private $dompdfFactory;

/**
* @param DompdfFactoryInterface $dompdfFactory
* @var EventDispatcherInterface|null
*/
public function __construct(DompdfFactoryInterface $dompdfFactory)
private $eventDispatcher;

/**
* @param DompdfFactoryInterface $dompdfFactory
* @param EventDispatcherInterface|null $eventDispatcher
*/
public function __construct(DompdfFactoryInterface $dompdfFactory, EventDispatcherInterface $eventDispatcher = null)
{
$this->dompdfFactory = $dompdfFactory;
$this->dompdfFactory = $dompdfFactory;
$this->eventDispatcher = $eventDispatcher;
}

/**
Expand All @@ -37,6 +48,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 StreamEvent($pdf, $filename, $html);
$this->eventDispatcher->dispatch(DompdfEvents::STREAM, $event);
}

$pdf->stream($filename, $options);
}

Expand Down Expand Up @@ -66,6 +83,11 @@ public function getPdf(string $html, array $options = []): string
$pdf->loadHtml($html);
$pdf->render();

if ($this->eventDispatcher instanceof EventDispatcherInterface) {
$event = new OutputEvent($pdf, $html);
$this->eventDispatcher->dispatch(DompdfEvents::OUTPUT, $event);
}

return $pdf->output();
}
}
31 changes: 31 additions & 0 deletions tests/Event/OutputEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\DompdfBundle\Tests\Event;

use Core23\DompdfBundle\Event\OutputEvent;
use Dompdf\Dompdf;
use PHPUnit\Framework\TestCase;

final class OutputEventTest extends TestCase
{
public function testEvent(): void
{
$dompdf = $this->createMock(Dompdf::class);
$html = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>";

$event = new OutputEvent($dompdf, $html);

$this->assertInstanceOf(Dompdf::class, $event->getPdf());
$this->assertSame($dompdf, $event->getPdf());
$this->assertSame($html, $event->getHtml());
}
}
33 changes: 33 additions & 0 deletions tests/Event/StreamEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

/*
* (c) Christian Gripp <mail@core23.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Core23\DompdfBundle\Tests\Event;

use Core23\DompdfBundle\Event\StreamEvent;
use Dompdf\Dompdf;
use PHPUnit\Framework\TestCase;

final class StreamEventTest extends TestCase
{
public function testEvent(): void
{
$dompdf = $this->createMock(Dompdf::class);
$filename = 'file.pdf';
$html = "<h1>Foo</h1>Bar <b>baz</b><img src='img/foo'>";

$event = new StreamEvent($dompdf, $filename, $html);

$this->assertInstanceOf(Dompdf::class, $event->getPdf());
$this->assertSame($dompdf, $event->getPdf());
$this->assertSame($filename, $event->getFilename());
$this->assertSame($html, $event->getHtml());
}
}
24 changes: 20 additions & 4 deletions tests/Wrapper/DompdfWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@

namespace Core23\DompdfBundle\Tests\Wrapper;

use Core23\DompdfBundle\DompdfEvents;
use Core23\DompdfBundle\Factory\DompdfFactoryInterface;
use Core23\DompdfBundle\Wrapper\DompdfWrapper;
use Dompdf\Dompdf;
use PHPUnit\Framework\TestCase;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

final class DompdfWrapperTest extends TestCase
{
private $dompdfFactory;

private $eventDispatcher;

/**
* @var DompdfWrapper
*/
Expand All @@ -32,10 +36,10 @@ final class DompdfWrapperTest extends TestCase
*/
protected function setUp(): void
{
$this->dompdf = $this->createMock(Dompdf::class);
$this->dompdfFactory = $this->createMock(DompdfFactoryInterface::class);

$this->dompdfWrapper = new DompdfWrapper($this->dompdfFactory);
$this->dompdf = $this->createMock(Dompdf::class);
$this->dompdfFactory = $this->createMock(DompdfFactoryInterface::class);
$this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->dompdfWrapper = new DompdfWrapper($this->dompdfFactory, $this->eventDispatcher);
}

public function testStreamHtml(): void
Expand All @@ -55,6 +59,10 @@ public function testStreamHtml(): void
->method('stream')
->with($this->equalTo('file.pdf'));

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with($this->equalTo(DompdfEvents::STREAM));

$this->dompdfWrapper->streamHtml($input, 'file.pdf');
}

Expand All @@ -77,6 +85,10 @@ public function testStreamHtmlWithImg(): void
->method('stream')
->with($this->equalTo('file.pdf'));

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with($this->equalTo(DompdfEvents::STREAM));

$this->dompdfWrapper->streamHtml($input, 'file.pdf', ['tempDir' => 'bar']);
}

Expand All @@ -97,6 +109,10 @@ public function testGetPdf(): void
->method('output')
->willReturn('BINARY_CONTENT');

$this->eventDispatcher->expects($this->once())
->method('dispatch')
->with($this->equalTo(DompdfEvents::OUTPUT));

$this->dompdfWrapper->getPdf($input, ['tempDir' => 'bar']);
}
}

0 comments on commit 1ed4300

Please sign in to comment.