mirror of
https://github.com/Respect/Validation.git
synced 2026-03-14 14:25:45 +01:00
Replace hardcoded validator class lists with a declarative #[Mixin] attribute and extract the mixin generation logic into a reusable CodeGen namespace under src-dev/CodeGen/. The new MixinGenerator discovers prefix definitions and filtering rules by scanning #[Mixin] attributes on the target namespace's classes, removing the need for hardcoded configuration. It supports configurable interface types (Builder for __callStatic, Chain for __call) with custom suffixes, return types, and root extends. This is the first step toward extracting the code generation into a standalone package that can map __call/__callStatic to any namespace, possibly for Respect/StringFormatter and any kind of project in the future.
122 lines
3.6 KiB
PHP
122 lines
3.6 KiB
PHP
<?php
|
|
|
|
/*
|
|
* SPDX-License-Identifier: MIT
|
|
* SPDX-FileCopyrightText: (c) Respect Project Contributors
|
|
* SPDX-FileContributor: Alexandre Gomes Gaigalas <alganet@gmail.com>
|
|
* SPDX-FileContributor: Henrique Moody <henriquemoody@gmail.com>
|
|
*/
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Respect\Dev\Commands;
|
|
|
|
use Respect\Dev\CodeGen\InterfaceConfig;
|
|
use Respect\Dev\CodeGen\MethodBuilder;
|
|
use Respect\Dev\CodeGen\MixinGenerator;
|
|
use Respect\Dev\Differ\ConsoleDiffer;
|
|
use Respect\Dev\Differ\Item;
|
|
use Respect\Validation\Mixins\Chain;
|
|
use Respect\Validation\Validator;
|
|
use Respect\Validation\ValidatorBuilder;
|
|
use Symfony\Component\Console\Attribute\AsCommand;
|
|
use Symfony\Component\Console\Command\Command;
|
|
use Symfony\Component\Console\Input\InputInterface;
|
|
use Symfony\Component\Console\Output\OutputInterface;
|
|
|
|
use function count;
|
|
use function dirname;
|
|
use function file_get_contents;
|
|
use function file_put_contents;
|
|
use function is_file;
|
|
use function is_readable;
|
|
use function sprintf;
|
|
|
|
#[AsCommand(
|
|
name: 'lint:mixin',
|
|
description: 'Apply linters to the generated mixin interfaces',
|
|
)]
|
|
final class LintMixinCommand extends Command
|
|
{
|
|
public function __construct(
|
|
private readonly ConsoleDiffer $differ,
|
|
) {
|
|
parent::__construct();
|
|
}
|
|
|
|
protected function configure(): void
|
|
{
|
|
$this->addOption(
|
|
'fix',
|
|
null,
|
|
null,
|
|
'Automatically fix files with issues.',
|
|
);
|
|
}
|
|
|
|
protected function execute(InputInterface $input, OutputInterface $output): int
|
|
{
|
|
$srcDir = dirname(__DIR__, 2) . '/src';
|
|
|
|
$generator = new MixinGenerator(
|
|
sourceDir: $srcDir . '/Validators',
|
|
sourceNamespace: 'Respect\\Validation\\Validators',
|
|
outputDir: $srcDir . '/Mixins',
|
|
outputNamespace: 'Respect\\Validation\\Mixins',
|
|
methodBuilder: new MethodBuilder(
|
|
excludedTypePrefixes: ['Sokil', 'Egulias'],
|
|
excludedTypeNames: ['finfo'],
|
|
),
|
|
interfaces: [
|
|
new InterfaceConfig(
|
|
suffix: 'Builder',
|
|
returnType: Chain::class,
|
|
static: true,
|
|
),
|
|
new InterfaceConfig(
|
|
suffix: 'Chain',
|
|
returnType: Chain::class,
|
|
rootExtends: [Validator::class],
|
|
rootComment: '@mixin ValidatorBuilder',
|
|
rootUses: [ValidatorBuilder::class],
|
|
),
|
|
],
|
|
);
|
|
|
|
$files = $generator->generate();
|
|
|
|
$updatableFiles = [];
|
|
foreach ($files as $filename => $content) {
|
|
$existingContent = '';
|
|
if (is_file($filename) && is_readable($filename)) {
|
|
$existingContent = file_get_contents($filename) ?: '';
|
|
}
|
|
|
|
if ($content === $existingContent) {
|
|
continue;
|
|
}
|
|
|
|
$updatableFiles[$filename] = $content;
|
|
$output->writeln($this->differ->diff(
|
|
new Item($filename, $existingContent),
|
|
new Item($filename, $content),
|
|
));
|
|
}
|
|
|
|
if ($updatableFiles === []) {
|
|
$output->writeln('<info>No changes needed.</info>');
|
|
} else {
|
|
$output->writeln(sprintf('<comment>Changes needed in %d files.</comment>', count($updatableFiles)));
|
|
}
|
|
|
|
if ($updatableFiles !== [] && !$input->getOption('fix')) {
|
|
return Command::FAILURE;
|
|
}
|
|
|
|
foreach ($updatableFiles as $filename => $content) {
|
|
file_put_contents($filename, $content);
|
|
}
|
|
|
|
return Command::SUCCESS;
|
|
}
|
|
}
|