From 7fc57d297230184c2f882c47f4538d98b97f959b Mon Sep 17 00:00:00 2001 From: Steve Boyd Date: Wed, 13 Apr 2022 14:04:42 +1200 Subject: [PATCH] ENH PHP 8.1 compatibility --- .../PdfExportControllerExtension.php | 26 +++++++++---------- src/Extensions/PdfExportExtension.php | 12 ++++----- src/Tasks/CleanupGeneratedPdfBuildTask.php | 2 +- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/Extensions/PdfExportControllerExtension.php b/src/Extensions/PdfExportControllerExtension.php index bd97f52..342013b 100644 --- a/src/Extensions/PdfExportControllerExtension.php +++ b/src/Extensions/PdfExportControllerExtension.php @@ -33,11 +33,11 @@ public function downloadpdf() Versioned::set_stage(Versioned::LIVE); $path = $this->owner->data()->getPdfFilename(); - if (!file_exists($path)) { + if (!file_exists($path ?? '')) { $this->owner->generatePDF(); } - return HTTPRequest::send_file(file_get_contents($path), basename($path), 'application/pdf'); + return HTTPRequest::send_file(file_get_contents($path ?? ''), basename($path ?? ''), 'application/pdf'); } /** @@ -95,9 +95,9 @@ public function generatePDF() Deprecation::notice('3.0', 'wkhtmltopdf_binary config is deprecated. '. 'Use WKHTMLTOPDF_BINARY env var instead.'); } - if (!$binaryPath || !is_executable($binaryPath)) { + if (!$binaryPath || !is_executable($binaryPath ?? '')) { if (Environment::getEnv('WKHTMLTOPDF_BINARY') - && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY')) + && is_executable(Environment::getEnv('WKHTMLTOPDF_BINARY') ?? '') ) { $binaryPath = Environment::getEnv('WKHTMLTOPDF_BINARY'); } @@ -118,12 +118,12 @@ public function generatePDF() // prepare the paths $pdfFile = $this->owner->data()->getPdfFilename(); - $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile); - $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile); + $bodyFile = str_replace('.pdf', '_pdf.html', $pdfFile ?? ''); + $footerFile = str_replace('.pdf', '_pdffooter.html', $pdfFile ?? ''); // make sure the work directory exists - if (!file_exists(dirname($pdfFile))) { - Filesystem::makeFolder(dirname($pdfFile)); + if (!file_exists(dirname($pdfFile ?? ''))) { + Filesystem::makeFolder(dirname($pdfFile ?? '')); } //decide the domain to use in generation @@ -141,13 +141,13 @@ public function generatePDF() $bodyViewer = $this->owner->getViewer('pdf'); // write the output of this page to HTML, ready for conversion to PDF - file_put_contents($bodyFile, $bodyViewer->process($this->owner)); + file_put_contents($bodyFile ?? '', $bodyViewer->process($this->owner)); // get the viewer for the current template with _pdffooter $footerViewer = $this->owner->getViewer('pdffooter'); // write the output of the footer template to HTML, ready for conversion to PDF - file_put_contents($footerFile, $footerViewer->process($this->owner)); + file_put_contents($footerFile ?? '', $footerViewer->process($this->owner)); //decide what the proxy should look like $proxy = $this->owner->getPDFProxy($pdfBaseUrl); @@ -164,8 +164,8 @@ public function generatePDF() ); // remove temporary file - unlink($bodyFile); - unlink($footerFile); + unlink($bodyFile ?? ''); + unlink($footerFile ?? ''); // output any errors if ($retVal != 0) { @@ -173,6 +173,6 @@ public function generatePDF() } // serve the generated file - return HTTPRequest::send_file(file_get_contents($pdfFile), basename($pdfFile), 'application/pdf'); + return HTTPRequest::send_file(file_get_contents($pdfFile ?? ''), basename($pdfFile ?? ''), 'application/pdf'); } } diff --git a/src/Extensions/PdfExportExtension.php b/src/Extensions/PdfExportExtension.php index 1ff00a2..a5a3b58 100644 --- a/src/Extensions/PdfExportExtension.php +++ b/src/Extensions/PdfExportExtension.php @@ -70,8 +70,8 @@ public function PdfLink() $path = $this->getPdfFilename(); - if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path)) { - return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path)); + if ((Versioned::get_stage() === Versioned::LIVE) && file_exists($path ?? '')) { + return Director::baseURL() . preg_replace('#^/#', '', Director::makeRelative($path) ?? ''); } return $this->owner->Link('downloadpdf'); } @@ -82,8 +82,8 @@ public function PdfLink() public function onAfterPublish() { $filepath = $this->getPdfFilename(); - if (file_exists($filepath)) { - unlink($filepath); + if (file_exists($filepath ?? '')) { + unlink($filepath ?? ''); } } @@ -93,8 +93,8 @@ public function onAfterPublish() public function onAfterUnpublish() { $filepath = $this->getPdfFilename(); - if (file_exists($filepath)) { - unlink($filepath); + if (file_exists($filepath ?? '')) { + unlink($filepath ?? ''); } } } diff --git a/src/Tasks/CleanupGeneratedPdfBuildTask.php b/src/Tasks/CleanupGeneratedPdfBuildTask.php index 7ab26f1..eddc1c7 100644 --- a/src/Tasks/CleanupGeneratedPdfBuildTask.php +++ b/src/Tasks/CleanupGeneratedPdfBuildTask.php @@ -18,7 +18,7 @@ class CleanupGeneratedPdfBuildTask extends BuildTask public function run($request) { $path = sprintf('%s/%s', BASE_PATH, BasePage::config()->get('generated_pdf_path')); - if (!file_exists($path)) { + if (!file_exists($path ?? '')) { return false; }