Locales: Fixed errors occuring for PHP < 8.2

This commit is contained in:
Dan Brown 2023-09-17 17:35:00 +01:00
parent 8994c1b9d9
commit b42e8cdb63
No known key found for this signature in database
GPG Key ID: 46D9F943C24A2EF9
4 changed files with 24 additions and 25 deletions

View File

@ -16,18 +16,11 @@ use Throwable;
class ExportFormatter class ExportFormatter
{ {
protected ImageService $imageService; public function __construct(
protected PdfGenerator $pdfGenerator; protected ImageService $imageService,
protected CspService $cspService; protected PdfGenerator $pdfGenerator,
protected CspService $cspService
/** ) {
* ExportService constructor.
*/
public function __construct(ImageService $imageService, PdfGenerator $pdfGenerator, CspService $cspService)
{
$this->imageService = $imageService;
$this->pdfGenerator = $pdfGenerator;
$this->cspService = $cspService;
} }
/** /**
@ -36,13 +29,14 @@ class ExportFormatter
* *
* @throws Throwable * @throws Throwable
*/ */
public function pageToContainedHtml(Page $page) public function pageToContainedHtml(Page $page): string
{ {
$page->html = (new PageContent($page))->render(); $page->html = (new PageContent($page))->render();
$pageHtml = view('exports.page', [ $pageHtml = view('exports.page', [
'page' => $page, 'page' => $page,
'format' => 'html', 'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(), 'cspContent' => $this->cspService->getCspMetaTagValue(),
'locale' => user()->getLocale(),
])->render(); ])->render();
return $this->containHtml($pageHtml); return $this->containHtml($pageHtml);
@ -53,7 +47,7 @@ class ExportFormatter
* *
* @throws Throwable * @throws Throwable
*/ */
public function chapterToContainedHtml(Chapter $chapter) public function chapterToContainedHtml(Chapter $chapter): string
{ {
$pages = $chapter->getVisiblePages(); $pages = $chapter->getVisiblePages();
$pages->each(function ($page) { $pages->each(function ($page) {
@ -64,6 +58,7 @@ class ExportFormatter
'pages' => $pages, 'pages' => $pages,
'format' => 'html', 'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(), 'cspContent' => $this->cspService->getCspMetaTagValue(),
'locale' => user()->getLocale(),
])->render(); ])->render();
return $this->containHtml($html); return $this->containHtml($html);
@ -74,7 +69,7 @@ class ExportFormatter
* *
* @throws Throwable * @throws Throwable
*/ */
public function bookToContainedHtml(Book $book) public function bookToContainedHtml(Book $book): string
{ {
$bookTree = (new BookContents($book))->getTree(false, true); $bookTree = (new BookContents($book))->getTree(false, true);
$html = view('exports.book', [ $html = view('exports.book', [
@ -82,6 +77,7 @@ class ExportFormatter
'bookChildren' => $bookTree, 'bookChildren' => $bookTree,
'format' => 'html', 'format' => 'html',
'cspContent' => $this->cspService->getCspMetaTagValue(), 'cspContent' => $this->cspService->getCspMetaTagValue(),
'locale' => user()->getLocale(),
])->render(); ])->render();
return $this->containHtml($html); return $this->containHtml($html);
@ -92,13 +88,14 @@ class ExportFormatter
* *
* @throws Throwable * @throws Throwable
*/ */
public function pageToPdf(Page $page) public function pageToPdf(Page $page): string
{ {
$page->html = (new PageContent($page))->render(); $page->html = (new PageContent($page))->render();
$html = view('exports.page', [ $html = view('exports.page', [
'page' => $page, 'page' => $page,
'format' => 'pdf', 'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(), 'engine' => $this->pdfGenerator->getActiveEngine(),
'locale' => user()->getLocale(),
])->render(); ])->render();
return $this->htmlToPdf($html); return $this->htmlToPdf($html);
@ -109,7 +106,7 @@ class ExportFormatter
* *
* @throws Throwable * @throws Throwable
*/ */
public function chapterToPdf(Chapter $chapter) public function chapterToPdf(Chapter $chapter): string
{ {
$pages = $chapter->getVisiblePages(); $pages = $chapter->getVisiblePages();
$pages->each(function ($page) { $pages->each(function ($page) {
@ -121,6 +118,7 @@ class ExportFormatter
'pages' => $pages, 'pages' => $pages,
'format' => 'pdf', 'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(), 'engine' => $this->pdfGenerator->getActiveEngine(),
'locale' => user()->getLocale(),
])->render(); ])->render();
return $this->htmlToPdf($html); return $this->htmlToPdf($html);
@ -131,7 +129,7 @@ class ExportFormatter
* *
* @throws Throwable * @throws Throwable
*/ */
public function bookToPdf(Book $book) public function bookToPdf(Book $book): string
{ {
$bookTree = (new BookContents($book))->getTree(false, true); $bookTree = (new BookContents($book))->getTree(false, true);
$html = view('exports.book', [ $html = view('exports.book', [
@ -139,6 +137,7 @@ class ExportFormatter
'bookChildren' => $bookTree, 'bookChildren' => $bookTree,
'format' => 'pdf', 'format' => 'pdf',
'engine' => $this->pdfGenerator->getActiveEngine(), 'engine' => $this->pdfGenerator->getActiveEngine(),
'locale' => user()->getLocale(),
])->render(); ])->render();
return $this->htmlToPdf($html); return $this->htmlToPdf($html);
@ -194,7 +193,7 @@ class ExportFormatter
/** @var DOMElement $iframe */ /** @var DOMElement $iframe */
foreach ($iframes as $iframe) { foreach ($iframes as $iframe) {
$link = $iframe->getAttribute('src'); $link = $iframe->getAttribute('src');
if (strpos($link, '//') === 0) { if (str_starts_with($link, '//')) {
$link = 'https:' . $link; $link = 'https:' . $link;
} }
@ -240,7 +239,7 @@ class ExportFormatter
foreach ($linksOutput[0] as $index => $linkMatch) { foreach ($linksOutput[0] as $index => $linkMatch) {
$oldLinkString = $linkMatch; $oldLinkString = $linkMatch;
$srcString = $linksOutput[2][$index]; $srcString = $linksOutput[2][$index];
if (strpos(trim($srcString), 'http') !== 0) { if (!str_starts_with(trim($srcString), 'http')) {
$newSrcString = url($srcString); $newSrcString = url($srcString);
$newLinkString = str_replace($srcString, $newSrcString, $oldLinkString); $newLinkString = str_replace($srcString, $newSrcString, $oldLinkString);
$htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent); $htmlContent = str_replace($oldLinkString, $newLinkString, $htmlContent);

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="{{ $locale?->htmlLang() ?? config('app.default_locale') }}" <html lang="{{ isset($locale) ? $locale->htmlLang() : config('app.default_locale') }}"
dir="{{ $locale?->htmlDirection() ?? 'auto' }}" dir="{{ isset($locale) ? $locale->htmlDirection() : 'auto' }}"
class="{{ setting()->getForCurrentUser('dark-mode-enabled') ? 'dark-mode ' : '' }}"> class="{{ setting()->getForCurrentUser('dark-mode-enabled') ? 'dark-mode ' : '' }}">
<head> <head>
<title>{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}</title> <title>{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}</title>

View File

@ -1,5 +1,5 @@
<!doctype html> <!doctype html>
<html lang="{{ $locale?->htmlLang() ?? config('app.default_locale') }}"> <html lang="{{ $locale->htmlLang() }}">
<head> <head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>@yield('title')</title> <title>@yield('title')</title>

View File

@ -1,6 +1,6 @@
<!DOCTYPE html> <!DOCTYPE html>
<html lang="{{ $locale?->htmlLang() ?? config('app.default_locale') }}" <html lang="{{ isset($locale) ? $locale->htmlLang() : config('app.default_locale') }}"
dir="{{ $locale?->htmlDirection() ?? 'auto' }}" dir="{{ isset($locale) ? $locale->htmlDirection() : 'auto' }}"
class="@yield('document-class')"> class="@yield('document-class')">
<head> <head>
<title>{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}</title> <title>{{ isset($pageTitle) ? $pageTitle . ' | ' : '' }}{{ setting('app-name') }}</title>