diff --git a/src/Facades/SnappyImage.php b/src/Facades/SnappyImage.php index c611fcb..1708ebc 100644 --- a/src/Facades/SnappyImage.php +++ b/src/Facades/SnappyImage.php @@ -1,16 +1,18 @@ fs = $fs; - } + $this->fs = $fs; + } /** * Wrapper for the "file_get_contents" function * - * @param string $filename - * + * @param string $filename * @return string */ protected function getFileContents($filename) @@ -36,9 +37,8 @@ protected function getFileContents($filename) /** * Wrapper for the "file_exists" function * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function fileExists($filename) { @@ -48,9 +48,8 @@ protected function fileExists($filename) /** * Wrapper for the "is_file" method * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function isFile($filename) { @@ -60,9 +59,8 @@ protected function isFile($filename) /** * Wrapper for the "filesize" function * - * @param string $filename - * - * @return integer or FALSE on failure + * @param string $filename + * @return int or FALSE on failure */ protected function filesize($filename) { @@ -72,9 +70,8 @@ protected function filesize($filename) /** * Wrapper for the "unlink" function * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function unlink($filename) { @@ -84,9 +81,8 @@ protected function unlink($filename) /** * Wrapper for the "is_dir" function * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function isDir($filename) { @@ -96,13 +92,11 @@ protected function isDir($filename) /** * Wrapper for the mkdir function * - * @param string $pathname - * - * @return boolean + * @param string $pathname + * @return bool */ protected function mkdir($pathname) { return $this->fs->makeDirectory($pathname, 0777, true, true); } - -} \ No newline at end of file +} diff --git a/src/IlluminateSnappyPdf.php b/src/IlluminateSnappyPdf.php index 2146673..353599d 100644 --- a/src/IlluminateSnappyPdf.php +++ b/src/IlluminateSnappyPdf.php @@ -1,31 +1,32 @@ -fs = $fs; - } + $this->fs = $fs; + } /** * Wrapper for the "file_get_contents" function * - * @param string $filename - * + * @param string $filename * @return string */ protected function getFileContents($filename) @@ -36,9 +37,8 @@ protected function getFileContents($filename) /** * Wrapper for the "file_exists" function * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function fileExists($filename) { @@ -48,9 +48,8 @@ protected function fileExists($filename) /** * Wrapper for the "is_file" method * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function isFile($filename) { @@ -60,9 +59,8 @@ protected function isFile($filename) /** * Wrapper for the "filesize" function * - * @param string $filename - * - * @return integer or FALSE on failure + * @param string $filename + * @return int or FALSE on failure */ protected function filesize($filename) { @@ -72,9 +70,8 @@ protected function filesize($filename) /** * Wrapper for the "unlink" function * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function unlink($filename) { @@ -84,9 +81,8 @@ protected function unlink($filename) /** * Wrapper for the "is_dir" function * - * @param string $filename - * - * @return boolean + * @param string $filename + * @return bool */ protected function isDir($filename) { @@ -96,13 +92,11 @@ protected function isDir($filename) /** * Wrapper for the mkdir function * - * @param string $pathname - * - * @return boolean + * @param string $pathname + * @return bool */ protected function mkdir($pathname) { return $this->fs->makeDirectory($pathname, 0777, true, true); } - } diff --git a/src/ImageWrapper.php b/src/ImageWrapper.php index 28fe0d7..a28cb10 100644 --- a/src/ImageWrapper.php +++ b/src/ImageWrapper.php @@ -1,19 +1,20 @@ -snappy = $snappy; + $this->snappy = $snappy; } /** @@ -48,51 +46,56 @@ public function __construct(SnappyImage $snappy) */ public function snappy() { - return $this->snappy; + return $this->snappy; } public function setOption($name, $value) { $this->snappy->setOption($name, $value); + return $this; } public function setOptions($options) { $this->snappy->setOptions($options); + return $this; } /** * Load a HTML string * - * @param string $string + * @param string $string * @return static */ public function loadHTML($string) { $this->html = (string) $string; $this->file = null; + return $this; } /** * Load a HTML file * - * @param string $file + * @param string $file * @return static */ public function loadFile($file) { $this->html = null; $this->file = $file; + return $this; } - public function loadView($view, $data = array(), $mergeData = array()) + public function loadView($view, $data = [], $mergeData = []) { $this->html = View::make($view, $data, $mergeData)->render(); $this->file = null; + return $this; } @@ -100,17 +103,16 @@ public function loadView($view, $data = array(), $mergeData = array()) * Output the PDF as a string. * * @return string The rendered PDF as string + * * @throws \InvalidArgumentException */ public function output() { - if ($this->html) - { + if ($this->html) { return $this->snappy->getOutputFromHtml($this->html, $this->options); } - if ($this->file) - { + if ($this->file) { return $this->snappy->getOutput($this->file, $this->options); } @@ -120,18 +122,14 @@ public function output() /** * Save the image to a file * - * @param $filename * @return static */ public function save($filename, $overwrite = false) { - if ($this->html) - { + if ($this->html) { $this->snappy->generateFromHtml($this->html, $filename, $this->options, $overwrite); - } - elseif ($this->file) - { + } elseif ($this->file) { $this->snappy->generate($this->file, $filename, $this->options, $overwrite); } @@ -141,46 +139,47 @@ public function save($filename, $overwrite = false) /** * Make the image downloadable by the user * - * @param string $filename + * @param string $filename * @return \Symfony\Component\HttpFoundation\Response */ public function download($filename = 'image.jpg') { - return new Response($this->output(), 200, array( + return new Response($this->output(), 200, [ 'Content-Type' => 'image/jpeg', - 'Content-Disposition' => 'attachment; filename="'.$filename.'"' - )); + 'Content-Disposition' => 'attachment; filename="'.$filename.'"', + ]); } /** * Return a response with the image to show in the browser * - * @param string $filename + * @param string $filename * @return \Illuminate\Http\Response */ public function inline($filename = 'image.jpg') { - return new Response($this->output(), 200, array( + return new Response($this->output(), 200, [ 'Content-Type' => 'image/jpeg', 'Content-Disposition' => 'inline; filename="'.$filename.'"', - )); + ]); } - + /** * Return a response with the image to show in the browser * * @deprecated Use inline() instead - * @param string $filename + * + * @param string $filename * @return \Symfony\Component\HttpFoundation\Response */ public function stream($filename = 'image.jpg') { - return new StreamedResponse(function() { + return new StreamedResponse(function () { echo $this->output(); - }, 200, array( + }, 200, [ 'Content-Type' => 'image/jpeg', 'Content-Disposition' => 'inline; filename="'.$filename.'"', - )); + ]); } /** @@ -191,18 +190,16 @@ public function stream($filename = 'image.jpg') * ->view => loadView * ->file => loadFile * - * @param string $name - * @param array $arguments + * @param string $name * @return mixed */ public function __call($name, array $arguments) { - $method = 'load' . ucfirst($name); - if (method_exists($this, $method)) - { - return call_user_func_array(array($this, $method), $arguments); + $method = 'load'.ucfirst($name); + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], $arguments); } - return call_user_func_array (array($this->snappy, $name), $arguments); + return call_user_func_array([$this->snappy, $name], $arguments); } } diff --git a/src/LumenServiceProvider.php b/src/LumenServiceProvider.php index d8a567c..f40ed3f 100644 --- a/src/LumenServiceProvider.php +++ b/src/LumenServiceProvider.php @@ -1,4 +1,6 @@ -app->configure('snappy'); - $configPath = __DIR__ . '/../config/snappy.php'; + $configPath = __DIR__.'/../config/snappy.php'; $this->mergeConfigFrom($configPath, 'snappy'); } public function boot() { if ($this->app['config']->get('snappy.pdf.enabled')) { - $this->app->bind('snappy.pdf',function ($app) { + $this->app->bind('snappy.pdf', function ($app) { $binary = $app['config']->get('snappy.pdf.binary', '/usr/local/bin/wkhtmltopdf'); - $options = $app['config']->get('snappy.pdf.options', array()); - $env = $app['config']->get('snappy.pdf.env', array()); + $options = $app['config']->get('snappy.pdf.options', []); + $env = $app['config']->get('snappy.pdf.env', []); $timeout = $app['config']->get('snappy.pdf.timeout', false); $snappy = new IlluminateSnappyPdf($app['files'], $binary, $options, $env); - if (false !== $timeout) { + if ($timeout !== false) { $snappy->setTimeout($timeout); } return $snappy; }); - $this->app->bind('snappy.pdf.wrapper',function ($app) { + $this->app->bind('snappy.pdf.wrapper', function ($app) { return new PdfWrapper($app['snappy.pdf']); }); $this->app->alias('snappy.pdf.wrapper', 'Barryvdh\Snappy\PdfWrapper'); } if ($this->app['config']->get('snappy.image.enabled')) { - $this->app->bind('snappy.image',function ($app) { + $this->app->bind('snappy.image', function ($app) { $binary = $app['config']->get('snappy.image.binary', '/usr/local/bin/wkhtmltoimage'); - $options = $app['config']->get('snappy.image.options', array()); - $env = $app['config']->get('snappy.image.env', array()); + $options = $app['config']->get('snappy.image.options', []); + $env = $app['config']->get('snappy.image.env', []); $timeout = $app['config']->get('snappy.image.timeout', false); $image = new IlluminateSnappyImage($app['files'], $binary, $options, $env); - if (false !== $timeout) { + if ($timeout !== false) { $image->setTimeout($timeout); } return $image; }); - $this->app->bind('snappy.image.wrapper',function ($app) { + $this->app->bind('snappy.image.wrapper', function ($app) { return new ImageWrapper($app['snappy.image']); }); $this->app->alias('snappy.image.wrapper', 'Barryvdh\Snappy\ImageWrapper'); @@ -76,6 +78,6 @@ public function boot() */ public function provides() { - return array('snappy.pdf', 'snappy.pdf.wrapper', 'snappy.image', 'snappy.image.wrapper'); + return ['snappy.pdf', 'snappy.pdf.wrapper', 'snappy.image', 'snappy.image.wrapper']; } } diff --git a/src/PdfFaker.php b/src/PdfFaker.php index 2b57266..57fcbeb 100644 --- a/src/PdfFaker.php +++ b/src/PdfFaker.php @@ -1,30 +1,32 @@ -view = ViewFacade::make($view, $data, $mergeData); + return parent::loadView($view, $data, $mergeData); } - /** * Ensure that the response has a view as its original content. * @@ -41,9 +43,9 @@ protected function ensureResponseHasView() public function assertViewIs($value) { - PHPUnit::assertEquals($value, $this->view->getName()); + PHPUnit::assertEquals($value, $this->view->getName()); - return $this; + return $this; } /** @@ -75,7 +77,6 @@ public function assertViewHas($key, $value = null) /** * Assert that the response view has a given list of bound data. * - * @param array $bindings * @return $this */ public function assertViewHasAll(array $bindings) @@ -157,7 +158,7 @@ public function assertDontSeeText($value) return $this; } - + /** * Assert that the given string is equal to the saved filename. * @@ -166,9 +167,9 @@ public function assertDontSeeText($value) */ public function assertFileNameIs($value) { - PHPUnit::assertEquals($value, $this->filename); + PHPUnit::assertEquals($value, $this->filename); - return $this; + return $this; } public function output() @@ -286,12 +287,12 @@ public function output() /** * Save the PDF to a file * - * @param $filename * @return $this */ public function save($filename, $overwrite = false) { - $this->filename = $filename; + $this->filename = $filename; + return $this; } } diff --git a/src/PdfWrapper.php b/src/PdfWrapper.php index 97fdfca..6cd1e03 100644 --- a/src/PdfWrapper.php +++ b/src/PdfWrapper.php @@ -1,19 +1,20 @@ -snappy = $snappy; + $this->snappy = $snappy; } /** @@ -47,66 +45,70 @@ public function __construct(SnappyPDF $snappy) * * @return \Knp\Snappy\Pdf */ - public function snappy() - { - return $this->snappy; - } + public function snappy() + { + return $this->snappy; + } /** * Set temporary folder * - * @param string $path + * @param string $path */ - public function setTemporaryFolder($path) - { - $this->snappy->setTemporaryFolder($path); - return $this; - } + public function setTemporaryFolder($path) + { + $this->snappy->setTemporaryFolder($path); + + return $this; + } /** * Set the paper size (default A4) * - * @param string $paper - * @param string $orientation + * @param string $paper + * @param string $orientation * @return $this */ - public function setPaper($paper, $orientation=null) + public function setPaper($paper, $orientation = null) { $this->snappy->setOption('page-size', $paper); - if($orientation){ + if ($orientation) { $this->snappy->setOption('orientation', $orientation); } + return $this; } /** * Set the orientation (default portrait) * - * @param string $orientation + * @param string $orientation * @return $this */ public function setOrientation($orientation) { $this->snappy->setOption('orientation', $orientation); + return $this; } /** * Show or hide warnings * - * @param bool $warnings + * @param bool $warnings * @return $this + * * @deprecated */ public function setWarnings($warnings) { - //Doesn't do anything + // Doesn't do anything return $this; } /** - * @param string $name - * @param mixed $value + * @param string $name + * @param mixed $value * @return $this */ public function setOption($name, $value) @@ -115,23 +117,25 @@ public function setOption($name, $value) $value = $value->render(); } $this->snappy->setOption($name, $value); + return $this; } /** - * @param array $options + * @param array $options * @return $this */ public function setOptions($options) { $this->snappy->setOptions($options); + return $this; } /** * Load a HTML string * - * @param Array|string|Renderable $html + * @param array|string|Renderable $html * @return $this */ public function loadHTML($html) @@ -141,73 +145,70 @@ public function loadHTML($html) } $this->html = $html; $this->file = null; + return $this; } /** * Load a HTML file * - * @param string $file + * @param string $file * @return $this */ public function loadFile($file) { $this->html = null; $this->file = $file; + return $this; } /** * Load a View and convert to HTML * - * @param string $view - * @param array $data - * @param array $mergeData + * @param string $view + * @param array $data + * @param array $mergeData * @return $this */ - public function loadView($view, $data = array(), $mergeData = array()) + public function loadView($view, $data = [], $mergeData = []) { - $view = View::make($view, $data, $mergeData); + $view = View::make($view, $data, $mergeData); - return $this->loadHTML($view); + return $this->loadHTML($view); } /** - * Output the PDF as a string. - * - * @return string The rendered PDF as string - * @throws \InvalidArgumentException - */ - public function output() - { - if ($this->html) - { - return $this->snappy->getOutputFromHtml($this->html, $this->options); - } - - if ($this->file) - { - return $this->snappy->getOutput($this->file, $this->options); - } - - throw new \InvalidArgumentException('PDF Generator requires a html or file in order to produce output.'); + * Output the PDF as a string. + * + * @return string The rendered PDF as string + * + * @throws \InvalidArgumentException + */ + public function output() + { + if ($this->html) { + return $this->snappy->getOutputFromHtml($this->html, $this->options); + } + + if ($this->file) { + return $this->snappy->getOutput($this->file, $this->options); + } + + throw new \InvalidArgumentException('PDF Generator requires a html or file in order to produce output.'); } /** * Save the PDF to a file * - * @param $filename * @return $this */ public function save($filename, $overwrite = false) { - if ($this->html) - { + if ($this->html) { $this->snappy->generateFromHtml($this->html, $filename, $this->options, $overwrite); - } - elseif ($this->file) - { + } elseif ($this->file) { $this->snappy->generate($this->file, $filename, $this->options, $overwrite); } @@ -217,46 +218,47 @@ public function save($filename, $overwrite = false) /** * Make the PDF downloadable by the user * - * @param string $filename + * @param string $filename * @return \Illuminate\Http\Response */ public function download($filename = 'document.pdf') { - return new Response($this->output(), 200, array( + return new Response($this->output(), 200, [ 'Content-Type' => 'application/pdf', - 'Content-Disposition' => 'attachment; filename="'.$filename.'"' - )); + 'Content-Disposition' => 'attachment; filename="'.$filename.'"', + ]); } /** * Return a response with the PDF to show in the browser * - * @param string $filename + * @param string $filename * @return \Illuminate\Http\Response */ public function inline($filename = 'document.pdf') { - return new Response($this->output(), 200, array( + return new Response($this->output(), 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename="'.$filename.'"', - )); + ]); } /** * Return a response with the PDF to show in the browser * - * @param string $filename + * @param string $filename * @return \Symfony\Component\HttpFoundation\StreamedResponse + * * @deprecated use inline() instead */ public function stream($filename = 'document.pdf') { - return new StreamedResponse(function() { + return new StreamedResponse(function () { echo $this->output(); - }, 200, array( + }, 200, [ 'Content-Type' => 'application/pdf', 'Content-Disposition' => 'inline; filename="'.$filename.'"', - )); + ]); } /** @@ -267,19 +269,16 @@ public function stream($filename = 'document.pdf') * ->view => loadView * ->file => loadFile * - * @param string $name - * @param array $arguments + * @param string $name * @return mixed */ public function __call($name, array $arguments) { - $method = 'load' . ucfirst($name); - if (method_exists($this, $method)) - { - return call_user_func_array(array($this, $method), $arguments); + $method = 'load'.ucfirst($name); + if (method_exists($this, $method)) { + return call_user_func_array([$this, $method], $arguments); } - return call_user_func_array (array($this->snappy, $name), $arguments); + return call_user_func_array([$this->snappy, $name], $arguments); } - } diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index 5e67eb4..ef3db64 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -1,88 +1,85 @@ -mergeConfigFrom($configPath, 'snappy'); } public function boot() { - $configPath = __DIR__ . '/../config/snappy.php'; + $configPath = __DIR__.'/../config/snappy.php'; $this->publishes([$configPath => config_path('snappy.php')], 'config'); - if($this->app['config']->get('snappy.pdf.enabled')){ - $this->app->bind('snappy.pdf', function($app) - { + if ($this->app['config']->get('snappy.pdf.enabled')) { + $this->app->bind('snappy.pdf', function ($app) { $binary = $app['config']->get('snappy.pdf.binary', '/usr/local/bin/wkhtmltopdf'); - $options = $app['config']->get('snappy.pdf.options', array()); - $env = $app['config']->get('snappy.pdf.env', array()); + $options = $app['config']->get('snappy.pdf.options', []); + $env = $app['config']->get('snappy.pdf.env', []); $timeout = $app['config']->get('snappy.pdf.timeout', false); $snappy = new IlluminateSnappyPdf($app['files'], $binary, $options, $env); - if (false !== $timeout) { + if ($timeout !== false) { $snappy->setTimeout($timeout); } return $snappy; }); - $this->app->bind('snappy.pdf.wrapper', function($app) - { + $this->app->bind('snappy.pdf.wrapper', function ($app) { return new PdfWrapper($app['snappy.pdf']); }); $this->app->alias('snappy.pdf.wrapper', 'Barryvdh\Snappy\PdfWrapper'); } - - if($this->app['config']->get('snappy.image.enabled')){ - $this->app->bind('snappy.image', function($app) - { + if ($this->app['config']->get('snappy.image.enabled')) { + $this->app->bind('snappy.image', function ($app) { $binary = $app['config']->get('snappy.image.binary', '/usr/local/bin/wkhtmltoimage'); - $options = $app['config']->get('snappy.image.options', array()); - $env = $app['config']->get('snappy.image.env', array()); + $options = $app['config']->get('snappy.image.options', []); + $env = $app['config']->get('snappy.image.env', []); $timeout = $app['config']->get('snappy.image.timeout', false); $image = new IlluminateSnappyImage($app['files'], $binary, $options, $env); - if (false !== $timeout) { + if ($timeout !== false) { $image->setTimeout($timeout); } return $image; }); - $this->app->bind('snappy.image.wrapper', function($app) - { + $this->app->bind('snappy.image.wrapper', function ($app) { return new ImageWrapper($app['snappy.image']); }); $this->app->alias('snappy.image.wrapper', 'Barryvdh\Snappy\ImageWrapper'); } - } + } - /** - * Get the services provided by the provider. - * - * @return array - */ - public function provides() - { - return array('snappy.pdf', 'snappy.pdf.wrapper', 'snappy.image', 'snappy.image.wrapper'); - } + /** + * Get the services provided by the provider. + * + * @return array + */ + public function provides() + { + return ['snappy.pdf', 'snappy.pdf.wrapper', 'snappy.image', 'snappy.image.wrapper']; + } } diff --git a/tests/PdfTest.php b/tests/PdfTest.php index 40373eb..8ed902f 100644 --- a/tests/PdfTest.php +++ b/tests/PdfTest.php @@ -2,7 +2,6 @@ namespace Barryvdh\Snappy\Tests; -use Barryvdh\Snappy\Facade; use Barryvdh\Snappy\Facades\SnappyPdf; use Illuminate\Http\Response; use Symfony\Component\HttpFoundation\StreamedResponse; @@ -80,5 +79,4 @@ public function testView(): void $this->assertEquals('application/pdf', $response->headers->get('Content-Type')); $this->assertEquals('attachment; filename="test.pdf"', $response->headers->get('Content-Disposition')); } - }