1
0
cms11/app/Console/Commands/Bundle/Validate.php

371 lines
10 KiB
PHP

<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use App\Services\HtmlValidator;
use Illuminate\Console\Command;
use Illuminate\Contracts\Filesystem\Filesystem;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Facades\Vite;
use Illuminate\Support\Str;
use JsonSchema\Validator;
use function Laravel\Prompts\progress;
class Validate extends Command
{
/**
* The console command description.
*
* @var string
*/
protected $description = 'Validate bundles metadata';
protected Filesystem $sourceDisk;
protected array $invalidJson = [];
protected array $invalidHtml = [];
protected array $invalidCss = [];
protected $bundles;
public function __construct()
{
$this->signature = 'bundle:validate
{ --no-css : Do not validate CSS }
{ --no-html : Do not validate HTML }
{ --no-json : Do not validate JSON }
{ --r|recursive : Also validate sub-bundles }
{ --source-disk= : Use specified content disk - Defaults to <info>' . env('CONTENT_DISK') . '</info> }
{ path? : Path to a specific bundle to validate - Default to <info>/</info> }
';
parent::__construct();
}
/**
* Execute the console command.
*/
public function handle()
{
$this->selectDisk()
->validateCss()
->selectBundles()
->validateJson()
->validateHtml()
->showReport();
if (
!empty($this->invalidCss)
|| !empty($this->invalidJson)
|| !empty($this->invalidHtml)
) {
exit(1);
}
}
/**
* Select the disk we will be working on
*/
private function selectDisk(): self
{
$sourceDisk = $this->option('source-disk') ?? env('CONTENT_DISK');
$this->sourceDisk = Storage::disk($sourceDisk);
$this->line(
sprintf(
'Using <info>%s</info> as source disk',
$sourceDisk
)
);
return $this;
}
/**
* Collect a list of bundles to validate
*/
private function selectBundles(): self
{
$path = $this->argument('path') ?? '/';
$comment = sprintf('Validating <info>%s</info>', $path);
if ($this->option('recursive')) {
$comment .= ' and <info>all sub-bundles</info>';
}
$this->line($comment);
$this->output->write('Collecting bundles... ');
if ($this->option('recursive')) {
$bundles = Bundle::findBundles($this->sourceDisk, $path, true);
} else {
$bundles = [new Bundle($path, $this->sourceDisk)];
}
$this->bundles = $bundles;
$this->info('OK');
return $this;
}
/**
* Return an associative array containing available bundles metadata files
* as keys and json-decoded validation schemas as values
*/
private function getJsonValidators()
{
return array_map(function ($path) {
return json_decode(file_get_contents($path));
}, config('json_validator'));
}
/**
* Perform json validation on selected bundles
*/
private function validateJson(): self
{
if ($this->option('no-json')) {
return $this;
}
$this->line('Validating <info>JSON</info>...');
$bundles = $this->bundles;
$validators = $this->getJsonValidators();
progress(
label: 'Validating... ',
steps: $bundles,
callback: fn (Bundle $bundle, $progress) => $this->handleBundleJsonValidation($bundle, $progress, $validators)
);
return $this;
}
/**
* Handle specific bundle
*/
private function handleBundleJsonValidation(Bundle $bundle, $progress, $validators)
{
$progress->label(sprintf('Validating %s ...', $bundle->getPath()));
foreach ($validators as $metadataFilename => $schema) {
$progress->hint(sprintf('Validating %s ...', $metadataFilename));
$filepath = $bundle->metadata($metadataFilename)->getFilename();
if (!$this->sourceDisk->exists($filepath)) {
continue;
}
$source = $this->sourceDisk->get($filepath) ?? '{}';
$data = json_decode($source);
$validator = new Validator();
$validator->validate($data, $schema);
if (!$validator->isValid()) {
$this->invalidJson[$bundle->getPath()] = [
'metadata' => $metadataFilename,
'errors' => $validator->getErrors(),
];
}
}
}
private function validateCss(): self
{
if ($this->option('no-css')) {
return $this;
}
$this->line('Validating <info>CSS</info>...');
$file = public_path(Vite::asset('resources/css/app.css'));
$cacheKey = sprintf('css_validation_%s', Str::slug($file));
$result = Cache::remember($cacheKey, now()->addMonth(), function () use ($file) {
$content = file_get_contents($file);
return HtmlValidator::validateCss($content);
});
$errors = collect($result['messages'])->where('type', '=', 'error');
if ($errors->count() > 0) {
$this->invalidCss = $result;
Cache::forever('css_is_valid', false);
} else {
Cache::forever('css_is_valid', true);
}
return $this;
}
/**
* Perform json validation on selected bundles
*/
private function validateHtml(): self
{
if ($this->option('no-html')) {
return $this;
}
$this->line('Validating <info>HTML</info>...');
$bundles = $this->bundles;
progress(
label: 'Validating... ',
steps: $bundles,
callback: fn (Bundle $bundle, $progress) => $this->handleBundleHtmlValidation($bundle, $progress)
);
return $this;
}
private function handleBundleHtmlValidation(Bundle $bundle, $progress)
{
$progress->label(sprintf('Validating %s ...', $bundle->getPath()));
$rendered = $bundle->render();
$changed = false;
foreach ($rendered as $path => $content) {
$result = HtmlValidator::validateHtml($content);
$errors = collect($result['messages'])->where('type', '=', 'error');
$valid = $errors->count() === 0;
$cacheKey = sprintf('html_is_valid_%s', Str::slug($path));
if (Cache::get($cacheKey) !== $valid) {
$changed = true;
Cache::forever($cacheKey, $valid);
}
if (!$valid) {
$this->invalidHtml[$path] = $result;
}
}
if ($changed) {
$bundle->touch();
}
}
/**
* Show a report in case of need
*/
private function showReport()
{
$this->showCssReport();
$this->showJsonReport();
$this->showHtmlReport();
}
/**
* Show a report in case of need
*/
private function showJsonReport()
{
if ($this->option('no-json')) {
return $this;
}
if (empty($this->invalidJson)) {
$this->info('Checked JSON files are valid');
} else {
$count = count($this->invalidJson);
$this->error(sprintf('%d invalid JSON %s reported', $count, Str::plural('file', $count)));
$this->newLine(2);
foreach ($this->invalidJson as $bundlePath => $data) {
$this->line($bundlePath);
$this->line(sprintf(' > In <comment>%s</comment>:', $data['metadata']));
foreach ($data['errors'] as $error) {
$this->line(sprintf(' - %s', $error['message']));
}
}
}
}
private function showCssReport()
{
if ($this->option('no-css')) {
return $this;
}
if (empty($this->invalidCss)) {
$this->info('Checked CSS files are valid');
} else {
$count = count($this->invalidCss);
$this->error(sprintf('%d CSS %s reported', $count, Str::plural('error', $count)));
$this->newLine();
$data = $this->invalidCss;
foreach ($data['messages'] as $message) {
$type = $message['type'];
$subType = $message['subType'] ?? $type;
$decoration = match ($subType) {
'error' => 'error',
'info' => 'comment',
'warning' => 'comment'
};
$this->newLine();
$this->line(sprintf(' > <%1$s>%2$s</%1$s>: %3$s', $decoration, $subType, $message['message']));
$this->line(' > > ' . $message['extract']);
}
}
}
private function showHtmlReport()
{
if ($this->option('no-html')) {
return $this;
}
if (empty($this->invalidHtml)) {
$this->info('Checked HTML files are valid');
} else {
$count = count($this->invalidHtml);
$this->error(sprintf('%d invalid HTML %s reported', $count, Str::plural('file', $count)));
$this->newLine(2);
foreach ($this->invalidHtml as $path => $data) {
$this->newLine(2);
$this->comment($path);
foreach ($data['messages'] as $message) {
$type = $message['type'];
$subType = $message['subType'] ?? $type;
$decoration = match ($subType) {
'error' => 'error',
'info' => 'comment',
'warning' => 'comment'
};
$this->newLine();
$this->line(sprintf(' > <%1$s>%2$s</%1$s>: %3$s', $decoration, $subType, $message['message']));
$this->line(' > > ' . $message['extract']);
}
}
}
}
}