From b14b0b819e5af6a5f9b1f66550a97502db6fa396 Mon Sep 17 00:00:00 2001 From: "Barry vd. Heuvel" Date: Sat, 29 Jan 2022 08:35:07 +0100 Subject: [PATCH] Rename Facade with fallback --- composer.json | 9 ++++-- readme.md | 20 ++++-------- src/Facade.php | 62 ++------------------------------------ src/Facade/Pdf.php | 67 +++++++++++++++++++++++++++++++++++++++++ src/PDF.php | 1 - src/ServiceProvider.php | 1 - tests/PdfTest.php | 24 +++++++++++++++ 7 files changed, 107 insertions(+), 77 deletions(-) create mode 100644 src/Facade/Pdf.php diff --git a/composer.json b/composer.json index e046fe9..915af4d 100644 --- a/composer.json +++ b/composer.json @@ -39,7 +39,7 @@ "Barryvdh\\DomPDF\\ServiceProvider" ], "aliases": { - "PDF": "Barryvdh\\DomPDF\\Facade" + "PDF": "Barryvdh\\DomPDF\\Facade\\Pdf" } } }, @@ -50,5 +50,10 @@ "phpstan": "phpstan analyze --memory-limit=-1" }, "minimum-stability": "dev", - "prefer-stable": true + "prefer-stable": true, + "config": { + "allow-plugins": { + "phpro/grumphp": true + } + } } diff --git a/readme.md b/readme.md index 2564668..195ae3f 100644 --- a/readme.md +++ b/readme.md @@ -4,23 +4,14 @@ ![Tests](https://github.com/barryvdh/laravel-dompdf/workflows/Tests/badge.svg) -Require this package in your composer.json and update composer. This will download the package and the dompdf + fontlib libraries also. - - composer require barryvdh/laravel-dompdf - ## Installation -### Laravel 5.x: - -After updating composer, add the ServiceProvider to the providers array in config/app.php - - Barryvdh\DomPDF\ServiceProvider::class, - -You can optionally use the facade for shorter code. Add this to your facades: +### Laravel +Require this package in your composer.json and update composer. This will download the package and the dompdf + fontlib libraries also. - 'PDF' => Barryvdh\DomPDF\Facade::class, + composer require barryvdh/laravel-dompdf -### Lumen: +### Lumen After updating composer add the following lines to register provider in `bootstrap/app.php` @@ -43,11 +34,12 @@ You can create a new DOMPDF instance and load a HTML string, file or view name. $pdf->loadHTML('

Test

'); return $pdf->stream(); ``` - Or use the facade: ```php + use Barryvdh\DomPDF\Facade\Pdf; + $pdf = PDF::loadView('pdf.invoice', $data); return $pdf->download('invoice.pdf'); ``` diff --git a/src/Facade.php b/src/Facade.php index bfb3009..f1acd8a 100644 --- a/src/Facade.php +++ b/src/Facade.php @@ -2,67 +2,11 @@ namespace Barryvdh\DomPDF; -use Illuminate\Support\Facades\Facade as IlluminateFacade; +use Barryvdh\DomPDF\Facade\Pdf as PdfFacade; /** - * @method static \Barryvdh\DomPDF\PDF setPaper($paper, $orientation = 'portrait') - * @method static \Barryvdh\DomPDF\PDF setWarnings($warnings) - * @method static \Barryvdh\DomPDF\PDF setOptions(array $options) - * @method static \Barryvdh\DomPDF\PDF loadView($view, $data = array(), $mergeData = array(), $encoding = null) - * @method static \Barryvdh\DomPDF\PDF loadHTML($string, $encoding = null) - * @method static \Barryvdh\DomPDF\PDF loadFile($file) - * @method static \Barryvdh\DomPDF\PDF addInfo($info) - * @method static string output($options = []) - * @method static \Barryvdh\DomPDF\PDF save() - * @method static \Illuminate\Http\Response download($filename = 'document.pdf') - * @method static \Illuminate\Http\Response stream($filename = 'document.pdf') - * + * @deprecated use \Barryvdh\DomPDF\Facade\Pdf instead */ -class Facade extends IlluminateFacade +class Facade extends PdfFacade { - - /** - * Get the registered name of the component. - * - * @return string - */ - protected static function getFacadeAccessor() - { - return 'dompdf.wrapper'; - } - - /** - * Resolve a new instance - * @param string $method - * @param array $args - * @return mixed - */ - public static function __callStatic($method, $args) - { - $instance = static::$app->make(static::getFacadeAccessor()); - - switch (count($args)) { - case 0: - return $instance->$method(); - - case 1: - return $instance->$method($args[0]); - - case 2: - return $instance->$method($args[0], $args[1]); - - case 3: - return $instance->$method($args[0], $args[1], $args[2]); - - case 4: - return $instance->$method($args[0], $args[1], $args[2], $args[3]); - - default: - $callable = [$instance, $method]; - if (! is_callable($callable)) { - throw new \UnexpectedValueException("Method PDF::{$method}() does not exist."); - } - return call_user_func_array($callable, $args); - } - } } diff --git a/src/Facade/Pdf.php b/src/Facade/Pdf.php new file mode 100644 index 0000000..72b826b --- /dev/null +++ b/src/Facade/Pdf.php @@ -0,0 +1,67 @@ + $args + * @return mixed + */ + public static function __callStatic($method, $args) + { + $instance = static::$app->make(static::getFacadeAccessor()); + + switch (count($args)) { + case 0: + return $instance->$method(); + + case 1: + return $instance->$method($args[0]); + + case 2: + return $instance->$method($args[0], $args[1]); + + case 3: + return $instance->$method($args[0], $args[1], $args[2]); + + case 4: + return $instance->$method($args[0], $args[1], $args[2], $args[3]); + + default: + $callable = [$instance, $method]; + if (! is_callable($callable)) { + throw new \UnexpectedValueException("Method PDF::{$method}() does not exist."); + } + return call_user_func_array($callable, $args); + } + } +} diff --git a/src/PDF.php b/src/PDF.php index ece223f..c603548 100644 --- a/src/PDF.php +++ b/src/PDF.php @@ -19,7 +19,6 @@ */ class PDF { - /** @var Dompdf */ protected $dompdf; diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index b1a09fa..3e0febd 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -9,7 +9,6 @@ class ServiceProvider extends IlluminateServiceProvider { - /** * Indicates if loading of the provider is deferred. * diff --git a/tests/PdfTest.php b/tests/PdfTest.php index 82a392d..19e2eea 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -19,6 +19,30 @@ public function testAlias(): void $this->assertEquals('attachment; filename="test.pdf"', $response->headers->get('Content-Disposition')); } + public function testFacade(): void + { + $pdf = Facade\Pdf::loadHtml('

Test

'); + /** @var Response $response */ + $response = $pdf->download('test.pdf'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertNotEmpty($response->getContent()); + $this->assertEquals('application/pdf', $response->headers->get('Content-Type')); + $this->assertEquals('attachment; filename="test.pdf"', $response->headers->get('Content-Disposition')); + } + + public function testDeprecatedFacade(): void + { + $pdf = Facade::loadHtml('

Test

'); + /** @var Response $response */ + $response = $pdf->download('test.pdf'); + + $this->assertInstanceOf(Response::class, $response); + $this->assertNotEmpty($response->getContent()); + $this->assertEquals('application/pdf', $response->headers->get('Content-Type')); + $this->assertEquals('attachment; filename="test.pdf"', $response->headers->get('Content-Disposition')); + } + public function testDownload(): void { $pdf = Facade::loadHtml('

Test

');