Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 43 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 1 |
AnnotationProcessor | |
0.00% |
0 / 43 |
|
0.00% |
0 / 11 |
702 | |
0.00% |
0 / 1 |
add | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
addNamespace | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
createAnnotationsPerType | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
processAll | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
isProcessAllowed | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
process | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
processClass | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
6 | |||
processProperties | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
processMethods | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
processSubject | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace Dynart\Micro\Middleware; |
4 | |
5 | use Dynart\Micro\Micro; |
6 | use Dynart\Micro\Middleware; |
7 | use Dynart\Micro\Annotation; |
8 | use Dynart\Micro\MicroException; |
9 | |
10 | /** |
11 | * Processes the annotations that are in the PHPDoc comments |
12 | * @package Dynart\Micro |
13 | */ |
14 | class AnnotationProcessor implements Middleware { |
15 | |
16 | /** @var string[] */ |
17 | protected $annotationClasses = []; |
18 | |
19 | /** @var Annotation[][] */ |
20 | protected $annotations = [ |
21 | Annotation::TYPE_CLASS => [], |
22 | Annotation::TYPE_PROPERTY => [], |
23 | Annotation::TYPE_METHOD => [] |
24 | ]; |
25 | |
26 | /** @var string[] */ |
27 | protected $namespaces = []; |
28 | |
29 | /** |
30 | * Adds an annotation for processing |
31 | * |
32 | * The given class name should implement the Annotation interface, otherwise |
33 | * it will throw an MicroException. |
34 | * |
35 | * @throws MicroException if the given class does not implement the Annotation |
36 | * @param string $className The class name |
37 | */ |
38 | public function add(string $className) { |
39 | if (!is_subclass_of($className, Annotation::class)) { |
40 | throw new MicroException("$className doesn't implement the Annotation interface"); |
41 | } |
42 | $this->annotationClasses[] = $className; |
43 | } |
44 | |
45 | /** |
46 | * Adds a namespace |
47 | * |
48 | * If one or more namespace added only those will be processed. The namespace should NOT start with a backslash! |
49 | * |
50 | * @param string $namespace |
51 | */ |
52 | public function addNamespace(string $namespace) { |
53 | $this->namespaces[] = $namespace; |
54 | } |
55 | |
56 | /** |
57 | * Creates the annotations then processes all interfaces in the App or those that are in the given namespaces. |
58 | */ |
59 | public function run() { |
60 | $this->createAnnotationsPerType(); |
61 | $this->processAll(); |
62 | } |
63 | |
64 | /** |
65 | * Creates the annotation instances and puts them into the right `$annotations` array |
66 | */ |
67 | protected function createAnnotationsPerType(): void { |
68 | foreach ($this->annotationClasses as $className) { |
69 | $annotation = Micro::get($className); |
70 | foreach ($annotation->types() as $type) { |
71 | $this->annotations[$type][] = $annotation; |
72 | } |
73 | } |
74 | } |
75 | |
76 | /** |
77 | * Processes all interfaces in the App or those are in the given namespaces |
78 | */ |
79 | protected function processAll(): void { |
80 | foreach (Micro::interfaces() as $className) { |
81 | if ($this->isProcessAllowed($className)) { |
82 | $this->process($className); |
83 | } |
84 | } |
85 | } |
86 | |
87 | /** |
88 | * If no namespace added returns true, otherwise checks the namespace and returns true if the interface is in it. |
89 | * @param string $className The name of the class |
90 | * @return bool Should we process this class? |
91 | */ |
92 | protected function isProcessAllowed(string $className): bool { |
93 | if (empty($this->namespaces)) { |
94 | return true; |
95 | } |
96 | foreach ($this->namespaces as $namespace) { |
97 | if (substr($className, 0, strlen($namespace)) == $namespace) { |
98 | return true; |
99 | } |
100 | } |
101 | return false; |
102 | } |
103 | |
104 | /** |
105 | * Processes one class with the given name |
106 | * @param string $className The name of the class |
107 | */ |
108 | protected function process(string $className): void { |
109 | try { |
110 | $refClass = new \ReflectionClass($className); |
111 | } catch (\ReflectionException $ignore) { |
112 | throw new MicroException("Can't create reflection for: $className"); |
113 | } |
114 | $this->processClass($refClass); |
115 | $this->processProperties($refClass); |
116 | $this->processMethods($refClass); |
117 | } |
118 | |
119 | /** |
120 | * Processes all class type annotations for the class |
121 | * @param $refClass |
122 | */ |
123 | protected function processClass(\ReflectionClass $refClass): void { |
124 | foreach ($this->annotations[Annotation::TYPE_CLASS] as $annotation) { |
125 | $this->processSubject($annotation, Annotation::TYPE_CLASS, $refClass->getName(), $refClass); |
126 | } |
127 | } |
128 | |
129 | /** |
130 | * Processes all property type annotations for all the properties of a class |
131 | * @param $refClass |
132 | */ |
133 | protected function processProperties(\ReflectionClass $refClass): void { |
134 | $refProperties = $refClass->getProperties(); |
135 | foreach ($this->annotations[Annotation::TYPE_PROPERTY] as $annotation) { |
136 | foreach ($refProperties as $refProperty) { |
137 | $this->processSubject($annotation, Annotation::TYPE_PROPERTY, $refClass->getName(), $refProperty); |
138 | } |
139 | } |
140 | } |
141 | |
142 | /** |
143 | * Processes all method type annotations for all the methods of a class |
144 | * @param $refClass |
145 | */ |
146 | protected function processMethods(\ReflectionClass $refClass): void { |
147 | $refMethods = $refClass->getMethods(); |
148 | foreach ($this->annotations[Annotation::TYPE_METHOD] as $annotation) { |
149 | foreach ($refMethods as $refMethod) { |
150 | $this->processSubject($annotation, Annotation::TYPE_METHOD, $refClass->getName(), $refMethod); |
151 | } |
152 | } |
153 | } |
154 | |
155 | /** |
156 | * Processes a class, property or a method annotation |
157 | * |
158 | * Gets the PHPDoc comment from the subject, search for the annotation name in it. |
159 | * If the annotation name present does the regular expression search and calls the `Annotation::process()` |
160 | * with the results. |
161 | * |
162 | * @param Annotation $annotation The annotation |
163 | * @param string $type The type of the annotation |
164 | * @param string $className The class name |
165 | * @param \ReflectionClass|\ReflectionProperty|\ReflectionMethod $subject The reflection class, property or method |
166 | */ |
167 | protected function processSubject(Annotation $annotation, string $type, string $className, $subject) { |
168 | $comment = $subject->getDocComment(); |
169 | $has = strpos($comment, '* @'.$annotation->name()) !== false; |
170 | if ($has) { |
171 | $matches = []; |
172 | $commentWithoutNewLines = str_replace(array("\r", "\n"), ' ', $comment); |
173 | $fullRegex = '/\*\s@'.$annotation->name().'\s'.$annotation->regex().'\s\*/U'; |
174 | preg_match($fullRegex, $commentWithoutNewLines, $matches); |
175 | $annotation->process($type, $className, $subject, $commentWithoutNewLines, $matches); |
176 | } |
177 | } |
178 | } |