-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Attribute also does the validate (#5818)
- Loading branch information
Showing
15 changed files
with
172 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drush\Attributes; | ||
|
||
use Attribute; | ||
use Consolidation\AnnotatedCommand\CommandData; | ||
use Consolidation\AnnotatedCommand\CommandError; | ||
|
||
#[Attribute(Attribute::TARGET_METHOD)] | ||
class ValidateQueueName extends ValidatorBase implements ValidatorInterface | ||
{ | ||
/** | ||
* @param string $argumentName | ||
* The name of the argument which specifies the queue name. | ||
*/ | ||
public function __construct( | ||
public string $argumentName = 'queue_name' | ||
) { | ||
} | ||
|
||
public function validate(CommandData $commandData) | ||
{ | ||
$queueName = $commandData->input()->getArgument($this->argumentName); | ||
if (!in_array($queueName, self::getQueues())) { | ||
$msg = dt('Queue not found: !name', ['!name' => $queueName]); | ||
return new CommandError($msg); | ||
} | ||
} | ||
|
||
public static function getQueues(): array | ||
{ | ||
return array_keys(\Drupal::service('plugin.manager.queue_worker')->getDefinitions()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Drush\Attributes; | ||
|
||
use Consolidation\AnnotatedCommand\Attributes\AttributeInterface; | ||
use Consolidation\AnnotatedCommand\CommandData; | ||
use Consolidation\AnnotatedCommand\Parser\CommandInfo; | ||
use Drush\Drush; | ||
|
||
abstract class ValidatorBase | ||
{ | ||
public static function handle(\ReflectionAttribute $attribute, CommandInfo $commandInfo) | ||
{ | ||
$instance = $attribute->newInstance(); | ||
$hookManager = Drush::getContainer()->get('hookManager'); | ||
$hookManager->add( | ||
// Use a Closure to acquire $commandData. | ||
fn(CommandData $commandData) => $instance->validate($commandData), | ||
$hookManager::ARGUMENT_VALIDATOR, | ||
$commandInfo->getName() | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Drush\Attributes; | ||
|
||
use Consolidation\AnnotatedCommand\CommandData; | ||
|
||
interface ValidatorInterface | ||
{ | ||
public function validate(CommandData $commandData); | ||
} |
Oops, something went wrong.