Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from thephpleague/feature/middleware
Command Middleware
- Loading branch information
Showing
15 changed files
with
248 additions
and
131 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
namespace League\Tactician; | ||
|
||
/** | ||
* | ||
*/ | ||
interface Middleware | ||
{ | ||
public function execute(Command $command, callable $next); | ||
} |
31 changes: 11 additions & 20 deletions
31
src/LockingCommandBus.php → src/Plugins/LockingMiddleware.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
namespace League\Tactician; | ||
|
||
use League\Tactician\Exception\CommandWasNotHandledException; | ||
|
||
/** | ||
* Receives a command and modifies or dispatches it to a handler in some way | ||
*/ | ||
class StandardCommandBus implements CommandBus | ||
{ | ||
/** | ||
* @var Closure | ||
*/ | ||
private $middlewareChain; | ||
|
||
/** | ||
* @param Middleware[] $middleware | ||
*/ | ||
public function __construct(array $middleware) | ||
{ | ||
$this->middlewareChain = $this->createExecutionChain($middleware); | ||
} | ||
|
||
/** | ||
* Executes the given command and optionally returns a value | ||
* | ||
* @param Command $command | ||
* @return mixed | ||
*/ | ||
public function execute(Command $command) | ||
{ | ||
$middlewareChain = $this->middlewareChain; | ||
return $middlewareChain($command); | ||
} | ||
|
||
/** | ||
* @param Middleware[] $middlewareList | ||
* @return \Closure | ||
*/ | ||
protected function createExecutionChain($middlewareList) | ||
{ | ||
$lastCallable = function (Command $command) { | ||
// the final callable is a no-op | ||
}; | ||
|
||
while ($middleware = array_pop($middlewareList)) { | ||
$lastCallable = function (Command $command) use ($middleware, $lastCallable) { | ||
return $middleware->execute($command, $lastCallable); | ||
}; | ||
} | ||
|
||
return $lastCallable; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.