|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
5
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
6
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
7
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
8
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
9
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
10
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
11
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
12
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
13
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
14
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
15
|
|
|
* |
|
16
|
|
|
* The software is based on the Axon Framework project which is |
|
17
|
|
|
* licensed under the Apache 2.0 license. For more information on the Axon Framework |
|
18
|
|
|
* see <http://www.axonframework.org/>. |
|
19
|
|
|
* |
|
20
|
|
|
* This software consists of voluntary contributions made by many individuals |
|
21
|
|
|
* and is licensed under the MIT license. For more information, see |
|
22
|
|
|
* <http://www.governor-framework.org/>. |
|
23
|
|
|
*/ |
|
24
|
|
|
|
|
25
|
|
|
namespace Governor\Framework\CommandHandling\Handlers; |
|
26
|
|
|
|
|
27
|
|
|
use Governor\Framework\Common\Annotation\SimpleAnnotationReaderFactory; |
|
28
|
|
|
use Governor\Framework\Common\Annotation\AnnotationReaderFactoryInterface; |
|
29
|
|
|
use Governor\Framework\Common\ParameterResolverFactoryInterface; |
|
30
|
|
|
use Governor\Framework\Common\PayloadParameterResolver; |
|
31
|
|
|
use Governor\Framework\Domain\MessageInterface; |
|
32
|
|
|
use Governor\Framework\CommandHandling\CommandHandlerInterface; |
|
33
|
|
|
|
|
34
|
|
|
/** |
|
35
|
|
|
* Description of AbstractAnnotatedCommandHandler |
|
36
|
|
|
* |
|
37
|
|
|
* @author "David Kalosi" <[email protected]> |
|
38
|
|
|
* @license <a href="http://www.opensource.org/licenses/mit-license.php">MIT License</a> |
|
39
|
|
|
*/ |
|
40
|
|
|
abstract class AbstractAnnotatedCommandHandler implements CommandHandlerInterface |
|
41
|
|
|
{ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var \ReflectionMethod |
|
45
|
|
|
*/ |
|
46
|
|
|
private $method; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var array |
|
50
|
|
|
*/ |
|
51
|
|
|
private $annotations; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var ParameterResolverFactoryInterface |
|
55
|
|
|
*/ |
|
56
|
|
|
private $parameterResolver; |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var AnnotationReaderFactoryInterface |
|
60
|
|
|
*/ |
|
61
|
|
|
private $annotationReaderFactory; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $className |
|
65
|
|
|
* @param string $methodName |
|
66
|
|
|
* @param ParameterResolverFactoryInterface $parameterResolver |
|
67
|
|
|
* @param AnnotationReaderFactoryInterface $annotationReaderFactory |
|
68
|
|
|
*/ |
|
69
|
4 |
|
function __construct( |
|
|
|
|
|
|
70
|
|
|
$className, |
|
71
|
|
|
$methodName, |
|
72
|
|
|
ParameterResolverFactoryInterface $parameterResolver, |
|
73
|
|
|
AnnotationReaderFactoryInterface $annotationReaderFactory = null |
|
74
|
|
|
) { |
|
75
|
4 |
|
$this->method = new \ReflectionMethod($className, $methodName); |
|
76
|
|
|
|
|
77
|
4 |
|
$this->annotationReaderFactory = null === $annotationReaderFactory ? new SimpleAnnotationReaderFactory( |
|
78
|
4 |
|
) : $annotationReaderFactory; |
|
79
|
|
|
|
|
80
|
4 |
|
$this->annotations = $this->annotationReaderFactory->getReader()->getMethodAnnotations($this->method); |
|
81
|
4 |
|
$this->parameterResolver = $parameterResolver; |
|
82
|
4 |
|
} |
|
83
|
|
|
|
|
84
|
|
|
/** |
|
85
|
|
|
* @return \ReflectionMethod |
|
86
|
|
|
*/ |
|
87
|
3 |
|
public function getMethod() |
|
88
|
|
|
{ |
|
89
|
3 |
|
return $this->method; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* @return array |
|
94
|
|
|
*/ |
|
95
|
|
|
public function getAnnotations() |
|
96
|
|
|
{ |
|
97
|
|
|
return $this->annotations; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
/** |
|
101
|
|
|
* @return ParameterResolverFactoryInterface |
|
102
|
|
|
*/ |
|
103
|
|
|
public function getParameterResolver() |
|
104
|
|
|
{ |
|
105
|
|
|
return $this->parameterResolver; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* @param MessageInterface $message |
|
110
|
|
|
* @return array |
|
111
|
|
|
*/ |
|
112
|
3 |
|
protected function resolveArguments(MessageInterface $message) |
|
113
|
|
|
{ |
|
114
|
3 |
|
$arguments = []; |
|
115
|
3 |
|
$parameters = $this->method->getParameters(); |
|
116
|
3 |
|
$count = count($parameters); |
|
117
|
|
|
|
|
118
|
3 |
|
for ($cc = 0; $cc < $count; $cc++) { |
|
119
|
3 |
|
if ($cc === 0) { |
|
120
|
3 |
|
$resolver = new PayloadParameterResolver($message->getPayloadType()); |
|
121
|
3 |
|
$arguments[] = $resolver->resolveParameterValue($message); |
|
122
|
3 |
|
} else { |
|
123
|
|
|
$resolver = $this->parameterResolver->createInstance( |
|
124
|
|
|
$this->annotations, |
|
125
|
|
|
$parameters[$cc] |
|
126
|
|
|
); |
|
127
|
|
|
|
|
128
|
|
|
$arguments[] = $resolver->resolveParameterValue($message); |
|
129
|
|
|
} |
|
130
|
3 |
|
} |
|
131
|
|
|
|
|
132
|
3 |
|
return $arguments; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
} |
|
136
|
|
|
|
Adding explicit visibility (
private,protected, orpublic) is generally recommend to communicate to other developers how, and from where this method is intended to be used.