1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the ICanBoogie package. |
5
|
|
|
* |
6
|
|
|
* (c) Olivier Laviale <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace ICanBoogie\Routing\RouteDispatcher; |
13
|
|
|
|
14
|
|
|
use ICanBoogie\Event; |
15
|
|
|
use ICanBoogie\HTTP\Request; |
16
|
|
|
use ICanBoogie\HTTP\Response; |
17
|
|
|
use ICanBoogie\Routing\RouteDispatcher; |
18
|
|
|
use ICanBoogie\Routing\Route; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Event class for the `ICanBoogie\Routing\RouteDispatcher::dispatch` event. |
22
|
|
|
* |
23
|
|
|
* Third parties may use this event to alter the response before it is returned by the dispatcher. |
24
|
|
|
* |
25
|
|
|
* @property-read Route $route |
26
|
|
|
* @property-read Request $request |
27
|
|
|
* @property Response $response |
28
|
|
|
*/ |
29
|
|
View Code Duplication |
class DispatchEvent extends Event |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
const TYPE = 'dispatch'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The route. |
35
|
|
|
* |
36
|
|
|
* @var Route |
37
|
|
|
*/ |
38
|
|
|
private $route; |
39
|
|
|
|
40
|
|
|
protected function get_route() |
41
|
|
|
{ |
42
|
|
|
return $this->route; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* The HTTP request. |
47
|
|
|
* |
48
|
|
|
* @var Request |
49
|
|
|
*/ |
50
|
|
|
private $request; |
51
|
|
|
|
52
|
|
|
protected function get_request() |
53
|
|
|
{ |
54
|
|
|
return $this->request; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Reference to the HTTP response. |
59
|
|
|
* |
60
|
|
|
* @var Response |
61
|
|
|
*/ |
62
|
|
|
private $response; |
63
|
|
|
|
64
|
|
|
protected function get_response() |
65
|
|
|
{ |
66
|
|
|
return $this->response; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function set_response(Response $response = null) |
70
|
|
|
{ |
71
|
|
|
$this->response = $response; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* The event is constructed with the type `dispatch`. |
76
|
|
|
* |
77
|
|
|
* @param RouteDispatcher $target |
78
|
|
|
* @param Route $route |
79
|
|
|
* @param Request $request |
80
|
|
|
* @param Response|null $response |
81
|
|
|
*/ |
82
|
|
|
public function __construct(RouteDispatcher $target, Route $route, Request $request, Response &$response = null) |
83
|
|
|
{ |
84
|
|
|
$this->route = $route; |
85
|
|
|
$this->request = $request; |
86
|
|
|
$this->response = &$response; |
87
|
|
|
|
88
|
|
|
parent::__construct($target, self::TYPE); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.