Skip to content

Commit

Permalink
Rename Facade with fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
barryvdh committed Jan 29, 2022
1 parent a381e42 commit b14b0b8
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 77 deletions.
9 changes: 7 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"Barryvdh\\DomPDF\\ServiceProvider"
],
"aliases": {
"PDF": "Barryvdh\\DomPDF\\Facade"
"PDF": "Barryvdh\\DomPDF\\Facade\\Pdf"
}
}
},
Expand All @@ -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
}
}
}
20 changes: 6 additions & 14 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`

Expand All @@ -43,11 +34,12 @@ You can create a new DOMPDF instance and load a HTML string, file or view name.
$pdf->loadHTML('<h1>Test</h1>');
return $pdf->stream();
```


Or use the facade:

```php
use Barryvdh\DomPDF\Facade\Pdf;

$pdf = PDF::loadView('pdf.invoice', $data);
return $pdf->download('invoice.pdf');
```
Expand Down
62 changes: 3 additions & 59 deletions src/Facade.php
Original file line number Diff line number Diff line change
Expand Up @@ -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<mixed> $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);
}
}
}
67 changes: 67 additions & 0 deletions src/Facade/Pdf.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php

namespace Barryvdh\DomPDF\Facade;

use Illuminate\Support\Facades\Facade as IlluminateFacade;

/**
* @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')
*
*/
class Pdf extends IlluminateFacade
{
/**
* 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<mixed> $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);
}
}
}
1 change: 0 additions & 1 deletion src/PDF.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
*/
class PDF
{

/** @var Dompdf */
protected $dompdf;

Expand Down
1 change: 0 additions & 1 deletion src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

class ServiceProvider extends IlluminateServiceProvider
{

/**
* Indicates if loading of the provider is deferred.
*
Expand Down
24 changes: 24 additions & 0 deletions tests/PdfTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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('<h1>Test</h1>');
/** @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('<h1>Test</h1>');
/** @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('<h1>Test</h1>');
Expand Down

0 comments on commit b14b0b8

Please sign in to comment.