-
-
Notifications
You must be signed in to change notification settings - Fork 108
Expand file tree
/
Copy pathAnnotationsAppender.php
More file actions
138 lines (120 loc) · 5.03 KB
/
AnnotationsAppender.php
File metadata and controls
138 lines (120 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
<?php
/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
declare(strict_types=1);
namespace ApiPlatform\SchemaGenerator\ClassMutator;
use ApiPlatform\SchemaGenerator\AnnotationGenerator\AnnotationGeneratorInterface;
use ApiPlatform\SchemaGenerator\Model\Class_;
use ApiPlatform\SchemaGenerator\Model\Use_;
use ApiPlatform\SchemaGenerator\Schema\Rdf\RdfResource;
final class AnnotationsAppender implements ClassMutatorInterface
{
/** @var Class_[] */
private array $classes;
/** @var AnnotationGeneratorInterface[] */
private array $annotationGenerators;
/** @var RdfResource[] */
private array $typesToGenerate;
/**
* @param Class_[] $classes
* @param AnnotationGeneratorInterface[] $annotationGenerators
* @param RdfResource[] $typesToGenerate
*/
public function __construct(array $classes, array $annotationGenerators, array $typesToGenerate)
{
$this->annotationGenerators = $annotationGenerators;
$this->classes = $classes;
$this->typesToGenerate = $typesToGenerate;
}
/**
* @param array{} $context
*/
public function __invoke(Class_ $class, array $context): void
{
$this->generateClassUses($class);
$this->generateClassAnnotations($class);
if (false === isset($this->typesToGenerate[$class->name()])) {
$this->generateInterfaceAnnotations($class);
}
$this->generateConstantAnnotations($class);
$this->generatePropertiesAnnotations($class);
}
private function generateClassUses(Class_ $class): void
{
$interfaceNamespace = isset($this->classes[$class->name()]) ? $this->classes[$class->name()]->interfaceNamespace() : null;
if ($interfaceNamespace && $class->interfaceNamespace() !== $class->namespace) {
$class->addUse(new Use_(sprintf('%s\\%s', $class->interfaceNamespace(), $class->interfaceName())));
}
foreach ($class->properties() as $property) {
if ($property->reference && $property->reference->interfaceName()) {
$class->addUse(new Use_(sprintf(
'%s\\%s',
$property->reference->interfaceNamespace(),
$property->reference->interfaceName()
)));
}
}
foreach ($this->annotationGenerators as $generator) {
foreach ($generator->generateUses($class) as $use) {
$class->addUse($use);
}
}
}
private function generateClassAnnotations(Class_ $class): void
{
foreach ($this->annotationGenerators as $generator) {
foreach ($generator->generateClassAnnotations($class) as $annotation) {
$class->addAnnotation($annotation);
}
}
}
private function generateConstantAnnotations(Class_ $class): void
{
foreach ($class->constants() as $constant) {
foreach ($this->annotationGenerators as $generator) {
foreach ($generator->generateConstantAnnotations($constant) as $constantAnnotation) {
$constant->addAnnotation($constantAnnotation);
}
}
}
}
private function generateInterfaceAnnotations(Class_ $class): void
{
foreach ($this->annotationGenerators as $generator) {
foreach ($generator->generateInterfaceAnnotations($class) as $interfaceAnnotation) {
$class->addInterfaceAnnotation($interfaceAnnotation);
}
}
}
private function generatePropertiesAnnotations(Class_ $class): void
{
foreach ($class->properties() as $property) {
foreach ($this->annotationGenerators as $annotationGenerator) {
foreach ($annotationGenerator->generatePropertyAnnotations($property, $class->name()) as $propertyAnnotation) {
$property->addAnnotation($propertyAnnotation);
}
foreach ($annotationGenerator->generateGetterAnnotations($property) as $getterAnnotation) {
$property->addGetterAnnotation($getterAnnotation);
}
if ($property->isArray()) {
foreach ($annotationGenerator->generateAdderAnnotations($property) as $adderAnnotation) {
$property->addAdderAnnotation($adderAnnotation);
}
foreach ($annotationGenerator->generateRemoverAnnotations($property) as $removerAnnotation) {
$property->addRemoverAnnotation($removerAnnotation);
}
} else {
foreach ($annotationGenerator->generateSetterAnnotations($property) as $setterAnnotation) {
$property->addSetterAnnotation($setterAnnotation);
}
}
}
}
}
}