Skip to content

Commit 038a783

Browse files
Relax auto-discoverable command file/class naming convention (#6153)
* Relax the file/class naming convention * Adapt test fixtures & documentation
1 parent 4e7fed6 commit 038a783

File tree

6 files changed

+11
-11
lines changed

6 files changed

+11
-11
lines changed

docs/commands.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ Such commands are auto-discovered by their class PSR4 namespace and class/file n
9090
}
9191
```
9292
then the Drush global commands class namespace should be `My\Custom\Library\Drush\Commands` and the class file should be located under the `src/Drush/Commands` directory.
93-
* The class and file name ends with `*DrushCommands`, e.g. `FooDrushCommands`.
93+
* The class and file name ends with `*Commands`, e.g. `FooCommands`.
9494

9595
Auto-discovered commandfiles should declare their Drush version compatibility via a `conflict` directive. For example, a Composer-managed site-wide command that works with both Drush 11 and Drush 12 might contain something similar to the following in its composer.json file:
9696
```json

src/Runtime/ServiceManager.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public function discoverPsr4Commands(): array
179179
{
180180
$classes = (new RelativeNamespaceDiscovery($this->autoloader))
181181
->setRelativeNamespace('Drush\Commands')
182-
->setSearchPattern('/.*DrushCommands\.php$/')
182+
->setSearchPattern('/.*Commands\.php$/')
183183
->getClasses();
184184

185185
return array_filter($classes, function (string $class): bool {

tests/fixtures/lib/Drush/Commands/CustomDrushCommands.php renamed to tests/fixtures/lib/Drush/Commands/CustomCommands.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
namespace Custom\Library\Drush\Commands;
44

55
use Drush\Commands\DrushCommands;
6+
use Drush\Attributes as CLI;
67

7-
class CustomDrushCommands extends DrushCommands
8+
class CustomCommands extends DrushCommands
89
{
910
/**
1011
* Auto-discoverable custom command. Used for Drush testing.
11-
*
12-
* @command custom_cmd
13-
* @hidden
1412
*/
13+
#[CLI\Command(name: 'custom_cmd')]
14+
#[CLI\Help(hidden: true)]
1515
public function customCommand(): void
1616
{
1717
$this->io()->note('Hello world!');

tests/fixtures/lib/Drush/Commands/ExampleAttributesDrushCommands.php renamed to tests/fixtures/lib/Drush/Commands/ExampleAttributesCommands.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
use Symfony\Component\Console\Completion\CompletionInput;
1313
use Symfony\Component\Console\Completion\CompletionSuggestions;
1414

15-
class ExampleAttributesDrushCommands extends DrushCommands
15+
class ExampleAttributesCommands extends DrushCommands
1616
{
1717
const ARITHMATIC = 'test:arithmatic';
1818
const ECHO = 'my:echo';

tests/fixtures/lib/Drush/Commands/StaticFactoryDrushCommands.php renamed to tests/fixtures/lib/Drush/Commands/StaticFactoryCommands.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
* in Drush/Commands directory + namespace, relative to some entry in
1313
* the library's `autoload` section in its composer.json file.
1414
*/
15-
class StaticFactoryDrushCommands extends DrushCommands
15+
class StaticFactoryCommands extends DrushCommands
1616
{
1717
use AutowireTrait;
1818

tests/integration/AttributesTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace Unish;
66

77
use Consolidation\AnnotatedCommand\AnnotatedCommandFactory;
8-
use Custom\Library\Drush\Commands\ExampleAttributesDrushCommands;
8+
use Custom\Library\Drush\Commands\ExampleAttributesCommands;
99
use Symfony\Component\Console\Tester\CommandCompletionTester;
1010
use Symfony\Component\Filesystem\Path;
1111

@@ -16,7 +16,7 @@
1616
*/
1717
class AttributesTest extends UnishIntegrationTestCase
1818
{
19-
private ExampleAttributesDrushCommands $commandFileInstance;
19+
private ExampleAttributesCommands $commandFileInstance;
2020
private AnnotatedCommandFactory $commandFactory;
2121

2222
public function testAttributes()
@@ -52,7 +52,7 @@ public function testCompletion()
5252
$this->markTestSkipped('Symfony Console 6.2+ needed for rest this test.');
5353
}
5454

55-
$this->commandFileInstance = new ExampleAttributesDrushCommands();
55+
$this->commandFileInstance = new ExampleAttributesCommands();
5656
$this->commandFactory = new AnnotatedCommandFactory();
5757
$commandInfo = $this->commandFactory->createCommandInfo($this->commandFileInstance, 'testArithmatic');
5858
$command = $this->commandFactory->createCommand($commandInfo, $this->commandFileInstance);

0 commit comments

Comments
 (0)