1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AnnotationReader.php |
5
|
|
|
* |
6
|
|
|
* Jaxon annotation manager. |
7
|
|
|
* |
8
|
|
|
* @package jaxon-core |
|
|
|
|
9
|
|
|
* @copyright 2022 Thierry Feuzeu <[email protected]> |
10
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause BSD 3-Clause License |
11
|
|
|
* @link https://github.com/jaxon-php/jaxon-core |
12
|
|
|
*/ |
|
|
|
|
13
|
|
|
|
14
|
|
|
namespace Jaxon\Annotations; |
15
|
|
|
|
16
|
|
|
use Jaxon\Annotations\Annotation\AbstractAnnotation; |
17
|
|
|
use Jaxon\Annotations\Annotation\AfterAnnotation; |
18
|
|
|
use Jaxon\Annotations\Annotation\BeforeAnnotation; |
19
|
|
|
use Jaxon\Annotations\Annotation\DataBagAnnotation; |
20
|
|
|
use Jaxon\Annotations\Annotation\ExcludeAnnotation; |
21
|
|
|
use Jaxon\Annotations\Annotation\UploadAnnotation; |
22
|
|
|
use mindplay\annotations\AnnotationException; |
23
|
|
|
use mindplay\annotations\AnnotationManager; |
24
|
|
|
|
25
|
|
|
use function array_filter; |
26
|
|
|
use function count; |
27
|
|
|
use function is_a; |
28
|
|
|
|
29
|
|
|
class AnnotationReader |
|
|
|
|
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var AnnotationManager |
33
|
|
|
*/ |
34
|
|
|
protected $xManager; |
|
|
|
|
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The constructor |
38
|
|
|
* |
39
|
|
|
* @param AnnotationManager $xManager |
|
|
|
|
40
|
|
|
*/ |
41
|
|
|
public function __construct(AnnotationManager $xManager) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
$this->xManager = $xManager; |
|
|
|
|
44
|
|
|
$this->xManager->registry['upload'] = UploadAnnotation::class; |
45
|
|
|
$this->xManager->registry['databag'] = DataBagAnnotation::class; |
46
|
|
|
$this->xManager->registry['exclude'] = ExcludeAnnotation::class; |
47
|
|
|
$this->xManager->registry['before'] = BeforeAnnotation::class;; |
48
|
|
|
$this->xManager->registry['after'] = AfterAnnotation::class;; |
49
|
|
|
// Missing standard annotations. |
50
|
|
|
// We need to define this, otherwise they throw an exception, and make the whole processing fail. |
51
|
|
|
$this->xManager->registry['const'] = false; |
|
|
|
|
52
|
|
|
$this->xManager->registry['inheritDoc'] = false; |
53
|
|
|
} |
|
|
|
|
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param array $aAnnotations |
|
|
|
|
57
|
|
|
* |
58
|
|
|
* @return AbstractAnnotation[] |
59
|
|
|
*/ |
60
|
|
|
private function filterAnnotations(array $aAnnotations): array |
61
|
|
|
{ |
62
|
|
|
// Only keep the annotations declared in this package. |
63
|
|
|
$aAnnotations = array_filter($aAnnotations, function($xAnnotation) { |
64
|
|
|
return is_a($xAnnotation, AbstractAnnotation::class); |
65
|
|
|
}); |
66
|
|
|
|
67
|
|
|
$aAttributes = []; |
68
|
|
|
foreach($aAnnotations as $xAnnotation) |
69
|
|
|
{ |
70
|
|
|
$sName = $xAnnotation->getName(); |
71
|
|
|
if(isset($aAttributes[$sName])) |
72
|
|
|
{ |
73
|
|
|
$xAnnotation->setPrevValue($aAttributes[$sName]); |
74
|
|
|
} |
75
|
|
|
$xValue = $xAnnotation->getValue(); |
76
|
|
|
if($sName === 'protected' && !$xValue) |
77
|
|
|
{ |
78
|
|
|
continue; // Ignore annotation @exclude with value false |
79
|
|
|
} |
80
|
|
|
$aAttributes[$sName] = $xValue; |
81
|
|
|
} |
82
|
|
|
return $aAttributes; |
83
|
|
|
} |
|
|
|
|
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Get the class attributes from its annotations |
87
|
|
|
* |
88
|
|
|
* @param string $sClass |
|
|
|
|
89
|
|
|
* @param array $aMethods |
|
|
|
|
90
|
|
|
* |
91
|
|
|
* @return array |
92
|
|
|
* @throws AnnotationException |
93
|
|
|
*/ |
94
|
|
|
public function getAttributes(string $sClass, array $aMethods): array |
95
|
|
|
{ |
96
|
|
|
// Class annotations |
97
|
|
|
$aClassAttrs = $this->filterAnnotations($this->xManager->getClassAnnotations($sClass)); |
98
|
|
|
if(isset($aClassAttrs['protected'])) |
99
|
|
|
{ |
100
|
|
|
return [true, [], []]; // The entire class is not to be exported. |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
$aProtected = []; |
104
|
|
|
$aAttributes = []; |
105
|
|
|
if(count($aClassAttrs) > 0) |
106
|
|
|
{ |
107
|
|
|
$aAttributes['*'] = $aClassAttrs; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
// Methods annotations |
111
|
|
|
foreach($aMethods as $sMethod) |
112
|
|
|
{ |
113
|
|
|
$aMethodAttrs = $this->filterAnnotations($this->xManager->getMethodAnnotations($sClass, $sMethod)); |
114
|
|
|
if(isset($aMethodAttrs['protected'])) |
115
|
|
|
{ |
116
|
|
|
$aProtected[] = $sMethod; // The method is not to be exported. |
117
|
|
|
} |
118
|
|
|
elseif(count($aMethodAttrs) > 0) |
119
|
|
|
{ |
120
|
|
|
$aAttributes[$sMethod] = $aMethodAttrs; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
return [false, $aAttributes, $aProtected]; |
124
|
|
|
} |
|
|
|
|
125
|
|
|
} |
126
|
|
|
|