75 lines
2.3 KiB
PHP
75 lines
2.3 KiB
PHP
<?php
|
|
|
|
namespace Modules\Stylist\Services;
|
|
|
|
use Illuminate\Filesystem\Filesystem;
|
|
use Modules\Stylist\Theme\Loader;
|
|
use Modules\Stylist\Theme\Theme;
|
|
|
|
final class ThemeValidator
|
|
{
|
|
public function __construct(
|
|
private readonly Filesystem $files,
|
|
private readonly Loader $loader,
|
|
private readonly ThemeAccessibilityValidator $accessibility,
|
|
) {}
|
|
|
|
/** @return array<int, array{severity:string,message:string,file:string}> */
|
|
public function validatePath(string $path): array
|
|
{
|
|
$theme = $this->loader->fromPath($path);
|
|
|
|
return $this->validate($theme);
|
|
}
|
|
|
|
/** @return array<int, array{severity:string,message:string,file:string}> */
|
|
public function validate(Theme $theme): array
|
|
{
|
|
$issues = [];
|
|
$entryView = 'views/'.str_replace('.', '/', $theme->entryView()).'.blade.php';
|
|
|
|
if (! $theme->has($entryView)) {
|
|
$issues[] = [
|
|
'severity' => 'error',
|
|
'message' => "Entry view [{$theme->entryView()}] was not found.",
|
|
'file' => $entryView,
|
|
];
|
|
}
|
|
|
|
foreach (['stylesheet' => $theme->stylesheet(), 'script' => $theme->script()] as $label => $relativePath) {
|
|
if ($relativePath !== '' && ! $theme->has($relativePath)) {
|
|
$issues[] = [
|
|
'severity' => 'error',
|
|
'message' => ucfirst($label)." file [{$relativePath}] was not found.",
|
|
'file' => $relativePath,
|
|
];
|
|
}
|
|
}
|
|
|
|
$preview = $theme->preview();
|
|
if ($preview !== null && ! $theme->has($preview)) {
|
|
$issues[] = [
|
|
'severity' => 'warning',
|
|
'message' => "Preview file [{$preview}] was not found.",
|
|
'file' => $preview,
|
|
];
|
|
}
|
|
|
|
foreach ((array) ($theme->meta()['assets'] ?? []) as $asset) {
|
|
if (! is_string($asset) || $asset === '') {
|
|
continue;
|
|
}
|
|
|
|
if (! $theme->has($asset)) {
|
|
$issues[] = [
|
|
'severity' => 'warning',
|
|
'message' => "Declared asset [{$asset}] was not found.",
|
|
'file' => $asset,
|
|
];
|
|
}
|
|
}
|
|
|
|
return array_merge($issues, $this->accessibility->validate($theme));
|
|
}
|
|
}
|