Skip to content

Commit

Permalink
Merge pull request #45 from thephpleague/feature/remove-command-inter…
Browse files Browse the repository at this point in the history
…face

Remove command interface
  • Loading branch information
rosstuck committed Mar 30, 2015
2 parents 7838354 + 94b2192 commit 887696a
Show file tree
Hide file tree
Showing 26 changed files with 95 additions and 84 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
## 0.4.0 (2015-03-30)
BC breaks:

- Removed the `League\Tactician\Command` interface. Now any plain ol' PHP object can be a command! See #43 and #45.

New features:

- None

Bug fixes:

- None

## 0.3.0 (2015-02-15)
First public release!
5 changes: 2 additions & 3 deletions examples/1-beginner-standard-usage.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@

use League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector;
use League\Tactician\Handler\Locator\InMemoryLocator;
use League\Tactician\Command;

// Our example Command and Handler. ///////////////////////////////////////////
class RegisterUserCommand implements Command
class RegisterUserCommand
{
public $emailAddress;
public $password;
}

class RegisterUserHandler
{
public function handleRegisterUserCommand(RegisterUserCommand $command)
public function handleRegisterUserphp (RegisterUserCommand $command)
{
// Do your core application logic here. Don't actually echo stuff. :)
echo "User {$command->emailAddress} was registered!\n";
Expand Down
3 changes: 1 addition & 2 deletions examples/2-intermediate-create-middleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use League\Tactician\Middleware;
use League\Tactician\CommandBus;
use League\Tactician\Command;

/**
* Let's say we want something to happen every time we execute a command.
Expand Down Expand Up @@ -37,7 +36,7 @@ public function __construct(Logger $logger)
$this->logger = $logger;
}

public function execute(Command $command, callable $next)
public function execute($command, callable $next)
{
$commandClass = get_class($command);

Expand Down
3 changes: 1 addition & 2 deletions examples/3-intermediate-custom-naming-conventions.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
*
* We can write a custom MethodNameInflector for that:
*/
use League\Tactician\Command;
use League\Tactician\CommandBus;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
Expand All @@ -21,7 +20,7 @@ class MyCustomInflector implements MethodNameInflector
{
// You can use the command and commandHandler to generate any name you
// prefer but here, we'll always return the same one.
public function inflect(Command $command, $commandHandler)
public function inflect($command, $commandHandler)
{
return 'handle';
}
Expand Down
3 changes: 1 addition & 2 deletions examples/4-advanced-custom-handler-loading.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
*
* We can create a custom HandlerLocator for that.
*/
use League\Tactician\Command;
use League\Tactician\CommandBus;
use League\Tactician\Handler\CommandHandlerMiddleware;
use League\Tactician\Handler\Locator\HandlerLocator;
Expand All @@ -24,7 +23,7 @@ public function __construct($container)
$this->container = $container;
}

public function getHandlerForCommand(Command $command)
public function getHandlerForCommand($command)
{
// This is a cheesy naming strategy but it's just an example
$handlerId = 'app.handler.' . get_class($command);
Expand Down
5 changes: 2 additions & 3 deletions examples/5-advanced-self-executing-commands.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
<?php
require __DIR__ . '/../vendor/autoload.php';

use League\Tactician\Command;
use League\Tactician\Middleware;
use League\Tactician\CommandBus;

Expand All @@ -12,7 +11,7 @@
*
* Here's a Tactician version of the wikipedia Light Switch example.
*/
interface SelfExecutingCommand extends Command
interface SelfExecutingCommand
{
public function execute(Light $light);
}
Expand Down Expand Up @@ -53,7 +52,7 @@ public function __construct($light)
$this->light = $light;
}

public function execute(Command $command, callable $next)
public function execute($command, callable $next)
{
if (!$command instanceof SelfExecutingCommand) {
throw new InvalidArgumentException("Can not execute command");
Expand Down
7 changes: 3 additions & 4 deletions examples/6-conditional-handlers.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
* We can write a custom middleware for that.
*/

use League\Tactician\Command;
use League\Tactician\CommandBus;
use League\Tactician\Middleware;

// External command
interface ExternalCommand extends Command
interface ExternalCommand
{
}

Expand All @@ -32,7 +31,7 @@ public function __construct(CommandBus $commandBus)
$this->commandBus = $commandBus;
}

public function execute(Command $command, callable $next)
public function execute($command, callable $next)
{
if ($command instanceof ExternalCommand) {
return $this->commandBus->handle($command);
Expand All @@ -44,7 +43,7 @@ public function execute(Command $command, callable $next)
// and we'll create a custom command handler/middleware
final class ExternalCommandHandler implements Middleware
{
public function execute(Command $command, callable $next)
public function execute($command, callable $next)
{
echo sprintf("Dispatched %s!\n", get_class($command));
}
Expand Down
3 changes: 1 addition & 2 deletions examples/repeated-sample-code.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@

use League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector;
use League\Tactician\Handler\Locator\InMemoryLocator;
use League\Tactician\Command;

class RegisterUserCommand implements Command
class RegisterUserCommand
{
public $emailAddress;
public $password;
Expand Down
10 changes: 0 additions & 10 deletions src/Command.php

This file was deleted.

13 changes: 9 additions & 4 deletions src/CommandBus.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace League\Tactician;

use Closure;
use League\Tactician\Exception\InvalidCommandException;

/**
* Receives a command and sends it through a chain of middleware for processing.
Expand All @@ -25,11 +26,15 @@ public function __construct(array $middleware)
/**
* Executes the given command and optionally returns a value
*
* @param Command $command
* @param object $command
* @return mixed
*/
public function handle(Command $command)
public function handle($command)
{
if (!is_object($command)) {
throw InvalidCommandException::forUnknownValue($command);
}

$middlewareChain = $this->middlewareChain;
return $middlewareChain($command);
}
Expand All @@ -40,12 +45,12 @@ public function handle(Command $command)
*/
private function createExecutionChain($middlewareList)
{
$lastCallable = function (Command $command) {
$lastCallable = function ($command) {
// the final callable is a no-op
};

while ($middleware = array_pop($middlewareList)) {
$lastCallable = function (Command $command) use ($middleware, $lastCallable) {
$lastCallable = function ($command) use ($middleware, $lastCallable) {
return $middleware->execute($command, $lastCallable);
};
}
Expand Down
12 changes: 5 additions & 7 deletions src/Exception/CanNotInvokeHandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,25 @@

namespace League\Tactician\Exception;

use League\Tactician\Command;

/**
* Thrown when a specific handler object can not used on a command object.
* Thrown when a specific handler object can not be used on a command object.
*
* The most common reason is the receiving method is missing or incorrectly
* named.
*/
class CanNotInvokeHandlerException extends \BadMethodCallException implements Exception
{
/**
* @var Command
* @var object
*/
private $command;

/**
* @param Command $command
* @param object $command
* @param string $reason
* @return static
*/
public static function forCommand(Command $command, $reason)
public static function forCommand($command, $reason)
{
$exception = new static(
'Could not invoke handler for command ' . get_class($command) .
Expand All @@ -36,7 +34,7 @@ public static function forCommand(Command $command, $reason)
/**
* Returns the command that could not be invoked
*
* @return Command
* @return object
*/
public function getCommand()
{
Expand Down
32 changes: 32 additions & 0 deletions src/Exception/InvalidCommandException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php
namespace League\Tactician\Exception;

/**
* Thrown when the command bus is given an non-object to use as a command.
*/
class InvalidCommandException extends \RuntimeException implements Exception
{
/**
* @var mixed
*/
private $invalidCommand;

/**
* @param mixed $invalidCommand
* @return static
*/
public static function forUnknownValue($invalidCommand)
{
return new static(
"Commands must be an object but the value given was of type " . gettype($invalidCommand)
);
}

/**
* @return mixed
*/
public function getInvalidCommand()
{
return $this->invalidCommand;
}
}
10 changes: 4 additions & 6 deletions src/Exception/MissingHandlerException.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,21 @@

namespace League\Tactician\Exception;

use League\Tactician\Command;

/**
* No handler could be found for the given command.
*/
class MissingHandlerException extends \OutOfBoundsException implements Exception
{
/**
* @var Command
* @var object
*/
private $command;

/**
* @param Command $command
* @param object $command
* @return static
*/
public static function forCommand(Command $command)
public static function forCommand($command)
{
$exception = new static('Missing handler for command: ' . get_class($command));
$exception->command = $command;
Expand All @@ -27,7 +25,7 @@ public static function forCommand(Command $command)
}

/**
* @return Command
* @return object
*/
public function getCommand()
{
Expand Down
5 changes: 2 additions & 3 deletions src/Handler/CommandHandlerMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace League\Tactician\Handler;

use League\Tactician\Middleware;
use League\Tactician\Command;
use League\Tactician\Exception\CanNotInvokeHandlerException;
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
use League\Tactician\Handler\Locator\HandlerLocator;
Expand Down Expand Up @@ -37,11 +36,11 @@ public function __construct(HandlerLocator $handlerLoader, MethodNameInflector $
* Executes a command and optionally returns a value
*
* @throws CanNotInvokeHandlerException
* @param Command $command
* @param object $command
* @param callable $next
* @return mixed
*/
public function execute(Command $command, callable $next)
public function execute($command, callable $next)
{
$handler = $this->handlerLocator->getHandlerForCommand($command);
$methodName = $this->methodNameInflector->inflect($command, $handler);
Expand Down
6 changes: 2 additions & 4 deletions src/Handler/Locator/HandlerLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

namespace League\Tactician\Handler\Locator;

use League\Tactician\Command;

/**
* Service locator for handler objects
*
Expand All @@ -15,8 +13,8 @@ interface HandlerLocator
/**
* Retrieves the handler for a specified command
*
* @param Command $command
* @param object $command
* @return mixed
*/
public function getHandlerForCommand(Command $command);
public function getHandlerForCommand($command);
}
5 changes: 2 additions & 3 deletions src/Handler/Locator/InMemoryLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

namespace League\Tactician\Handler\Locator;

use League\Tactician\Command;
use League\Tactician\Exception\MissingHandlerException;

/**
Expand Down Expand Up @@ -65,11 +64,11 @@ protected function addHandlers(array $commandClassToHandlerMap)
/**
* Retrieve handler for the given command
*
* @param Command $command
* @param object $command
* @return object
* @throws MissingHandlerException
*/
public function getHandlerForCommand(Command $command)
public function getHandlerForCommand($command)
{
$className = get_class($command);

Expand Down
Loading

0 comments on commit 887696a

Please sign in to comment.