mirror of
https://github.com/DesignPatternsPHP/DesignPatternsPHP.git
synced 2025-05-21 22:09:43 +02:00
add the generic handler and the request classes
This commit is contained in:
parent
fc781c891a
commit
5ef810f3d0
58
ChainOfResponsibilities/Handler.php
Normal file
58
ChainOfResponsibilities/Handler.php
Normal file
@ -0,0 +1,58 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\ChainOfResponsibilities;
|
||||
|
||||
/**
|
||||
* Handler is a generic handler in the chain of responsibilities
|
||||
*/
|
||||
abstract class Handler implements KeyValueStorage
|
||||
{
|
||||
|
||||
protected $successor = null;
|
||||
|
||||
/**
|
||||
* Append a responsibility to the end of chain
|
||||
*
|
||||
* A prepend method could be done with the same spirit
|
||||
*
|
||||
* You could also send the successor in the contructor but in PHP it is a
|
||||
* bad idea because you have to remove the type-hint of the parameter because
|
||||
* the last handler has a null successor.
|
||||
*
|
||||
* And if you override the contructor, your Handler can no longer have a
|
||||
* successor. One solution is to provide a NullObject (see pattern)
|
||||
*/
|
||||
final public function append(Handler $handler)
|
||||
{
|
||||
if (is_null($this->successor)) {
|
||||
$this->successor = $handler;
|
||||
} else {
|
||||
$this->successor->append($handler);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle the request.
|
||||
*
|
||||
* This approach by using a template method pattern ensures you that
|
||||
* each subclass will not forget to call the successor. Beside, the returned
|
||||
* boolean value indicates you if the request have been processed or not.
|
||||
*/
|
||||
final public function handle(Request $req)
|
||||
{
|
||||
$processed = $this->processing($req);
|
||||
if (!$processed) {
|
||||
if (!is_null($this->successor)) {
|
||||
$processed = $this->successor->handle($req);
|
||||
}
|
||||
}
|
||||
|
||||
return $processed;
|
||||
}
|
||||
|
||||
abstract protected function processing(Request $req);
|
||||
}
|
23
ChainOfResponsibilities/Request.php
Normal file
23
ChainOfResponsibilities/Request.php
Normal file
@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\ChainOfResponsibilities;
|
||||
|
||||
/**
|
||||
* Request is a request which goes throught the chain of responsibilities.
|
||||
*
|
||||
* About the request : Sometimes, you don't need an object, just an integer or
|
||||
* an array. But in this case of a full example, I've made a class to illustrate
|
||||
* this important idea in the CoR (Chain of Responsibilities). In real world,
|
||||
* I recommand to always use a class, even a \stdClass if you want, it proves
|
||||
* to be more adaptative because a single handler doesn't know much about the
|
||||
* outside world and it is more difficult if, one day, you want add some
|
||||
* criterion in a decision process.
|
||||
*/
|
||||
class Request
|
||||
{
|
||||
|
||||
}
|
22
Tests/ChainOfResponsibilities/ChainTest.php
Normal file
22
Tests/ChainOfResponsibilities/ChainTest.php
Normal file
@ -0,0 +1,22 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* DesignPatternPHP
|
||||
*/
|
||||
|
||||
namespace DesignPatterns\Tests\ChainOfResponsibilities;
|
||||
|
||||
use DesignPatterns\ChainOfResponsibilities\Request;
|
||||
|
||||
/**
|
||||
* ChainTest tests the CoR
|
||||
*/
|
||||
class ChainTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
public function testProcess()
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user