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