mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-07-31 04:00:18 +02:00
PHP7 Command
This commit is contained in:
@@ -4,14 +4,14 @@ namespace DesignPatterns\Behavioral\Command;
|
||||
|
||||
/**
|
||||
* This concrete command tweaks receiver to add current date to messages
|
||||
* invoker just knows that it can call "execute".
|
||||
* invoker just knows that it can call "execute"
|
||||
*/
|
||||
class AddMessageDateCommand implements UndoableCommandInterface
|
||||
{
|
||||
/**
|
||||
* @var Receiver
|
||||
*/
|
||||
protected $output;
|
||||
private $output;
|
||||
|
||||
/**
|
||||
* Each concrete command is built with different receivers.
|
||||
|
@@ -2,9 +2,6 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\Command;
|
||||
|
||||
/**
|
||||
* class CommandInterface.
|
||||
*/
|
||||
interface CommandInterface
|
||||
{
|
||||
/**
|
||||
|
@@ -4,18 +4,18 @@ namespace DesignPatterns\Behavioral\Command;
|
||||
|
||||
/**
|
||||
* This concrete command calls "print" on the Receiver, but an external
|
||||
* invoker just knows that it can call "execute".
|
||||
* invoker just knows that it can call "execute"
|
||||
*/
|
||||
class HelloCommand implements CommandInterface
|
||||
{
|
||||
/**
|
||||
* @var Receiver
|
||||
*/
|
||||
protected $output;
|
||||
private $output;
|
||||
|
||||
/**
|
||||
* Each concrete command is built with different receivers.
|
||||
* There can be one, many or completely no receivers, but there can be other commands in the parameters.
|
||||
* There can be one, many or completely no receivers, but there can be other commands in the parameters
|
||||
*
|
||||
* @param Receiver $console
|
||||
*/
|
||||
@@ -29,8 +29,7 @@ class HelloCommand implements CommandInterface
|
||||
*/
|
||||
public function execute()
|
||||
{
|
||||
// sometimes, there is no receiver and this is the command which
|
||||
// does all the work
|
||||
// sometimes, there is no receiver and this is the command which does all the work
|
||||
$this->output->write('Hello World');
|
||||
}
|
||||
}
|
||||
|
@@ -11,11 +11,11 @@ class Invoker
|
||||
/**
|
||||
* @var CommandInterface
|
||||
*/
|
||||
protected $command;
|
||||
private $command;
|
||||
|
||||
/**
|
||||
* In the invoker we find this kind of method for subscribing the command.
|
||||
* There can be also a stack, a list, a fixed set...
|
||||
* in the invoker we find this kind of method for subscribing the command
|
||||
* There can be also a stack, a list, a fixed set ...
|
||||
*
|
||||
* @param CommandInterface $cmd
|
||||
*/
|
||||
@@ -25,12 +25,10 @@ class Invoker
|
||||
}
|
||||
|
||||
/**
|
||||
* executes the command.
|
||||
* executes the command; the invoker is the same whatever is the command
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
// here is a key feature of the invoker
|
||||
// the invoker is the same whatever is the command
|
||||
$this->command->execute();
|
||||
}
|
||||
}
|
||||
|
@@ -7,14 +7,20 @@ namespace DesignPatterns\Behavioral\Command;
|
||||
*/
|
||||
class Receiver
|
||||
{
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $enableDate = false;
|
||||
|
||||
private $output = array();
|
||||
/**
|
||||
* @var string[]
|
||||
*/
|
||||
private $output = [];
|
||||
|
||||
/**
|
||||
* @param string $str
|
||||
*/
|
||||
public function write($str)
|
||||
public function write(string $str)
|
||||
{
|
||||
if ($this->enableDate) {
|
||||
$str .= ' ['.date('Y-m-d').']';
|
||||
@@ -23,13 +29,13 @@ class Receiver
|
||||
$this->output[] = $str;
|
||||
}
|
||||
|
||||
public function getOutput()
|
||||
public function getOutput(): string
|
||||
{
|
||||
return implode("\n", $this->output);
|
||||
return join("\n", $this->output);
|
||||
}
|
||||
|
||||
/**
|
||||
* Enable receiver to display message date.
|
||||
* Enable receiver to display message date
|
||||
*/
|
||||
public function enableDate()
|
||||
{
|
||||
@@ -37,7 +43,7 @@ class Receiver
|
||||
}
|
||||
|
||||
/**
|
||||
* Disable receiver to display message date.
|
||||
* Disable receiver to display message date
|
||||
*/
|
||||
public function disableDate()
|
||||
{
|
||||
|
@@ -6,31 +6,15 @@ use DesignPatterns\Behavioral\Command\HelloCommand;
|
||||
use DesignPatterns\Behavioral\Command\Invoker;
|
||||
use DesignPatterns\Behavioral\Command\Receiver;
|
||||
|
||||
/**
|
||||
* CommandTest has the role of the Client in the Command Pattern.
|
||||
*/
|
||||
class CommandTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Invoker
|
||||
*/
|
||||
protected $invoker;
|
||||
|
||||
/**
|
||||
* @var Receiver
|
||||
*/
|
||||
protected $receiver;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->invoker = new Invoker();
|
||||
$this->receiver = new Receiver();
|
||||
}
|
||||
|
||||
public function testInvocation()
|
||||
{
|
||||
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
||||
$this->invoker->run();
|
||||
$this->assertEquals($this->receiver->getOutput(), 'Hello World');
|
||||
$invoker = new Invoker();
|
||||
$receiver = new Receiver();
|
||||
|
||||
$invoker->setCommand(new HelloCommand($receiver));
|
||||
$invoker->run();
|
||||
$this->assertEquals($receiver->getOutput(), 'Hello World');
|
||||
}
|
||||
}
|
||||
|
@@ -6,44 +6,27 @@ use DesignPatterns\Behavioral\Command\AddMessageDateCommand;
|
||||
use DesignPatterns\Behavioral\Command\HelloCommand;
|
||||
use DesignPatterns\Behavioral\Command\Invoker;
|
||||
use DesignPatterns\Behavioral\Command\Receiver;
|
||||
use PHPUnit_Framework_TestCase;
|
||||
|
||||
/**
|
||||
* UndoableCommandTest has the role of the Client in the Command Pattern.
|
||||
*/
|
||||
class UndoableCommandTest extends PHPUnit_Framework_TestCase
|
||||
class UndoableCommandTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @var Invoker
|
||||
*/
|
||||
protected $invoker;
|
||||
|
||||
/**
|
||||
* @var Receiver
|
||||
*/
|
||||
protected $receiver;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->invoker = new Invoker();
|
||||
$this->receiver = new Receiver();
|
||||
}
|
||||
|
||||
public function testInvocation()
|
||||
{
|
||||
$this->invoker->setCommand(new HelloCommand($this->receiver));
|
||||
$this->invoker->run();
|
||||
$this->assertEquals($this->receiver->getOutput(), 'Hello World');
|
||||
$invoker = new Invoker();
|
||||
$receiver = new Receiver();
|
||||
|
||||
$messageDateCommand = new AddMessageDateCommand($this->receiver);
|
||||
$invoker->setCommand(new HelloCommand($receiver));
|
||||
$invoker->run();
|
||||
$this->assertEquals($receiver->getOutput(), 'Hello World');
|
||||
|
||||
$messageDateCommand = new AddMessageDateCommand($receiver);
|
||||
$messageDateCommand->execute();
|
||||
|
||||
$this->invoker->run();
|
||||
$this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']');
|
||||
$invoker->run();
|
||||
$this->assertEquals($receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d').']');
|
||||
|
||||
$messageDateCommand->undo();
|
||||
|
||||
$this->invoker->run();
|
||||
$this->assertEquals($this->receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World");
|
||||
$invoker->run();
|
||||
$this->assertEquals($receiver->getOutput(), "Hello World\nHello World [".date('Y-m-d')."]\nHello World");
|
||||
}
|
||||
}
|
||||
|
@@ -2,14 +2,10 @@
|
||||
|
||||
namespace DesignPatterns\Behavioral\Command;
|
||||
|
||||
/**
|
||||
* Interface UndoableCommandInterface.
|
||||
*/
|
||||
interface UndoableCommandInterface extends CommandInterface
|
||||
{
|
||||
/**
|
||||
* This method is used to undo change made by command execution
|
||||
* The Receiver goes in the constructor.
|
||||
*/
|
||||
public function undo();
|
||||
}
|
||||
|
Reference in New Issue
Block a user