Skip to content

Commit bda11dc

Browse files
committed
fix: avoid registering command aliases in composer provider
1 parent 139ec4d commit bda11dc

4 files changed

Lines changed: 30 additions & 27 deletions

File tree

src/Composer/Capability/DevToolsCommandProvider.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,22 +22,37 @@
2222
use Composer\Plugin\Capability\CommandProvider;
2323
use FastForward\DevTools\Composer\Command\ProxyCommand;
2424
use FastForward\DevTools\Console\DevTools;
25-
use Symfony\Component\Console\Command\Command;
2625

2726
/**
2827
* Provides a registry of custom dev-tools commands mapped for Composer integration.
2928
* This capability struct MUST implement the defined `CommandProvider`.
3029
*/
3130
final class DevToolsCommandProvider implements CommandProvider
3231
{
32+
/**
33+
* @var string the namespace prefix for dev-tools console commands to be registered as Composer commands
34+
*/
35+
private const string COMMAND_NAMESPACE = 'FastForward\DevTools\Console\Command';
36+
3337
/**
3438
* {@inheritDoc}
3539
*/
3640
public function getCommands()
3741
{
38-
return array_map(
39-
static fn(Command $command): ProxyCommand => new ProxyCommand($command),
40-
iterator_to_array(DevTools::create()->getCommands()),
41-
);
42+
$commands = [];
43+
44+
foreach (DevTools::create()->all() as $registeredName => $command) {
45+
if ($registeredName !== $command->getName()) {
46+
continue;
47+
}
48+
49+
if (! str_starts_with($command::class, self::COMMAND_NAMESPACE)) {
50+
continue;
51+
}
52+
53+
$commands[] = new ProxyCommand($command);
54+
}
55+
56+
return $commands;
4257
}
4358
}

src/Console/DevTools.php

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -53,13 +53,12 @@ final class DevTools extends Application
5353
*
5454
* @param CommandLoaderInterface $commandLoader the command loader responsible for providing command instances
5555
*/
56-
public function __construct(
57-
private readonly CommandLoaderInterface $commandLoader
58-
) {
56+
public function __construct(CommandLoaderInterface $commandLoader)
57+
{
5958
parent::__construct('Fast Forward Dev Tools');
6059

61-
$this->setDefaultCommand('standards');
62-
$this->setCommandLoader($this->commandLoader);
60+
$this->setDefaultCommand('dev-tools:standards');
61+
$this->setCommandLoader($commandLoader);
6362
}
6463

6564
/**
@@ -73,18 +72,6 @@ public function getHelp(): string
7372
return self::LOGO . "\n\n" . parent::getHelp();
7473
}
7574

76-
/**
77-
* Retrieves the shared DevTools service container.
78-
*
79-
* @return iterable
80-
*/
81-
public function getCommands(): iterable
82-
{
83-
foreach ($this->commandLoader->getNames() as $name) {
84-
yield $name => $this->commandLoader->get($name);
85-
}
86-
}
87-
8875
/**
8976
* Create DevTools instance from container.
9077
*

tests/Composer/Capability/DevToolsCommandProviderTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ protected function setUp(): void
5757
->willReturn($this->devTools->reveal())
5858
->shouldBeCalledOnce();
5959

60-
$this->devTools->getCommands()
60+
$this->devTools->all()
6161
->willReturn([])->shouldBeCalledOnce();
6262

6363
$this->commandProvider = new DevToolsCommandProvider();
@@ -90,8 +90,10 @@ public function getCommandsWillReturnComposerProxyCommandsForRegisteredSymfonyCo
9090
$symfonyCommand->setHelp('');
9191
$symfonyCommand->setHidden(false);
9292

93-
$this->devTools->getCommands()
94-
->willReturn([$symfonyCommand])
93+
$this->devTools->all()
94+
->willReturn([
95+
'agents' => $symfonyCommand,
96+
])
9597
->shouldBeCalledOnce();
9698

9799
$commands = $this->commandProvider->getCommands();

tests/Console/CommandLoader/DevToolsCommandLoaderTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public function constructorWillRegisterOnlyInstantiableCommands(): void
108108
* @return void
109109
*/
110110
#[Test]
111-
public function constructorWillRegisterCommandAliasesFromAsCommandAttribute(): void
111+
public function constructorWillRegisterPrimaryCommandFromAsCommandAttribute(): void
112112
{
113113
$commandDirectory = \dirname(__DIR__, 3) . '/src/Console/Command';
114114

@@ -134,7 +134,6 @@ public function constructorWillRegisterCommandAliasesFromAsCommandAttribute(): v
134134
$loader = new DevToolsCommandLoader($this->finderFactory->reveal(), $this->container->reveal());
135135

136136
self::assertTrue($loader->has('dev-tools:sync'));
137-
self::assertTrue($loader->has('sync'));
138137
}
139138

140139
/**

0 commit comments

Comments
 (0)