Skip to content

Commit ab60e86

Browse files
committed
feat: allow modern commands to opt out of the spark header
1 parent 102bc17 commit ab60e86

14 files changed

Lines changed: 267 additions & 6 deletions

File tree

‎system/CLI/AbstractCommand.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ abstract class AbstractCommand
4646
private readonly array $aliases;
4747

4848
private readonly bool $hidden;
49+
private readonly bool $headerless;
4950

5051
/**
5152
* @var list<non-empty-string>
@@ -145,6 +146,7 @@ public function __construct(private readonly Commands $commands)
145146
$this->group = $attribute->group;
146147
$this->aliases = $attribute->aliases;
147148
$this->hidden = $attribute->hidden;
149+
$this->headerless = $attribute->headerless;
148150

149151
$this->configure();
150152
$this->provideDefaultOptions();
@@ -185,6 +187,11 @@ public function isHidden(): bool
185187
return $this->hidden;
186188
}
187189

190+
public function isHeaderless(): bool
191+
{
192+
return $this->headerless;
193+
}
194+
188195
/**
189196
* @return list<non-empty-string>
190197
*/

‎system/CLI/Attributes/Command.php‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ public function __construct(
4545
public string $group = '',
4646
array $aliases = [],
4747
public bool $hidden = false,
48+
public bool $headerless = false,
4849
) {
4950
if ($name === '') {
5051
throw new LogicException(lang('Commands.emptyCommandName'));

‎system/CLI/Commands.php‎

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
* Command discovery and execution class.
2929
*
3030
* @phpstan-type legacy_commands array<string, array{class: class-string<BaseCommand>, file: string, group: string, description: string}>
31-
* @phpstan-type modern_commands array<string, array{class: class-string<AbstractCommand>, file: string, group: string, description: string, aliases: list<string>, hidden: bool}>
31+
* @phpstan-type modern_commands array<string, array{class: class-string<AbstractCommand>, file: string, group: string, description: string, aliases: list<string>, hidden: bool, headerless: bool}>
3232
*/
3333
class Commands
3434
{
@@ -214,6 +214,20 @@ public function isHiddenCommand(string $name): bool
214214
return $resolved !== null && $this->modernCommands[$resolved]['hidden'];
215215
}
216216

217+
/**
218+
* Checks whether the given command name or alias resolves to a headerless modern command that no legacy command shadows.
219+
*/
220+
public function isHeaderlessCommand(string $name): bool
221+
{
222+
if (isset($this->commands[$name])) {
223+
return false;
224+
}
225+
226+
$resolved = $this->resolveCommand($name);
227+
228+
return $resolved !== null && $this->modernCommands[$resolved]['headerless'];
229+
}
230+
217231
/**
218232
* @return ($legacy is true ? BaseCommand : AbstractCommand)
219233
*
@@ -295,7 +309,7 @@ public function discoverCommands()
295309
ksort($this->modernCommands);
296310

297311
foreach (array_keys(array_intersect_key($this->commands, $this->modernCommands)) as $name) {
298-
CLI::write(
312+
CLI::error(
299313
CLI::wrap(
300314
lang('Commands.duplicateCommandName', [
301315
$name,
@@ -481,6 +495,7 @@ private function registerModernCommand(ReflectionClass $class, string $file): vo
481495
'description' => $attribute->description,
482496
'aliases' => $attribute->aliases,
483497
'hidden' => $attribute->hidden,
498+
'headerless' => $attribute->headerless,
484499
];
485500
}
486501
}

‎system/CLI/Console.php‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ public function run(array $tokens = [])
4848

4949
$arguments = $parser->getArguments();
5050
$this->options = $parser->getOptions();
51-
52-
$this->showHeader($this->hasParameterOption(['no-header']));
51+
$noHeader = $this->hasParameterOption(['no-header']);
5352

5453
if ($this->hasParameterOption(['help', 'h'])) {
5554
if ($arguments === []) {
@@ -69,6 +68,8 @@ public function run(array $tokens = [])
6968

7069
$this->command = array_shift($arguments) ?? self::DEFAULT_COMMAND;
7170

71+
$this->showHeader($noHeader || $commands->isHeaderlessCommand($this->command));
72+
7273
if (
7374
$this->isInteractive()
7475
&& ! $commands->hasLegacyCommand($this->command)
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Commands\Modern;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
use CodeIgniter\CLI\CLI;
19+
20+
#[Command(
21+
name: 'test:headerless',
22+
description: 'Fixture command exercising commands without the header.',
23+
group: 'Fixtures',
24+
aliases: ['test:quiet'],
25+
headerless: true,
26+
)]
27+
final class HeaderlessCommand extends AbstractCommand
28+
{
29+
protected function execute(array $arguments, array $options): int
30+
{
31+
CLI::write('Ran test:headerless.');
32+
33+
return EXIT_SUCCESS;
34+
}
35+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of CodeIgniter 4 framework.
7+
*
8+
* (c) CodeIgniter Foundation <admin@codeigniter.com>
9+
*
10+
* For the full copyright and license information, please view
11+
* the LICENSE file that was distributed with this source code.
12+
*/
13+
14+
namespace Tests\Support\Duplicates;
15+
16+
use CodeIgniter\CLI\AbstractCommand;
17+
use CodeIgniter\CLI\Attributes\Command;
18+
19+
/**
20+
* Headerless modern fixture shadowed by the legacy command of the same name.
21+
*
22+
* @internal
23+
*/
24+
#[Command(
25+
name: 'dup:test',
26+
description: 'Headerless modern fixture that collides with a legacy command of the same name.',
27+
group: 'Duplicates',
28+
headerless: true,
29+
)]
30+
final class HeaderlessDuplicateModern extends AbstractCommand
31+
{
32+
protected function execute(array $arguments, array $options): int
33+
{
34+
return EXIT_SUCCESS;
35+
}
36+
}

‎tests/system/CLI/AbstractCommandTest.php‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
use ReflectionClass;
3939
use ReflectionProperty;
4040
use Tests\Support\Commands\Modern\AppAboutCommand;
41+
use Tests\Support\Commands\Modern\HeaderlessCommand;
4142
use Tests\Support\Commands\Modern\HiddenCommand;
4243
use Tests\Support\Commands\Modern\InteractFixtureCommand;
4344
use Tests\Support\Commands\Modern\InteractiveStateProbeCommand;
@@ -84,6 +85,7 @@ public function testConstructorSetsNeededProperties(): void
8485
$this->assertSame($attribute->description, $command->getDescription());
8586
$this->assertSame($attribute->group, $command->getGroup());
8687
$this->assertSame($attribute->hidden, $command->isHidden());
88+
$this->assertSame($attribute->headerless, $command->isHeaderless());
8789
$this->assertSame($commands, $command->getCommandRunner());
8890
$this->assertSame('help [options] [--] [<command_name>]', $command->getUsages()[0]);
8991
}
@@ -93,6 +95,11 @@ public function testHiddenCommandReportsItself(): void
9395
$this->assertTrue((new HiddenCommand(new Commands()))->isHidden());
9496
}
9597

98+
public function testHeaderlessCommandReportsItself(): void
99+
{
100+
$this->assertTrue((new HeaderlessCommand(new Commands()))->isHeaderless());
101+
}
102+
96103
public function testCommandRequiresCommandAttribute(): void
97104
{
98105
$this->expectException(LogicException::class);

‎tests/system/CLI/Attributes/CommandTest.php‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,19 @@ public function testAttributeAllowsOmittedDescriptionAndGroup(): void
4343
$this->assertSame('', $command->group);
4444
$this->assertSame([], $command->aliases);
4545
$this->assertFalse($command->hidden);
46+
$this->assertFalse($command->headerless);
4647
}
4748

4849
public function testAttributeExposesHidden(): void
4950
{
5051
$this->assertTrue((new Command(name: 'app:about', hidden: true))->hidden);
5152
}
5253

54+
public function testAttributeExposesHeaderless(): void
55+
{
56+
$this->assertTrue((new Command(name: 'app:about', headerless: true))->headerless);
57+
}
58+
5359
public function testAttributeExposesAliases(): void
5460
{
5561
$command = new Command(name: 'app:about', aliases: ['app:ab', 'ab']);

‎tests/system/CLI/CommandsTest.php‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
use CodeIgniter\Exceptions\LogicException;
2323
use CodeIgniter\Log\Logger;
2424
use CodeIgniter\Test\CIUnitTestCase;
25+
use CodeIgniter\Test\Filters\CITestStreamFilter;
2526
use CodeIgniter\Test\ReflectionHelper;
2627
use CodeIgniter\Test\StreamFilterTrait;
2728
use Config\Services;
@@ -37,6 +38,7 @@
3738
use Tests\Support\Commands\Modern\AppAboutCommand;
3839
use Tests\Support\Duplicates\DuplicateLegacy;
3940
use Tests\Support\Duplicates\DuplicateModern;
41+
use Tests\Support\Duplicates\HeaderlessDuplicateModern;
4042
use Tests\Support\Duplicates\HiddenDuplicateModern;
4143
use Tests\Support\InvalidCommands\AliasClashCommand;
4244
use Tests\Support\InvalidCommands\AliasSecondClashCommand;
@@ -283,6 +285,8 @@ public function testDiscoveryWarnsWhenSameCommandNameExistsInBothRegistries(): v
283285
CLI::getWidth(),
284286
);
285287

288+
CITestStreamFilter::removeOutputFilter();
289+
286290
$commands = new Commands();
287291

288292
$this->assertSame("\n{$message}\n", $this->getUndecoratedBuffer());
@@ -392,6 +396,36 @@ public function testIsHiddenCommandIsFalseWhenLegacyCommandShadowsIt(): void
392396
$this->assertFalse((new Commands())->isHiddenCommand('dup:test'));
393397
}
394398

399+
public function testHeaderlessCommandIsRegisteredWithItsFlag(): void
400+
{
401+
$commands = (new Commands())->getModernCommands();
402+
403+
$this->assertTrue($commands['test:headerless']['headerless']);
404+
$this->assertFalse($commands['fixture:aliased']['headerless']);
405+
}
406+
407+
public function testIsHeaderlessCommand(): void
408+
{
409+
$commands = new Commands();
410+
411+
$this->assertTrue($commands->isHeaderlessCommand('test:headerless'));
412+
$this->assertTrue($commands->isHeaderlessCommand('test:quiet'));
413+
$this->assertFalse($commands->isHeaderlessCommand('fixture:aliased'));
414+
$this->assertFalse($commands->isHeaderlessCommand('fixture:alias'));
415+
$this->assertFalse($commands->isHeaderlessCommand('app:info'));
416+
$this->assertFalse($commands->isHeaderlessCommand('app:unknown'));
417+
}
418+
419+
public function testIsHeaderlessCommandIsFalseWhenLegacyCommandShadowsIt(): void
420+
{
421+
$this->injectFixtureLocator([
422+
DuplicateLegacy::class => SUPPORTPATH . 'Duplicates/DuplicateLegacy.php',
423+
HeaderlessDuplicateModern::class => SUPPORTPATH . 'Duplicates/HeaderlessDuplicateModern.php',
424+
]);
425+
426+
$this->assertFalse((new Commands())->isHeaderlessCommand('dup:test'));
427+
}
428+
395429
public function testHiddenCommandRunsByName(): void
396430
{
397431
command('fixture:hidden');

‎tests/system/CLI/ConsoleTest.php‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
use CodeIgniter\Test\Mock\MockInputOutput;
2525
use CodeIgniter\Test\StreamFilterTrait;
2626
use PHPUnit\Framework\Attributes\CoversClass;
27+
use PHPUnit\Framework\Attributes\DataProvider;
2728
use PHPUnit\Framework\Attributes\Group;
2829

2930
/**
@@ -99,6 +100,70 @@ public function testHeaderDoesNotShowOnNoHeader(): void
99100
);
100101
}
101102

103+
#[DataProvider('provideHeaderlessCommandOmitsHeader')]
104+
public function testHeaderlessCommandOmitsHeader(string $command): void
105+
{
106+
$this->initializeConsole($command);
107+
(new Console())->run();
108+
109+
$this->assertSame(
110+
<<<'EOT'
111+
112+
Ran test:headerless.
113+
114+
EOT,
115+
$this->getStreamFilterBuffer(),
116+
);
117+
}
118+
119+
/**
120+
* @return iterable<string, array{string}>
121+
*/
122+
public static function provideHeaderlessCommandOmitsHeader(): iterable
123+
{
124+
yield 'name' => ['test:headerless'];
125+
126+
yield 'alias' => ['test:quiet'];
127+
}
128+
129+
/**
130+
* @param list<string> $tokens
131+
*/
132+
#[DataProvider('provideHelpForHeaderlessCommandShowsHeader')]
133+
public function testHelpForHeaderlessCommandShowsHeader(array $tokens): void
134+
{
135+
$this->initializeConsole(...$tokens);
136+
(new Console())->run();
137+
138+
$this->assertStringContainsString(
139+
sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION),
140+
$this->getStreamFilterBuffer(),
141+
);
142+
}
143+
144+
/**
145+
* @return iterable<string, array{list<string>}>
146+
*/
147+
public static function provideHelpForHeaderlessCommandShowsHeader(): iterable
148+
{
149+
yield 'help command' => [['help', 'test:headerless']];
150+
151+
yield 'help option' => [['test:headerless', '--help']];
152+
153+
yield 'help shortcut on alias' => [['test:quiet', '-h']];
154+
}
155+
156+
public function testNoHeaderOptionAppliesAlongsideHelpOption(): void
157+
{
158+
$this->initializeConsole('env', '--help', '--no-header');
159+
(new Console())->run();
160+
161+
$this->assertStringNotContainsString(
162+
sprintf('CodeIgniter v%s Command Line Tool', CodeIgniter::CI_VERSION),
163+
$this->getStreamFilterBuffer(),
164+
);
165+
}
166+
102167
public function testRun(): void
103168
{
104169
$this->initializeConsole();

0 commit comments

Comments
 (0)