49 lines
1.5 KiB
PHP
49 lines
1.5 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
declare(strict_types=1);
|
||
|
|
|
||
|
|
namespace PhpQml\Bridge\Maker\Support;
|
||
|
|
|
||
|
|
use Symfony\Bundle\MakerBundle\ConsoleStyle;
|
||
|
|
use Symfony\Component\Console\Input\InputInterface;
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Shared interactive name prompt for the bridge makers.
|
||
|
|
*
|
||
|
|
* Every `make:bridge:*` maker takes a single CamelCase `name` argument
|
||
|
|
* and re-implemented the same "prompt, trim, ucfirst, reject empty"
|
||
|
|
* closure inline. This collapses that into one call site so the empty-
|
||
|
|
* argument and validation behaviour stay in lockstep across makers.
|
||
|
|
*/
|
||
|
|
final class NameInput
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* Fill the named argument from an interactive prompt if it isn't set.
|
||
|
|
*
|
||
|
|
* The closure-based validator throws `\RuntimeException` on empty input,
|
||
|
|
* which Maker-bundle's `ConsoleStyle::ask()` interprets as "render
|
||
|
|
* error, re-prompt" rather than aborting — same behaviour as the
|
||
|
|
* inline closures it replaces.
|
||
|
|
*/
|
||
|
|
public static function askOrFail(
|
||
|
|
InputInterface $input,
|
||
|
|
ConsoleStyle $io,
|
||
|
|
string $argument,
|
||
|
|
string $question,
|
||
|
|
string $errorMessage = 'Name cannot be empty.',
|
||
|
|
): void {
|
||
|
|
if (null !== $input->getArgument($argument)) {
|
||
|
|
return;
|
||
|
|
}
|
||
|
|
|
||
|
|
$value = $io->ask($question, null, static function (?string $v) use ($errorMessage): string {
|
||
|
|
if (null === $v || '' === trim($v)) {
|
||
|
|
throw new \RuntimeException($errorMessage);
|
||
|
|
}
|
||
|
|
|
||
|
|
return ucfirst(trim($v));
|
||
|
|
});
|
||
|
|
$input->setArgument($argument, $value);
|
||
|
|
}
|
||
|
|
}
|