|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* Copyright (c) 2013 Janos Szurovecz |
|
4
|
|
|
* |
|
5
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy of |
|
6
|
|
|
* this software and associated documentation files (the "Software"), to deal in |
|
7
|
|
|
* the Software without restriction, including without limitation the rights to |
|
8
|
|
|
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies |
|
9
|
|
|
* of the Software, and to permit persons to whom the Software is furnished to do |
|
10
|
|
|
* so, subject to the following conditions: |
|
11
|
|
|
* |
|
12
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
13
|
|
|
* copies or substantial portions of the Software. |
|
14
|
|
|
* |
|
15
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
16
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
17
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
18
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
19
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
20
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
21
|
|
|
* SOFTWARE. |
|
22
|
|
|
*/ |
|
23
|
|
|
|
|
24
|
|
|
namespace predaddy\messagehandling; |
|
25
|
|
|
|
|
26
|
|
|
use precore\lang\ClassCastException; |
|
27
|
|
|
use precore\lang\NullPointerException; |
|
28
|
|
|
use precore\lang\Object; |
|
29
|
|
|
use precore\lang\ObjectClass; |
|
30
|
|
|
use precore\lang\ObjectInterface; |
|
31
|
|
|
use precore\util\Objects; |
|
32
|
|
|
use precore\util\Preconditions; |
|
33
|
|
|
use ReflectionClass; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* Validates a function or a method whether it is a valid message handler or not. |
|
37
|
|
|
* The following rules must be followed the given function/method: |
|
38
|
|
|
* - exactly one parameter |
|
39
|
|
|
* - the parameter has typehint |
|
40
|
|
|
* |
|
41
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
42
|
|
|
*/ |
|
43
|
|
|
class DefaultFunctionDescriptor extends Object implements FunctionDescriptor |
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* @var string |
|
47
|
|
|
*/ |
|
48
|
|
|
private $handledMessageClass = null; |
|
49
|
|
|
private $compatibleMessageClassNames = []; |
|
50
|
|
|
private $valid = false; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @var CallableWrapper |
|
54
|
|
|
*/ |
|
55
|
|
|
private $callableWrapper; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @var int |
|
59
|
|
|
*/ |
|
60
|
|
|
private $priority; |
|
61
|
|
|
|
|
62
|
|
|
public function __construct(CallableWrapper $callableWrapper, $priority) |
|
63
|
|
|
{ |
|
64
|
|
|
$this->priority = (int) $priority; |
|
65
|
|
|
$this->callableWrapper = $callableWrapper; |
|
66
|
|
|
$this->valid = $this->check(); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param object $message |
|
71
|
|
|
* @return boolean |
|
72
|
|
|
*/ |
|
73
|
|
|
public function isHandlerFor($message) |
|
74
|
|
|
{ |
|
75
|
|
|
if (!$this->valid) { |
|
76
|
|
|
return false; |
|
77
|
|
|
} |
|
78
|
|
|
$messageClassName = get_class($message); |
|
79
|
|
|
if (!array_key_exists($messageClassName, $this->compatibleMessageClassNames)) { |
|
80
|
|
|
$this->compatibleMessageClassNames[$messageClassName] = $this->canHandleValidMessage($message); |
|
81
|
|
|
} |
|
82
|
|
|
return $this->compatibleMessageClassNames[$messageClassName]; |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @return boolean |
|
87
|
|
|
*/ |
|
88
|
|
|
public function isValid() |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->valid; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return string |
|
95
|
|
|
*/ |
|
96
|
|
|
public function getHandledMessageClassName() |
|
97
|
|
|
{ |
|
98
|
|
|
return $this->handledMessageClass; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* @return int |
|
103
|
|
|
*/ |
|
104
|
|
|
public function getPriority() |
|
105
|
|
|
{ |
|
106
|
|
|
return $this->priority; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
/** |
|
110
|
|
|
* @param $object |
|
111
|
|
|
* @return int a negative integer, zero, or a positive integer |
|
112
|
|
|
* as this object is less than, equal to, or greater than the specified object. |
|
113
|
|
|
* @throws ClassCastException - if the specified object's type prevents it from being compared to this object. |
|
114
|
|
|
* @throws NullPointerException if the specified object is null |
|
115
|
|
|
*/ |
|
116
|
|
|
public function compareTo($object) |
|
117
|
|
|
{ |
|
118
|
|
|
Preconditions::checkNotNull($object, 'Compared object cannot be null'); |
|
119
|
|
|
$this->getObjectClass()->cast($object); |
|
120
|
|
|
return $object->priority - $this->priority; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
public function equals(ObjectInterface $object = null) |
|
124
|
|
|
{ |
|
125
|
|
|
if ($object === $this) { |
|
126
|
|
|
return true; |
|
127
|
|
|
} |
|
128
|
|
|
/* @var $object self */ |
|
129
|
|
|
return $object !== null |
|
130
|
|
|
&& $this->getClassName() === $object->getClassName() |
|
131
|
|
|
&& Objects::equal($this->callableWrapper, $object->callableWrapper) |
|
132
|
|
|
&& Objects::equal($this->priority, $object->priority); |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
/** |
|
136
|
|
|
* @return CallableWrapper |
|
137
|
|
|
*/ |
|
138
|
|
|
public function getCallableWrapper() |
|
139
|
|
|
{ |
|
140
|
|
|
return $this->callableWrapper; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
|
|
protected function canHandleValidMessage($object) |
|
144
|
|
|
{ |
|
145
|
|
|
return is_a($object, $this->handledMessageClass); |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
protected function getBaseMessageClassName() |
|
149
|
|
|
{ |
|
150
|
|
|
return null; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
private function check() |
|
154
|
|
|
{ |
|
155
|
|
|
$params = $this->callableWrapper->reflectionFunction()->getParameters(); |
|
156
|
|
|
if (count($params) !== 1) { |
|
157
|
|
|
return false; |
|
158
|
|
|
} |
|
159
|
|
|
/* @var $paramType ReflectionClass */ |
|
160
|
|
|
$paramType = $params[0]->getClass(); |
|
161
|
|
|
if ($paramType === null) { |
|
162
|
|
|
return false; |
|
163
|
|
|
} |
|
164
|
|
|
$baseClassName = $this->getBaseMessageClassName(); |
|
|
|
|
|
|
165
|
|
|
$acceptableType = $baseClassName === null || ObjectClass::forName($baseClassName)->isAssignableFrom($paramType); |
|
166
|
|
|
if (!$acceptableType) { |
|
167
|
|
|
$deadMessage = DeadMessage::objectClass()->isAssignableFrom($paramType); |
|
168
|
|
|
if (!$deadMessage) { |
|
169
|
|
|
return false; |
|
170
|
|
|
} |
|
171
|
|
|
} |
|
172
|
|
|
$this->handledMessageClass = $paramType->getName(); |
|
173
|
|
|
return true; |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
This check looks for function or method calls that always return null and whose return value is assigned to a variable.
The method
getObject()can return nothing but null, so it makes no sense to assign that value to a variable.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.