PHP code example of gendoria / command-queue

1. Go to this page and download the library: Download gendoria/command-queue library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gendoria / command-queue example snippets


use Gendoria\CommandQueue\Command\CommandInterface;
use Gendoria\CommandQueue\CommandProcessor\CommandProcessorInterface;
use Gendoria\CommandQueue\ProcessorFactory\ProcessorFactory;
use Gendoria\CommandQueue\SendDriver\DirectProcessingDriver;
use Psr\Log\LoggerInterface;

class SimpleCommand implements CommandInterface
{
    public $testData;
    
    public function __construct($testData)
    {
        $this->testData = $testData;
    }
}

class SimpleProcessor implements CommandProcessorInterface
{
    /**
     *
     * @var LoggerInterface
     */
    private $logger;
    
    /**
     * Process command.
     * 
     * @param SimpleCommand $command
     */
    public function process(CommandInterface $command)
    {
        echo "Command class: ".get_class($command)."\n";
        echo "Command payload: ".$command->testData."\n";
        echo "\n";
    }

    public function setLogger(LoggerInterface $logger)
    {
        $this->logger = $logger;
    }

    public function supports(CommandInterface $command)
    {
        return $command instanceof SimpleCommand;
    }

}

$simpleProcessor = new SimpleProcessor();

$processorFactory = new ProcessorFactory();
$processorFactory->registerProcessorForCommand(SimpleCommand::class, $simpleProcessor);

$driver = new DirectProcessingDriver($processorFactory);

for ($k = 0; $k < 5; $k++) {
    $command = new SimpleCommand("Test ".($k+1));
    $driver->send($command);
    sleep(1);
}