Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ENH PHP 8.1 compatibility #23

Merged
merged 1 commit into from
Apr 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions src/Extensions/PdfExportControllerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

/**
Expand Down Expand Up @@ -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');
}
Expand All @@ -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
Expand All @@ -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);
Expand All @@ -164,15 +164,15 @@ public function generatePDF()
);

// remove temporary file
unlink($bodyFile);
unlink($footerFile);
unlink($bodyFile ?? '');
unlink($footerFile ?? '');

// output any errors
if ($retVal != 0) {
user_error('wkhtmltopdf failed: ' . implode("\n", $output), E_USER_ERROR);
}

// 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');
}
}
12 changes: 6 additions & 6 deletions src/Extensions/PdfExportExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
Expand All @@ -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 ?? '');
}
}

Expand All @@ -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 ?? '');
}
}
}
2 changes: 1 addition & 1 deletion src/Tasks/CleanupGeneratedPdfBuildTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down