1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Mikemirten\Component\JsonApi\Mapper\Definition; |
5
|
|
|
use Mikemirten\Component\JsonApi\Mapper\Definition\AnnotationProcessor\AnnotationProcessorInterface; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Annotation Definition Provider based on the Doctrine-Annotation library |
9
|
|
|
* |
10
|
|
|
* @package Mikemirten\Component\JsonApi\Mapper\Definition |
11
|
|
|
*/ |
12
|
|
|
class AnnotationDefinitionProvider implements DefinitionProviderInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Registered annotation processors |
16
|
|
|
* |
17
|
|
|
* @var AnnotationProcessorInterface[] |
18
|
|
|
*/ |
19
|
|
|
private $processors = []; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Cache of created definitions |
23
|
|
|
* |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
private $definitionCache = []; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Register annotation processor |
30
|
|
|
* |
31
|
|
|
* @param AnnotationProcessorInterface $processor |
32
|
|
|
*/ |
33
|
1 |
|
public function registerProcessor(AnnotationProcessorInterface $processor) |
34
|
|
|
{ |
35
|
1 |
|
$this->processors[] = $processor; |
36
|
1 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* {@inheritdoc} |
40
|
|
|
*/ |
41
|
4 |
|
public function getDefinition(string $class): Definition |
42
|
|
|
{ |
43
|
4 |
|
if (! isset($this->definitionCache[$class])) { |
44
|
4 |
|
$reflection = new \ReflectionClass($class); |
45
|
|
|
|
46
|
4 |
|
$this->definitionCache[$class] = $this->createDefinition($reflection); |
47
|
|
|
} |
48
|
|
|
|
49
|
4 |
|
return $this->definitionCache[$class]; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Create definition for given class |
54
|
|
|
* |
55
|
|
|
* @param \ReflectionClass $reflection |
56
|
|
|
* @return Definition |
57
|
|
|
*/ |
58
|
4 |
|
protected function createDefinition(\ReflectionClass $reflection): Definition |
59
|
|
|
{ |
60
|
4 |
|
$definition = new Definition($reflection->getName()); |
|
|
|
|
61
|
|
|
|
62
|
4 |
|
foreach ($this->processors as $processor) |
63
|
|
|
{ |
64
|
1 |
|
$processor->process($reflection, $definition); |
65
|
|
|
} |
66
|
|
|
|
67
|
4 |
|
$parent = $reflection->getParentClass(); |
68
|
|
|
|
69
|
4 |
|
if ($parent !== false) { |
70
|
1 |
|
$definition->merge($this->createDefinition($parent)); |
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
73
|
4 |
|
foreach ($reflection->getTraits() as $trait) |
74
|
|
|
{ |
75
|
1 |
|
$definition->merge($this->createDefinition($trait)); |
|
|
|
|
76
|
|
|
} |
77
|
|
|
|
78
|
4 |
|
return $definition; |
79
|
|
|
} |
80
|
|
|
} |