1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Middleware |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Middleware\Middleware; |
16
|
|
|
|
17
|
|
|
use Phossa2\Route\Dispatcher; |
18
|
|
|
use Psr\Http\Message\RequestInterface; |
19
|
|
|
use Psr\Http\Message\ResponseInterface; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Phossa2RouteMiddleware |
23
|
|
|
* |
24
|
|
|
* Using phossa2/route to dispatching |
25
|
|
|
* |
26
|
|
|
* @package Phossa2\Middleware |
27
|
|
|
* @author Hong Zhang <[email protected]> |
28
|
|
|
* @see MiddlewareAbstract |
29
|
|
|
* @version 2.0.0 |
30
|
|
|
* @since 2.0.0 added |
31
|
|
|
*/ |
32
|
|
|
class Phossa2RouteMiddleware extends MiddlewareAbstract |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var Dispatcher |
36
|
|
|
* @access protected |
37
|
|
|
*/ |
38
|
|
|
protected $dispatcher; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param Dispatcher $dispatcher |
42
|
|
|
* @access public |
43
|
|
|
*/ |
44
|
|
|
public function __construct(Dispatcher $dispatcher) |
45
|
|
|
{ |
46
|
|
|
$this->dispatcher = $dispatcher; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* {@inheritDoc} |
51
|
|
|
*/ |
52
|
|
|
protected function before( |
53
|
|
|
RequestInterface $request, |
54
|
|
|
ResponseInterface $response |
55
|
|
|
)/* : ResponseInterface */ { |
56
|
|
|
$this->dispatcher->dispatch( |
57
|
|
|
$request->getMethod(), |
58
|
|
|
$request->getUri()->getPath(), |
59
|
|
|
['request' => $request, 'response' => $response] |
60
|
|
|
); |
61
|
|
|
|
62
|
|
|
// response in the result |
63
|
|
|
return $this->dispatcher->getResult()->getParameters()['response']; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Doing nothing afterwards |
68
|
|
|
* |
69
|
|
|
* {@inheritDoc} |
70
|
|
|
*/ |
71
|
|
|
protected function after( |
72
|
|
|
RequestInterface $request, |
73
|
|
|
ResponseInterface $response |
74
|
|
|
)/* : ResponseInterface */ { |
75
|
|
|
return $response; |
76
|
|
|
} |
77
|
|
|
} |
78
|
|
|
|