Skip to content

Commit facdc25

Browse files
authored
feat(console): allow dynamic arguments for specific console commands (#1322)
1 parent dba57d8 commit facdc25

File tree

5 files changed

+48
-2
lines changed

5 files changed

+48
-2
lines changed

packages/console/src/ConsoleCommand.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ public function __construct(
2929

3030
/** @var class-string<\Tempest\Console\CompletesConsoleCommand>|null */
3131
public readonly ?string $complete = null,
32+
33+
public readonly bool $allowDynamicArguments = false,
3234
) {}
3335

3436
public function setHandler(MethodReflector $handler): self

packages/console/src/Middleware/ValidateNamedArgumentsMiddleware.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ final class ValidateNamedArgumentsMiddleware implements ConsoleMiddleware
1919
{
2020
public function __invoke(Invocation $invocation, ConsoleMiddlewareCallable $next): ExitCode|int
2121
{
22+
if ($invocation->consoleCommand->allowDynamicArguments) {
23+
return $next($invocation);
24+
}
25+
2226
$allowedParameterNames = arr($invocation->consoleCommand->getArgumentDefinitions())
2327
->flatMap(function (ConsoleArgumentDefinition $definition) {
2428
return [$definition->name, ...$definition->aliases];

packages/core/src/Commands/InstallCommand.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,13 @@ public function __construct(
2424
private Container $container,
2525
) {}
2626

27-
#[ConsoleCommand(name: 'install', description: 'Applies the specified installer', middleware: [ForceMiddleware::class])]
28-
public function __invoke(?string $installer = null): void
27+
#[ConsoleCommand(
28+
name: 'install',
29+
description: 'Applies the specified installer',
30+
middleware: [ForceMiddleware::class],
31+
allowDynamicArguments: true,
32+
)]
33+
public function __invoke(?string $installer = null, bool $_tailwind = false): void
2934
{
3035
$installer = $this->resolveInstaller($installer);
3136

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<?php
2+
3+
namespace Tests\Tempest\Fixtures\Commands;
4+
5+
use Tempest\Console\ConsoleCommand;
6+
use Tempest\Console\HasConsole;
7+
use Tempest\Console\Input\ConsoleArgumentBag;
8+
9+
final class DynamicParamsCommand
10+
{
11+
use HasConsole;
12+
13+
public function __construct(
14+
private readonly ConsoleArgumentBag $consoleArgumentBag,
15+
) {}
16+
17+
#[ConsoleCommand(allowDynamicArguments: true)]
18+
public function __invoke(): void
19+
{
20+
$dynamic = $this->consoleArgumentBag->get('dynamic');
21+
22+
$this->console->info($dynamic ? 'yes' : 'no');
23+
}
24+
}

tests/Integration/Console/Middleware/ValidateNamedArgumentsMiddlewareTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,15 @@ public function test_invalid_parameters_throw_exception(): void
1818
->assertDoesNotContain('help')
1919
->assertDoesNotContain('interaction');
2020
}
21+
22+
public function test_command_with_dynamic_parameters(): void
23+
{
24+
$this->console
25+
->call('dynamic:params --dynamic')
26+
->assertContains('yes');
27+
28+
$this->console
29+
->call('dynamic:params')
30+
->assertContains('no');
31+
}
2132
}

0 commit comments

Comments
 (0)