Skip to content

Commit

Permalink
Relax auto-discoverable command file/class naming convention (#6153)
Browse files Browse the repository at this point in the history
* Relax the file/class naming convention

* Adapt test fixtures & documentation
  • Loading branch information
claudiu-cristea authored Nov 1, 2024
1 parent 4e7fed6 commit 038a783
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion docs/commands.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/Runtime/ServiceManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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!');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 3 additions & 3 deletions tests/integration/AttributesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -16,7 +16,7 @@
*/
class AttributesTest extends UnishIntegrationTestCase
{
private ExampleAttributesDrushCommands $commandFileInstance;
private ExampleAttributesCommands $commandFileInstance;
private AnnotatedCommandFactory $commandFactory;

public function testAttributes()
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 038a783

Please sign in to comment.