Skip to content

Commit 887696a

Browse files
committed
Merge pull request #45 from thephpleague/feature/remove-command-interface
Remove command interface
2 parents 7838354 + 94b2192 commit 887696a

26 files changed

+95
-84
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
## 0.4.0 (2015-03-30)
2+
BC breaks:
3+
4+
- Removed the `League\Tactician\Command` interface. Now any plain ol' PHP object can be a command! See #43 and #45.
5+
6+
New features:
7+
8+
- None
9+
10+
Bug fixes:
11+
12+
- None
13+
14+
## 0.3.0 (2015-02-15)
15+
First public release!

examples/1-beginner-standard-usage.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33

44
use League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector;
55
use League\Tactician\Handler\Locator\InMemoryLocator;
6-
use League\Tactician\Command;
76

87
// Our example Command and Handler. ///////////////////////////////////////////
9-
class RegisterUserCommand implements Command
8+
class RegisterUserCommand
109
{
1110
public $emailAddress;
1211
public $password;
1312
}
1413

1514
class RegisterUserHandler
1615
{
17-
public function handleRegisterUserCommand(RegisterUserCommand $command)
16+
public function handleRegisterUserphp (RegisterUserCommand $command)
1817
{
1918
// Do your core application logic here. Don't actually echo stuff. :)
2019
echo "User {$command->emailAddress} was registered!\n";

examples/2-intermediate-create-middleware.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
use League\Tactician\Middleware;
66
use League\Tactician\CommandBus;
7-
use League\Tactician\Command;
87

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

40-
public function execute(Command $command, callable $next)
39+
public function execute($command, callable $next)
4140
{
4241
$commandClass = get_class($command);
4342

examples/3-intermediate-custom-naming-conventions.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
*
1313
* We can write a custom MethodNameInflector for that:
1414
*/
15-
use League\Tactician\Command;
1615
use League\Tactician\CommandBus;
1716
use League\Tactician\Handler\CommandHandlerMiddleware;
1817
use League\Tactician\Handler\MethodNameInflector\MethodNameInflector;
@@ -21,7 +20,7 @@ class MyCustomInflector implements MethodNameInflector
2120
{
2221
// You can use the command and commandHandler to generate any name you
2322
// prefer but here, we'll always return the same one.
24-
public function inflect(Command $command, $commandHandler)
23+
public function inflect($command, $commandHandler)
2524
{
2625
return 'handle';
2726
}

examples/4-advanced-custom-handler-loading.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
*
1010
* We can create a custom HandlerLocator for that.
1111
*/
12-
use League\Tactician\Command;
1312
use League\Tactician\CommandBus;
1413
use League\Tactician\Handler\CommandHandlerMiddleware;
1514
use League\Tactician\Handler\Locator\HandlerLocator;
@@ -24,7 +23,7 @@ public function __construct($container)
2423
$this->container = $container;
2524
}
2625

27-
public function getHandlerForCommand(Command $command)
26+
public function getHandlerForCommand($command)
2827
{
2928
// This is a cheesy naming strategy but it's just an example
3029
$handlerId = 'app.handler.' . get_class($command);

examples/5-advanced-self-executing-commands.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
require __DIR__ . '/../vendor/autoload.php';
33

4-
use League\Tactician\Command;
54
use League\Tactician\Middleware;
65
use League\Tactician\CommandBus;
76

@@ -12,7 +11,7 @@
1211
*
1312
* Here's a Tactician version of the wikipedia Light Switch example.
1413
*/
15-
interface SelfExecutingCommand extends Command
14+
interface SelfExecutingCommand
1615
{
1716
public function execute(Light $light);
1817
}
@@ -53,7 +52,7 @@ public function __construct($light)
5352
$this->light = $light;
5453
}
5554

56-
public function execute(Command $command, callable $next)
55+
public function execute($command, callable $next)
5756
{
5857
if (!$command instanceof SelfExecutingCommand) {
5958
throw new InvalidArgumentException("Can not execute command");

examples/6-conditional-handlers.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,11 @@
1212
* We can write a custom middleware for that.
1313
*/
1414

15-
use League\Tactician\Command;
1615
use League\Tactician\CommandBus;
1716
use League\Tactician\Middleware;
1817

1918
// External command
20-
interface ExternalCommand extends Command
19+
interface ExternalCommand
2120
{
2221
}
2322

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

35-
public function execute(Command $command, callable $next)
34+
public function execute($command, callable $next)
3635
{
3736
if ($command instanceof ExternalCommand) {
3837
return $this->commandBus->handle($command);
@@ -44,7 +43,7 @@ public function execute(Command $command, callable $next)
4443
// and we'll create a custom command handler/middleware
4544
final class ExternalCommandHandler implements Middleware
4645
{
47-
public function execute(Command $command, callable $next)
46+
public function execute($command, callable $next)
4847
{
4948
echo sprintf("Dispatched %s!\n", get_class($command));
5049
}

examples/repeated-sample-code.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@
55

66
use League\Tactician\Handler\MethodNameInflector\HandleClassNameInflector;
77
use League\Tactician\Handler\Locator\InMemoryLocator;
8-
use League\Tactician\Command;
98

10-
class RegisterUserCommand implements Command
9+
class RegisterUserCommand
1110
{
1211
public $emailAddress;
1312
public $password;

src/Command.php

Lines changed: 0 additions & 10 deletions
This file was deleted.

src/CommandBus.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace League\Tactician;
44

55
use Closure;
6+
use League\Tactician\Exception\InvalidCommandException;
67

78
/**
89
* Receives a command and sends it through a chain of middleware for processing.
@@ -25,11 +26,15 @@ public function __construct(array $middleware)
2526
/**
2627
* Executes the given command and optionally returns a value
2728
*
28-
* @param Command $command
29+
* @param object $command
2930
* @return mixed
3031
*/
31-
public function handle(Command $command)
32+
public function handle($command)
3233
{
34+
if (!is_object($command)) {
35+
throw InvalidCommandException::forUnknownValue($command);
36+
}
37+
3338
$middlewareChain = $this->middlewareChain;
3439
return $middlewareChain($command);
3540
}
@@ -40,12 +45,12 @@ public function handle(Command $command)
4045
*/
4146
private function createExecutionChain($middlewareList)
4247
{
43-
$lastCallable = function (Command $command) {
48+
$lastCallable = function ($command) {
4449
// the final callable is a no-op
4550
};
4651

4752
while ($middleware = array_pop($middlewareList)) {
48-
$lastCallable = function (Command $command) use ($middleware, $lastCallable) {
53+
$lastCallable = function ($command) use ($middleware, $lastCallable) {
4954
return $middleware->execute($command, $lastCallable);
5055
};
5156
}

0 commit comments

Comments
 (0)