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

81 lines
2.2 KiB
PHP
Raw Normal View History

2024-04-26 12:50:38 +02:00
<?php
namespace App\Console\Commands\Bundle;
use App\Classes\Bundle;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use JsonSchema\Constraints\Constraint;
use JsonSchema\Validator;
class Validate extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'bundle:validate { path? : Specific bundle to repair }';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Validate bundles metadata';
/**
* Execute the console command.
*/
public function handle()
{
$path = $this->argument('path') ?? '/';
$bundles = Bundle::findBundles(Storage::disk(env('CONTENT_DISK')), $path, true);
$validators = [
'index' => json_decode(file_get_contents(resource_path('schemas/index.json'))),
'metadata' => json_decode(file_get_contents(resource_path('schemas/metadata.json'))),
];
$allValid = true;
foreach ($bundles as $bundle) {
$this->output->write(sprintf('Validating %s... ', $bundle->getPath()));
$isValid = false;
foreach ($validators as $file => $schema) {
$data = json_decode(Storage::disk(env('CONTENT_DISK'))->get($bundle->metadata($file)->getFilename()) ?? '{}');
$validator = new Validator();
$validator->validate($data, $schema, Constraint::CHECK_MODE_APPLY_DEFAULTS);
if ($validator->isValid()) {
$isValid = true;
} else {
$isValid = false;
$allValid = false;
$this->error(sprintf('%s INVALID', $file));
foreach ($validator->getErrors() as $error) {
$this->line(" $error[message]");
}
}
}
if ($isValid) {
$this->info('OK');
}
}
$this->newLine();
if ($allValid) {
$this->info('All bundles are valid');
} else {
$this->comment('Errors have been reported');
}
}
}