1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Webino (http://webino.sk) |
4
|
|
|
* |
5
|
|
|
* @link https://github.com/webino for the canonical source repository |
6
|
|
|
* @copyright Copyright (c) 2015-2017 Webino, s.r.o. (http://webino.sk) |
7
|
|
|
* @author Peter Bačinský <[email protected]> |
8
|
|
|
* @license BSD-3-Clause |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace WebinoAppLib\Event; |
12
|
|
|
|
13
|
|
|
use WebinoAppLib\Application\AbstractApplication; |
14
|
|
|
use WebinoAppLib\Application\ConfiguredApplicationInterface; |
15
|
|
|
use WebinoBaseLib\Util\ToString; |
16
|
|
|
use Zend\Http\AbstractMessage; |
17
|
|
|
use Zend\Stdlib\RequestInterface; |
18
|
|
|
use Zend\Stdlib\ResponseInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class DispatchEvent |
22
|
|
|
*/ |
23
|
|
|
class DispatchEvent extends AppEvent implements |
24
|
|
|
DispatchEventInterface |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* @param ConfiguredApplicationInterface|AbstractApplication $app |
28
|
|
|
*/ |
29
|
|
|
public function __construct(ConfiguredApplicationInterface $app) |
30
|
|
|
{ |
31
|
|
|
parent::__construct(AppEvent::DISPATCH); |
32
|
|
|
$this->setApp($app); |
|
|
|
|
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return RequestInterface |
37
|
|
|
*/ |
38
|
|
|
public function getRequest() |
39
|
|
|
{ |
40
|
|
|
return $this->getEventParam($this::REQUEST); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param RequestInterface $request |
45
|
|
|
* @return $this |
46
|
|
|
*/ |
47
|
|
|
public function setRequest(RequestInterface $request) |
48
|
|
|
{ |
49
|
|
|
$this->setEventParam($this::REQUEST, $request); |
50
|
|
|
return $this; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return ResponseInterface|AbstractMessage |
55
|
|
|
*/ |
56
|
|
|
public function getResponse() |
57
|
|
|
{ |
58
|
|
|
return $this->getEventParam($this::RESPONSE); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Set response |
63
|
|
|
* |
64
|
|
|
* It sets response content when response is a string. |
65
|
|
|
* It joins values to a string when response is an array. |
66
|
|
|
* It converts to string when response is an object. |
67
|
|
|
* It sets response object when response is an interface. |
68
|
|
|
* |
69
|
|
|
* @param string|array|ResponseInterface $response |
70
|
|
|
* @return $this |
71
|
|
|
*/ |
72
|
|
|
public function setResponse($response) |
73
|
|
|
{ |
74
|
|
|
if ($response instanceof ResponseInterface) { |
75
|
|
|
$this->setEventParam($this::RESPONSE, $response); |
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$_response = $this->getResponse(); |
80
|
|
|
$_response->setContent($_response->getContent() . $this->normalizeResponseContent($response)); |
81
|
|
|
return $this; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Reset response |
86
|
|
|
* |
87
|
|
|
* @param string|array|ResponseInterface $response |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
|
|
public function resetResponse($response = null) |
91
|
|
|
{ |
92
|
|
|
if ($response instanceof ResponseInterface) { |
93
|
|
|
// TODO implement?? |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
$this->getResponse()->setContent($this->normalizeResponseContent($response)); |
|
|
|
|
97
|
|
|
return $this; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param string|array $content |
102
|
|
|
* @return string |
103
|
|
|
*/ |
104
|
|
|
private function normalizeResponseContent($content) |
105
|
|
|
{ |
106
|
|
|
return ToString::value($content); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: