Commander
Categories
Component ID
2886404
Component name
Commander
Component type
module
Maintenance status
Development status
Component security advisory coverage
covered
Downloads
269
Component created
Component changed
Component body
Provides a command bus
All you need to do is to create two classes, command and command handler, and pass the command object to the command bus.
How to use the module
1. Create your command handler plugin. That is a class that knows how to execute your command. You can inject any services into your handler class.
<?php
namespace Drupalmy_modulePluginCommandHandler;
use DrupalcommanderContractsCommandInterface;
use DrupalcommanderPluginCommandHandlerBase;
/**
* @CommandHandler(
* id = "register_user_handler",
* label = @Translation("Register user handler"),
* )
*/
class RegisterUserHandler extends CommandHandlerBase {
public function execute(CommandInterface $command) {
$data = $command->data;
// Do cool stuff with $data.
return TRUE;
}
}
?>
2. Create your command class and pass any data to it that will be required for executing the command. Make sure to implement the handlerPluginId() method which returns the plugin ID of your command handler.
<?php
namespace Drupalmy_moduleCommands;
use DrupalcommanderContractsCommandInterface;
class RegisterUser implements CommandInterface {
public $data;
public function __construct($data) {
$this->data = $data;
}
public function handlerPluginId() {
return 'register_user_handler';
}
}
?>3. Send your command to the command bus for execution.
<?php
$command = new RegisterUser($data);
Drupal::service('commander.command_bus')->execute($command);
?>