From 038a783c8e8eb8d3a8cc858b0d8e740205bda1de Mon Sep 17 00:00:00 2001 From: Claudiu Cristea Date: Sat, 2 Nov 2024 01:17:24 +0200 Subject: [PATCH] Relax auto-discoverable command file/class naming convention (#6153) * Relax the file/class naming convention * Adapt test fixtures & documentation --- docs/commands.md | 2 +- src/Runtime/ServiceManager.php | 2 +- .../{CustomDrushCommands.php => CustomCommands.php} | 8 ++++---- ...tesDrushCommands.php => ExampleAttributesCommands.php} | 2 +- ...FactoryDrushCommands.php => StaticFactoryCommands.php} | 2 +- tests/integration/AttributesTest.php | 6 +++--- 6 files changed, 11 insertions(+), 11 deletions(-) rename tests/fixtures/lib/Drush/Commands/{CustomDrushCommands.php => CustomCommands.php} (65%) rename tests/fixtures/lib/Drush/Commands/{ExampleAttributesDrushCommands.php => ExampleAttributesCommands.php} (98%) rename tests/fixtures/lib/Drush/Commands/{StaticFactoryDrushCommands.php => StaticFactoryCommands.php} (93%) diff --git a/docs/commands.md b/docs/commands.md index 095cfb88ca..4ad62fc748 100644 --- a/docs/commands.md +++ b/docs/commands.md @@ -90,7 +90,7 @@ Such commands are auto-discovered by their class PSR4 namespace and class/file n } ``` 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. -* The class and file name ends with `*DrushCommands`, e.g. `FooDrushCommands`. +* The class and file name ends with `*Commands`, e.g. `FooCommands`. 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: ```json diff --git a/src/Runtime/ServiceManager.php b/src/Runtime/ServiceManager.php index 894fe5bf8b..0588d63549 100644 --- a/src/Runtime/ServiceManager.php +++ b/src/Runtime/ServiceManager.php @@ -179,7 +179,7 @@ public function discoverPsr4Commands(): array { $classes = (new RelativeNamespaceDiscovery($this->autoloader)) ->setRelativeNamespace('Drush\Commands') - ->setSearchPattern('/.*DrushCommands\.php$/') + ->setSearchPattern('/.*Commands\.php$/') ->getClasses(); return array_filter($classes, function (string $class): bool { diff --git a/tests/fixtures/lib/Drush/Commands/CustomDrushCommands.php b/tests/fixtures/lib/Drush/Commands/CustomCommands.php similarity index 65% rename from tests/fixtures/lib/Drush/Commands/CustomDrushCommands.php rename to tests/fixtures/lib/Drush/Commands/CustomCommands.php index ebe75d6aa4..8137069ad5 100644 --- a/tests/fixtures/lib/Drush/Commands/CustomDrushCommands.php +++ b/tests/fixtures/lib/Drush/Commands/CustomCommands.php @@ -3,15 +3,15 @@ namespace Custom\Library\Drush\Commands; use Drush\Commands\DrushCommands; +use Drush\Attributes as CLI; -class CustomDrushCommands extends DrushCommands +class CustomCommands extends DrushCommands { /** * Auto-discoverable custom command. Used for Drush testing. - * - * @command custom_cmd - * @hidden */ + #[CLI\Command(name: 'custom_cmd')] + #[CLI\Help(hidden: true)] public function customCommand(): void { $this->io()->note('Hello world!'); diff --git a/tests/fixtures/lib/Drush/Commands/ExampleAttributesDrushCommands.php b/tests/fixtures/lib/Drush/Commands/ExampleAttributesCommands.php similarity index 98% rename from tests/fixtures/lib/Drush/Commands/ExampleAttributesDrushCommands.php rename to tests/fixtures/lib/Drush/Commands/ExampleAttributesCommands.php index ca1f391d70..8aa7db0597 100644 --- a/tests/fixtures/lib/Drush/Commands/ExampleAttributesDrushCommands.php +++ b/tests/fixtures/lib/Drush/Commands/ExampleAttributesCommands.php @@ -12,7 +12,7 @@ use Symfony\Component\Console\Completion\CompletionInput; use Symfony\Component\Console\Completion\CompletionSuggestions; -class ExampleAttributesDrushCommands extends DrushCommands +class ExampleAttributesCommands extends DrushCommands { const ARITHMATIC = 'test:arithmatic'; const ECHO = 'my:echo'; diff --git a/tests/fixtures/lib/Drush/Commands/StaticFactoryDrushCommands.php b/tests/fixtures/lib/Drush/Commands/StaticFactoryCommands.php similarity index 93% rename from tests/fixtures/lib/Drush/Commands/StaticFactoryDrushCommands.php rename to tests/fixtures/lib/Drush/Commands/StaticFactoryCommands.php index 14b946adc0..0e1c883b1a 100644 --- a/tests/fixtures/lib/Drush/Commands/StaticFactoryDrushCommands.php +++ b/tests/fixtures/lib/Drush/Commands/StaticFactoryCommands.php @@ -12,7 +12,7 @@ * in Drush/Commands directory + namespace, relative to some entry in * the library's `autoload` section in its composer.json file. */ -class StaticFactoryDrushCommands extends DrushCommands +class StaticFactoryCommands extends DrushCommands { use AutowireTrait; diff --git a/tests/integration/AttributesTest.php b/tests/integration/AttributesTest.php index 238e2b6869..13988a0a5f 100644 --- a/tests/integration/AttributesTest.php +++ b/tests/integration/AttributesTest.php @@ -5,7 +5,7 @@ namespace Unish; use Consolidation\AnnotatedCommand\AnnotatedCommandFactory; -use Custom\Library\Drush\Commands\ExampleAttributesDrushCommands; +use Custom\Library\Drush\Commands\ExampleAttributesCommands; use Symfony\Component\Console\Tester\CommandCompletionTester; use Symfony\Component\Filesystem\Path; @@ -16,7 +16,7 @@ */ class AttributesTest extends UnishIntegrationTestCase { - private ExampleAttributesDrushCommands $commandFileInstance; + private ExampleAttributesCommands $commandFileInstance; private AnnotatedCommandFactory $commandFactory; public function testAttributes() @@ -52,7 +52,7 @@ public function testCompletion() $this->markTestSkipped('Symfony Console 6.2+ needed for rest this test.'); } - $this->commandFileInstance = new ExampleAttributesDrushCommands(); + $this->commandFileInstance = new ExampleAttributesCommands(); $this->commandFactory = new AnnotatedCommandFactory(); $commandInfo = $this->commandFactory->createCommandInfo($this->commandFileInstance, 'testArithmatic'); $command = $this->commandFactory->createCommand($commandInfo, $this->commandFileInstance);