-
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #841 from hydephp/publication-type-validation
Add a publication type validation command and helpers
- Loading branch information
Showing
7 changed files
with
648 additions
and
0 deletions.
There are no files selected for viewing
142 changes: 142 additions & 0 deletions
142
packages/publications/src/Commands/ValidatePublicationTypesCommand.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,142 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Hyde\Publications\Commands; | ||
|
||
use function array_filter; | ||
use function basename; | ||
use function dirname; | ||
use function glob; | ||
use Hyde\Hyde; | ||
use Hyde\Publications\PublicationService; | ||
use function implode; | ||
use InvalidArgumentException; | ||
use function json_encode; | ||
use LaravelZero\Framework\Commands\Command; | ||
use function memory_get_peak_usage; | ||
use function microtime; | ||
use function next; | ||
use function round; | ||
use function sprintf; | ||
|
||
/** | ||
* Hyde Command to validate all publication schema file.. | ||
* | ||
* @see \Hyde\Publications\Testing\Feature\ValidatePublicationTypesCommandTest | ||
* | ||
* @internal This command is not part of the public API and may change without notice. | ||
*/ | ||
class ValidatePublicationTypesCommand extends ValidatingCommand | ||
{ | ||
protected const CROSS_MARK = 'x'; | ||
|
||
/** @var string */ | ||
protected $signature = 'validate:publicationTypes {--json : Display results as JSON.}'; | ||
|
||
/** @var string */ | ||
protected $description = 'Validate all publication schema files.'; | ||
|
||
protected array $results = []; | ||
|
||
public function safeHandle(): int | ||
{ | ||
$timeStart = microtime(true); | ||
|
||
if (! $this->option('json')) { | ||
$this->title('Validating publication schemas!'); | ||
} | ||
|
||
$this->validateSchemaFiles(); | ||
|
||
if ($this->option('json')) { | ||
$this->outputJson(); | ||
} else { | ||
$this->displayResults(); | ||
$this->outputSummary($timeStart); | ||
} | ||
|
||
if ($this->countErrors() > 0) { | ||
return Command::FAILURE; | ||
} | ||
|
||
return Command::SUCCESS; | ||
} | ||
|
||
protected function validateSchemaFiles(): void | ||
{ | ||
/** Uses the same glob pattern as {@see PublicationService::getSchemaFiles()} */ | ||
$schemaFiles = glob(Hyde::path(Hyde::getSourceRoot()).'/*/schema.json'); | ||
|
||
if (empty($schemaFiles)) { | ||
throw new InvalidArgumentException('No publication types to validate!'); | ||
} | ||
|
||
foreach ($schemaFiles as $schemaFile) { | ||
$publicationName = basename(dirname($schemaFile)); | ||
$this->results[$publicationName] = PublicationService::validateSchemaFile($publicationName, false); | ||
} | ||
} | ||
|
||
protected function displayResults(): void | ||
{ | ||
foreach ($this->results as $name => $errors) { | ||
$this->infoComment('Validating schema file for', $name); | ||
|
||
$schemaErrors = $errors['schema']; | ||
if (empty($schemaErrors)) { | ||
$this->line('<info> No top-level schema errors found</info>'); | ||
} else { | ||
$this->line(sprintf(' <fg=red>Found %s top-level schema errors:</>', count($schemaErrors))); | ||
foreach ($schemaErrors as $error) { | ||
$this->line(sprintf(' <fg=red>%s</> <comment>%s</comment>', self::CROSS_MARK, implode(' ', $error))); | ||
} | ||
} | ||
|
||
$schemaFields = $errors['fields']; | ||
if (empty(array_filter($schemaFields))) { | ||
$this->line('<info> No field-level schema errors found</info>'); | ||
} else { | ||
$this->newLine(); | ||
$this->line(sprintf(' <fg=red>Found errors in %s field definitions:</>', count($schemaFields))); | ||
foreach ($schemaFields as $fieldNumber => $fieldErrors) { | ||
$this->line(sprintf(' <fg=cyan>Field #%s:</>', $fieldNumber + 1)); | ||
foreach ($fieldErrors as $error) { | ||
$this->line(sprintf(' <fg=red>%s</> <comment>%s</comment>', self::CROSS_MARK, | ||
implode(' ', $error))); | ||
} | ||
} | ||
} | ||
|
||
if (next($this->results)) { | ||
$this->newLine(); | ||
} | ||
} | ||
} | ||
|
||
protected function outputSummary($timeStart): void | ||
{ | ||
$this->newLine(); | ||
$this->info(sprintf('All done in %sms using %sMB peak memory!', | ||
round((microtime(true) - $timeStart) * 1000), | ||
round(memory_get_peak_usage() / 1024 / 1024) | ||
)); | ||
} | ||
|
||
protected function outputJson(): void | ||
{ | ||
$this->output->writeln(json_encode($this->results, JSON_PRETTY_PRINT)); | ||
} | ||
|
||
protected function countErrors(): int | ||
{ | ||
$errors = 0; | ||
|
||
foreach ($this->results as $results) { | ||
$errors += count($results['schema']); | ||
$errors += count(array_filter($results['fields'])); | ||
} | ||
|
||
return $errors; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.