Skip to content

Commit 6782102

Browse files
committed
sync
2 parents 1d44f1a + c1f0fac commit 6782102

File tree

638 files changed

+24182
-11662
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

638 files changed

+24182
-11662
lines changed

.travis.yml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,10 @@ language: php
33
sudo: false
44

55
php:
6-
- 5.3
7-
- 5.4
8-
- 5.5
9-
- 5.6
106
- 7.0
11-
- hhvm
7+
- 7.1
128

139
matrix:
14-
allow_failures:
15-
- php: hhvm
16-
- php: 7.0
1710
fast_finish: true
1811

1912
cache:

Behavioral/ChainOfResponsibilities/Handler.php

Lines changed: 16 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,77 +2,42 @@
22

33
namespace DesignPatterns\Behavioral\ChainOfResponsibilities;
44

5-
/**
6-
* Handler is a generic handler in the chain of responsibilities.
7-
*
8-
* Yes you could have a lighter CoR with a simpler handler but if you want your CoR
9-
* to be extendable and decoupled, it's a better idea to do things like that in real
10-
* situations. Usually, a CoR is meant to be changed everytime and evolves, that's
11-
* why we slice the workflow in little bits of code.
12-
*/
5+
use Psr\Http\Message\RequestInterface;
6+
use Psr\Http\Message\ResponseInterface;
7+
138
abstract class Handler
149
{
1510
/**
16-
* @var Handler
11+
* @var Handler|null
1712
*/
1813
private $successor = null;
1914

20-
/**
21-
* Append a responsibility to the end of chain.
22-
*
23-
* A prepend method could be done with the same spirit
24-
*
25-
* You could also send the successor in the constructor but in PHP that is a
26-
* bad idea because you have to remove the type-hint of the parameter because
27-
* the last handler has a null successor.
28-
*
29-
* And if you override the constructor, that Handler can no longer have a
30-
* successor. One solution is to provide a NullObject (see pattern).
31-
* It is more preferable to keep the constructor "free" to inject services
32-
* you need with the DiC of symfony2 for example.
33-
*
34-
* @param Handler $handler
35-
*/
36-
final public function append(Handler $handler)
15+
public function __construct(Handler $handler = null)
3716
{
38-
if (is_null($this->successor)) {
39-
$this->successor = $handler;
40-
} else {
41-
$this->successor->append($handler);
42-
}
17+
$this->successor = $handler;
4318
}
4419

4520
/**
46-
* Handle the request.
47-
*
4821
* This approach by using a template method pattern ensures you that
49-
* each subclass will not forget to call the successor. Besides, the returned
50-
* boolean value indicates you if the request have been processed or not.
22+
* each subclass will not forget to call the successor
5123
*
52-
* @param Request $req
24+
* @param RequestInterface $request
5325
*
54-
* @return bool
26+
* @return string|null
5527
*/
56-
final public function handle(Request $req)
28+
final public function handle(RequestInterface $request)
5729
{
58-
$req->forDebugOnly = get_called_class();
59-
$processed = $this->processing($req);
60-
if (!$processed) {
30+
$processed = $this->processing($request);
31+
32+
if ($processed === null) {
6133
// the request has not been processed by this handler => see the next
62-
if (!is_null($this->successor)) {
63-
$processed = $this->successor->handle($req);
34+
if ($this->successor !== null) {
35+
$processed = $this->successor->handle($request);
6436
}
6537
}
6638

6739
return $processed;
6840
}
6941

70-
/**
71-
* Each concrete handler has to implement the processing of the request.
72-
*
73-
* @param Request $req
74-
*
75-
* @return bool true if the request has been processed
76-
*/
77-
abstract protected function processing(Request $req);
42+
abstract protected function processing(RequestInterface $request);
7843
}

Behavioral/ChainOfResponsibilities/README.rst

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,7 @@ UML Diagram
3131
Code
3232
----
3333

34-
You can also find these code on `GitHub`_
35-
36-
Request.php
37-
38-
.. literalinclude:: Request.php
39-
:language: php
40-
:linenos:
34+
You can also find this code on `GitHub`_
4135

4236
Handler.php
4337

Behavioral/ChainOfResponsibilities/Request.php

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

Behavioral/ChainOfResponsibilities/Responsible/FastStorage.php

Lines changed: 0 additions & 40 deletions
This file was deleted.
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
4+
5+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class HttpInMemoryCacheHandler extends Handler
9+
{
10+
/**
11+
* @var array
12+
*/
13+
private $data;
14+
15+
/**
16+
* @param array $data
17+
* @param Handler|null $successor
18+
*/
19+
public function __construct(array $data, Handler $successor = null)
20+
{
21+
parent::__construct($successor);
22+
23+
$this->data = $data;
24+
}
25+
26+
/**
27+
* @param RequestInterface $request
28+
*
29+
* @return string|null
30+
*/
31+
protected function processing(RequestInterface $request)
32+
{
33+
$key = sprintf(
34+
'%s?%s',
35+
$request->getUri()->getPath(),
36+
$request->getUri()->getQuery()
37+
);
38+
39+
if ($request->getMethod() == 'GET' && isset($this->data[$key])) {
40+
return $this->data[$key];
41+
}
42+
43+
return null;
44+
}
45+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace DesignPatterns\Behavioral\ChainOfResponsibilities\Responsible;
4+
5+
use DesignPatterns\Behavioral\ChainOfResponsibilities\Handler;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
class SlowDatabaseHandler extends Handler
9+
{
10+
/**
11+
* @param RequestInterface $request
12+
*
13+
* @return string|null
14+
*/
15+
protected function processing(RequestInterface $request)
16+
{
17+
// this is a mockup, in production code you would ask a slow (compared to in-memory) DB for the results
18+
19+
return 'Hello World!';
20+
}
21+
}

Behavioral/ChainOfResponsibilities/Responsible/SlowStorage.php

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

0 commit comments

Comments
 (0)