|
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
|
|
|
public function registerProcessor(AnnotationProcessorInterface $processor) |
|
34
|
|
|
{ |
|
35
|
|
|
$this->processors[] = $processor; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* {@inheritdoc} |
|
40
|
|
|
*/ |
|
41
|
|
|
public function getDefinition(string $class): Definition |
|
42
|
|
|
{ |
|
43
|
|
|
if (! isset($this->definitionCache[$class])) { |
|
44
|
|
|
$reflection = new \ReflectionClass($class); |
|
45
|
|
|
|
|
46
|
|
|
$this->definitionCache[$class] = $this->createDefinition($reflection); |
|
47
|
|
|
} |
|
48
|
18 |
|
|
|
49
|
|
|
return $this->definitionCache[$class]; |
|
50
|
18 |
|
} |
|
51
|
1 |
|
|
|
52
|
1 |
|
/** |
|
53
|
1 |
|
* Create definition for given class |
|
54
|
|
|
* |
|
55
|
1 |
|
* @param \ReflectionClass $reflection |
|
56
|
|
|
* @return Definition |
|
57
|
18 |
|
*/ |
|
58
|
|
|
protected function createDefinition(\ReflectionClass $reflection): Definition |
|
59
|
|
|
{ |
|
60
|
|
|
$definition = new Definition($reflection->getName()); |
|
|
|
|
|
|
61
|
|
|
|
|
62
|
|
|
foreach ($this->processors as $processor) |
|
63
|
|
|
{ |
|
64
|
|
|
$processor->process($reflection, $definition); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$parent = $reflection->getParentClass(); |
|
68
|
|
|
|
|
69
|
|
|
if ($parent !== false) { |
|
70
|
|
|
$definition->merge($this->createDefinition($parent)); |
|
|
|
|
|
|
71
|
18 |
|
} |
|
72
|
|
|
|
|
73
|
18 |
|
foreach ($reflection->getTraits() as $trait) |
|
74
|
|
|
{ |
|
75
|
18 |
|
$definition->merge($this->createDefinition($trait)); |
|
|
|
|
|
|
76
|
18 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
return $definition; |
|
79
|
|
|
} |
|
80
|
|
|
} |