1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Intraxia\Jaxion\Core; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
6
|
|
|
use Doctrine\Common\Annotations\AnnotationRegistry; |
7
|
|
|
use ReflectionClass; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* HooksReader class. |
11
|
|
|
*/ |
12
|
|
|
class HooksReader { |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Construct a new HooksReader instance. |
16
|
|
|
*/ |
17
|
3 |
|
private function __construct() { |
18
|
3 |
|
$this->reader = new AnnotationReader(); |
|
|
|
|
19
|
3 |
|
AnnotationRegistry::registerFile( |
|
|
|
|
20
|
1 |
|
__DIR__ . '/Annotations.php' |
21
|
2 |
|
); |
22
|
3 |
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Get the Annotation reader. |
26
|
|
|
* |
27
|
|
|
* @return AnnotationReader |
28
|
|
|
*/ |
29
|
30 |
|
private function reader() { |
30
|
30 |
|
return $this->reader; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Get the shared reader instance. |
35
|
|
|
* |
36
|
|
|
* @return HooksReader |
37
|
|
|
*/ |
38
|
30 |
|
static private function instance() { |
39
|
30 |
|
static $instance; |
40
|
|
|
|
41
|
30 |
|
if ( ! $instance ) { |
42
|
3 |
|
$instance = new HooksReader(); |
43
|
2 |
|
} |
44
|
|
|
|
45
|
30 |
|
return $instance; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Shared implementation of hook reading logic. |
50
|
|
|
* |
51
|
|
|
* @param object $target Object to read annotations from. |
52
|
|
|
* @param \Closure $condition Whether the annotation should be registered. |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
30 |
|
static public function read( $target, $condition ) { |
56
|
30 |
|
$reader = static::instance()->reader(); |
|
|
|
|
57
|
30 |
|
$rmethods = (new ReflectionClass( $target ))->getMethods(); |
58
|
|
|
|
59
|
30 |
|
$hooks = []; |
60
|
|
|
|
61
|
30 |
|
foreach ( $rmethods as $rmethod ) { |
62
|
30 |
|
foreach ( $reader->getMethodAnnotations( $rmethod ) as $annotation ) { |
63
|
24 |
|
if ( $condition( $annotation ) ) { |
64
|
24 |
|
$hooks[] = [ |
65
|
24 |
|
'hook' => $annotation->hook, |
66
|
24 |
|
'method' => $rmethod->getName(), |
|
|
|
|
67
|
24 |
|
'priority' => $annotation->priority, |
68
|
24 |
|
'args' => $annotation->args, |
69
|
|
|
]; |
70
|
16 |
|
} |
71
|
16 |
|
} |
72
|
16 |
|
} |
73
|
|
|
|
74
|
24 |
|
return $hooks; |
75
|
|
|
} |
76
|
|
|
} |
77
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: