Refactor names in CommandResolverInterface
This commit is contained in:
parent
07f8ae467c
commit
9ab9561270
@ -51,6 +51,11 @@ class CommandBus implements CommandBusInterface
|
|||||||
$this->resolvers[] = $resolver;
|
$this->resolvers[] = $resolver;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function removeResolver(CommandResolverInterface $resolver): void
|
||||||
|
{
|
||||||
|
// FIXME implement
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the names of defined commands
|
* Get the names of defined commands
|
||||||
*
|
*
|
||||||
@ -60,7 +65,7 @@ class CommandBus implements CommandBusInterface
|
|||||||
{
|
{
|
||||||
$commands = [];
|
$commands = [];
|
||||||
foreach ($this->resolvers as $resolver) {
|
foreach ($this->resolvers as $resolver) {
|
||||||
$commands = array_merge($commands, $resolver->getNames());
|
$commands = array_merge($commands, $resolver->getCommandNames());
|
||||||
}
|
}
|
||||||
sort($commands);
|
sort($commands);
|
||||||
return array_unique($commands);
|
return array_unique($commands);
|
||||||
@ -75,7 +80,7 @@ class CommandBus implements CommandBusInterface
|
|||||||
public function findCommand(string $command): ?Command
|
public function findCommand(string $command): ?Command
|
||||||
{
|
{
|
||||||
foreach ($this->resolvers as $resolver) {
|
foreach ($this->resolvers as $resolver) {
|
||||||
if ($found = $resolver->find($command))
|
if ($found = $resolver->findCommand($command))
|
||||||
return $found;
|
return $found;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
|
@ -40,12 +40,12 @@ class CommandRegistry implements CommandResolverInterface, EventEmitterInterface
|
|||||||
$this->emit(self::EVENT_UNREGISTERED, [ $command ]);
|
$this->emit(self::EVENT_UNREGISTERED, [ $command ]);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function find(string $command): ?Command
|
public function findCommand(string $command): ?Command
|
||||||
{
|
{
|
||||||
return $this->commands[$command] ?? null;
|
return $this->commands[$command] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getNames(): array
|
public function getCommandNames(): array
|
||||||
{
|
{
|
||||||
return array_keys($this->commands);
|
return array_keys($this->commands);
|
||||||
}
|
}
|
||||||
|
@ -11,9 +11,9 @@ use Evenement\EventEmitterTrait;
|
|||||||
interface CommandResolverInterface
|
interface CommandResolverInterface
|
||||||
{
|
{
|
||||||
|
|
||||||
public function find(string $command): ?Command;
|
public function findCommand(string $command): ?Command;
|
||||||
|
|
||||||
public function getNames(): array;
|
public function getCommandNames(): array;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
53
src/CommandResolverTrait.php
Normal file
53
src/CommandResolverTrait.php
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace NoccyLabs\React\CommandBus;
|
||||||
|
use Evenement\EventEmitterInterface;
|
||||||
|
use Evenement\EventEmitterTrait;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A collection of commands that can be executed via CommandBusInterface
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
trait CommandResolverTrait implements EventEmitterInterface
|
||||||
|
{
|
||||||
|
use EventEmitterTrait;
|
||||||
|
|
||||||
|
const EVENT_REGISTERED = 'registered';
|
||||||
|
const EVENT_UNREGISTERED = 'unregistered';
|
||||||
|
|
||||||
|
/** @var array<string,Command> */
|
||||||
|
private array $commands = [];
|
||||||
|
|
||||||
|
public function registerCommand(string $command, callable $handler): void
|
||||||
|
{
|
||||||
|
$isNew = !array_key_exists($command, $this->commands);
|
||||||
|
|
||||||
|
$this->commands[$command] = new Command($command, $handler);
|
||||||
|
|
||||||
|
if ($isNew) {
|
||||||
|
$this->emit(self::EVENT_REGISTERED, [ $command ]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public function unregisterCommand(string $command): void
|
||||||
|
{
|
||||||
|
if (!array_key_exists($command, $this->commands)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
unset($this->commands[$command]);
|
||||||
|
|
||||||
|
$this->emit(self::EVENT_UNREGISTERED, [ $command ]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function findCommand(string $command): ?Command
|
||||||
|
{
|
||||||
|
return $this->commands[$command] ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function getCommandNames(): array
|
||||||
|
{
|
||||||
|
return array_keys($this->commands);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -48,8 +48,8 @@ class CommandRegistryTest extends \PHPUnit\Framework\TestCase
|
|||||||
$reg->register('a', $cmda);
|
$reg->register('a', $cmda);
|
||||||
$reg->register('b', $cmdb);
|
$reg->register('b', $cmdb);
|
||||||
|
|
||||||
$this->assertEquals('a', $reg->find('a')?->getName());
|
$this->assertEquals('a', $reg->findCommand('a')?->getName());
|
||||||
$this->assertEquals('b', $reg->find('b')?->getName());
|
$this->assertEquals('b', $reg->findCommand('b')?->getName());
|
||||||
}
|
}
|
||||||
|
|
||||||
public function testGettingCommandNames()
|
public function testGettingCommandNames()
|
||||||
@ -61,7 +61,7 @@ class CommandRegistryTest extends \PHPUnit\Framework\TestCase
|
|||||||
$reg->register('a', $cmda);
|
$reg->register('a', $cmda);
|
||||||
$reg->register('b', $cmdb);
|
$reg->register('b', $cmdb);
|
||||||
|
|
||||||
$this->assertEquals(['a','b'], $reg->getNames());
|
$this->assertEquals(['a','b'], $reg->getCommandNames());
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user