Files
InvoiceShelf/app/Support/Pdf/PdfTemplateUtils.php
Darko Gjorgjijoski 6d1816bd1b refactor: reorganize app/Services and app/Support by domain
The app/Services/ directory had grown into 22 flat files at the root plus 7 uneven subdirectories — finding anything required scrolling through an alphabetical mix of small CRUD services, infrastructure drivers, and install-time utilities. This commit groups services by domain, folds Backup into a new Storage namespace, and moves framework-infrastructure and install-time helpers out of Services and into Support where they belong.

New Services layout: Documents/ (Invoice, Estimate, RecurringInvoice, Payment, Expense, Transaction, DocumentItem, SerialNumber, Currency — matches the 'Documents' navigation group); Company/ (Company, Member, Invitation); Mail/ (MailConfiguration, CompanyMailConfig); Storage/ (FileDisk, plus Backup folded in). ExchangeRateProviderService moves next to its drivers in ExchangeRate/; FontService moves into Pdf/ where it belongs. CustomerService, ItemService, CustomFieldService stay at the Services root as standalone single-file domains.

Moves to Support/: Hashids/ (library wrapper — not business logic); Setup/ (one-shot install-time utilities — stateless helpers); Pdf/ (ImageUtils, PdfTemplateUtils, plus the existing PdfHtmlSanitizer consolidated into the same subdir). These are all framework infrastructure and stateless utilities — the 'service' label never really fit them.

Namespace declarations in 29 moved files updated to match new paths. 62 consumer files (controllers, other services, tests, database factories, seeders, routes, bootstrap/providers.php) have their use statements rewritten via a literal-string replacement script — no regex meant no risk of half-matching. Three Documents services needed an explicit 'use App\Services\Mail\CompanyMailConfigService' added because the same-namespace short reference they relied on no longer resolves after the split.

Verified: composer dump-autoload, 350 tests pass (850 assertions), vendor/bin/pint clean, npm run build succeeds.
2026-04-11 10:00:00 +02:00

141 lines
4.1 KiB
PHP

<?php
namespace App\Support\Pdf;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
class PdfTemplateUtils
{
/**
* Find the formatted template
*
* @param string $imageFormat
* @return array|null
*/
public static function findFormattedTemplate($templateType, $templateName, $imageFormat = 'base64')
{
foreach (array_reverse(self::getFormattedTemplates($templateType, $imageFormat)) as $formattedTemplate) {
if ($formattedTemplate['name'] === $templateName) {
return $formattedTemplate;
}
}
return null;
}
/**
* Return the available formatted template paths
*
* @param string $imageFormat
* @return array|array[]
*/
public static function getFormattedTemplates($templateType, $imageFormat = 'base64')
{
$files_native = array_map(function ($file) {
return [
'path' => $file,
'custom' => false,
];
}, Storage::disk('views')->files(sprintf('/app/pdf/%s', $templateType)));
$files_custom = array_map(function ($file) {
return [
'path' => $file,
'custom' => true,
];
}, Storage::disk('pdf_templates')->files(sprintf('/%s', $templateType)));
$files = array_merge($files_native, $files_custom);
$files = array_filter($files, function ($file) {
return Str::endsWith($file['path'], '.blade.php');
});
return array_map(function ($file) use ($templateType, $imageFormat) {
$templateName = Str::before(basename($file['path']), '.blade.php');
if ($file['custom']) {
$imagePath = self::getCustomTemplateFilePath($templateType, sprintf('%s.png', $templateName));
$isCustomTemplate = true;
} else {
$imagePath = resource_path('static/img/PDF/'.$templateName.'.png');
$isCustomTemplate = false;
}
if (empty($imageFormat)) {
$imageValue = '';
} elseif ($imageFormat == 'path') {
$imageValue = $imagePath;
} else {
$imageValue = File::exists($imagePath) ? ImageUtils::toBase64Src($imagePath) : '';
}
return [
'name' => $templateName,
'path' => $imageValue,
'custom' => $isCustomTemplate,
];
}, $files);
}
/**
* Returns custom template path
*
* @param string $fileName
*/
public static function getCustomTemplateFilePath($templateType, $fileName = ''): string
{
$path = ! empty($fileName) ? sprintf('/%s/%s', $templateType, $fileName) : sprintf('/%s/', $templateType);
return Storage::disk('pdf_templates')->path($path);
}
/**
* Check if custom template exists.
*
* @param $templateName
* @return string
*/
public static function customTemplateFileExists($templateType, $fileName)
{
return Storage::disk('pdf_templates')->exists(sprintf('/%s/%s', $templateType, $fileName));
}
/**
* Save template markup file
*
* @return bool|string
*/
public static function toCustomTemplateMarkupFile($contents, $templateType, $templateName)
{
return self::toCustomTemplateFile($contents, $templateType, $templateName.'.blade.php');
}
/**
* Save template image file
*
*
* @return bool|string
*/
public static function toCustomTemplateImageFile($contents, $templateType, $templateName, $imageType = 'png')
{
return self::toCustomTemplateFile($contents, $templateType, $templateName.'.'.$imageType);
}
/**
* Save file contents into a template file of specific template type.
*
*
* @return bool|string
*/
public static function toCustomTemplateFile($contents, $templateType, $fileName)
{
return Storage::disk('pdf_templates')->put(
sprintf('/%s/%s', $templateType, $fileName),
$contents
);
}
}