1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Ivory Serializer package. |
5
|
|
|
* |
6
|
|
|
* (c) Eric GELOEN <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please read the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Ivory\Serializer\Mapping\Factory; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\AnnotationReader; |
15
|
|
|
use Ivory\Serializer\Mapping\ClassMetadata; |
16
|
|
|
use Ivory\Serializer\Mapping\ClassMetadataInterface; |
17
|
|
|
use Ivory\Serializer\Mapping\Loader\AnnotationClassMetadataLoader; |
18
|
|
|
use Ivory\Serializer\Mapping\Loader\ChainClassMetadataLoader; |
19
|
|
|
use Ivory\Serializer\Mapping\Loader\ClassMetadataLoaderInterface; |
20
|
|
|
use Ivory\Serializer\Mapping\Loader\ReflectionClassMetadataLoader; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @author GeLo <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class ClassMetadataFactory implements ClassMetadataFactoryInterface |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var ClassMetadataLoaderInterface |
29
|
|
|
*/ |
30
|
|
|
private $loader; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ClassMetadataInterface[] |
34
|
|
|
*/ |
35
|
|
|
private $classMetadatas = []; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ClassMetadataLoaderInterface $loader |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ClassMetadataLoaderInterface $loader) |
41
|
|
|
{ |
42
|
|
|
$this->loader = $loader; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param ClassMetadataLoaderInterface[] $loaders |
47
|
|
|
* |
48
|
|
|
* @return ClassMetadataFactoryInterface |
49
|
|
|
*/ |
50
|
|
|
public static function create(array $loaders = []) |
51
|
|
|
{ |
52
|
|
|
if (empty($loaders)) { |
53
|
|
|
$loaders[] = class_exists(AnnotationReader::class) |
54
|
|
|
? new AnnotationClassMetadataLoader(new AnnotationReader()) |
55
|
|
|
: new ReflectionClassMetadataLoader(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return new static(count($loaders) > 1 ? new ChainClassMetadataLoader($loaders) : array_shift($loaders)); |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritdoc} |
63
|
|
|
*/ |
64
|
|
|
public function getClassMetadata($class) |
65
|
|
|
{ |
66
|
|
|
if (array_key_exists($class, $this->classMetadatas)) { |
67
|
|
|
return $this->classMetadatas[$class]; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
$classMetadata = new ClassMetadata($class); |
71
|
|
|
$found = false; |
72
|
|
|
|
73
|
|
|
if (($parentMetadata = $this->getParentClassMetadata($class)) !== null) { |
74
|
|
|
$classMetadata->merge($parentMetadata); |
75
|
|
|
$found = true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$found = $this->loader->loadClassMetadata($classMetadata) || $found; |
79
|
|
|
|
80
|
|
|
return $this->classMetadatas[$class] = $found ? $classMetadata : null; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param string $class |
85
|
|
|
* |
86
|
|
|
* @return ClassMetadataInterface|null |
87
|
|
|
*/ |
88
|
|
|
private function getParentClassMetadata($class) |
89
|
|
|
{ |
90
|
|
|
if (($parent = get_parent_class($class)) !== false) { |
91
|
|
|
return $this->getClassMetadata($parent); |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
} |
95
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: