|
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\annotation; |
|
25
|
|
|
|
|
26
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
|
27
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
|
28
|
|
|
use Doctrine\Common\Annotations\CachedReader; |
|
29
|
|
|
use Doctrine\Common\Annotations\Reader; |
|
30
|
|
|
use Doctrine\Common\Cache\ArrayCache; |
|
31
|
|
|
use precore\lang\ObjectClass; |
|
32
|
|
|
use predaddy\messagehandling\FunctionDescriptor; |
|
33
|
|
|
use predaddy\messagehandling\FunctionDescriptorFactory; |
|
34
|
|
|
use predaddy\messagehandling\MessageHandlerDescriptor; |
|
35
|
|
|
use predaddy\messagehandling\MethodWrapper; |
|
36
|
|
|
use ReflectionMethod; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Finds handler methods which are annotated with Subscribe. |
|
40
|
|
|
* |
|
41
|
|
|
* @author Janos Szurovecz <[email protected]> |
|
42
|
|
|
*/ |
|
43
|
|
|
class AnnotatedMessageHandlerDescriptor implements MessageHandlerDescriptor |
|
44
|
|
|
{ |
|
45
|
|
|
/** |
|
46
|
|
|
* @var Reader |
|
47
|
|
|
*/ |
|
48
|
|
|
private static $reader; |
|
49
|
|
|
|
|
50
|
|
|
private $handlerClass; |
|
51
|
|
|
private $descriptors = null; |
|
52
|
|
|
private $handler; |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @var FunctionDescriptorFactory |
|
56
|
|
|
*/ |
|
57
|
|
|
private $functionDescriptorFactory; |
|
58
|
|
|
|
|
59
|
|
|
public static function init() |
|
60
|
|
|
{ |
|
61
|
|
|
self::$reader = new CachedReader(new AnnotationReader(), new ArrayCache()); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* @return Reader |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function getReader() |
|
68
|
|
|
{ |
|
69
|
|
|
return self::$reader; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param Reader $reader |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function setReader(Reader $reader) |
|
76
|
|
|
{ |
|
77
|
|
|
self::$reader = $reader; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param object $handler |
|
82
|
|
|
* @param FunctionDescriptorFactory $functionDescFactory |
|
83
|
|
|
*/ |
|
84
|
|
|
public function __construct($handler, FunctionDescriptorFactory $functionDescFactory) |
|
85
|
|
|
{ |
|
86
|
|
|
$this->handlerClass = ObjectClass::forName(get_class($handler)); |
|
87
|
|
|
$this->handler = $handler; |
|
88
|
|
|
$this->functionDescriptorFactory = $functionDescFactory; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return FunctionDescriptor[] |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getFunctionDescriptors() |
|
95
|
|
|
{ |
|
96
|
|
|
if ($this->descriptors === null) { |
|
97
|
|
|
$this->descriptors = $this->findHandlerMethods(); |
|
98
|
|
|
} |
|
99
|
|
|
return $this->descriptors; |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* @return FunctionDescriptor[] |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function findHandlerMethods() |
|
106
|
|
|
{ |
|
107
|
|
|
$result = []; |
|
108
|
|
|
/* @var $reflMethod ReflectionMethod */ |
|
109
|
|
|
foreach ($this->handlerClass->getMethods($this->methodVisibility()) as $reflMethod) { |
|
110
|
|
|
$methodAnnotation = self::getReader()->getMethodAnnotation($reflMethod, __NAMESPACE__ . '\Subscribe'); |
|
111
|
|
|
if ($methodAnnotation === null) { |
|
112
|
|
|
continue; |
|
113
|
|
|
} |
|
114
|
|
|
$funcDescriptor = $this->functionDescriptorFactory->create( |
|
115
|
|
|
new MethodWrapper($this->handler, $reflMethod), |
|
116
|
|
|
$methodAnnotation->priority |
|
117
|
|
|
); |
|
118
|
|
|
if (!$funcDescriptor->isValid()) { |
|
119
|
|
|
continue; |
|
120
|
|
|
} |
|
121
|
|
|
$reflMethod->setAccessible(true); |
|
122
|
|
|
$result[] = $funcDescriptor; |
|
123
|
|
|
} |
|
124
|
|
|
return $result; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
protected function methodVisibility() |
|
128
|
|
|
{ |
|
129
|
|
|
return ReflectionMethod::IS_PUBLIC; |
|
130
|
|
|
} |
|
131
|
|
|
} |
|
132
|
|
|
AnnotationRegistry::registerFile(__DIR__ . '/MessageHandlingAnnotations.php'); |
|
133
|
|
|
AnnotatedMessageHandlerDescriptor::init(); |
|
134
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.