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
|
|
|
use phpDocumentor\Reflection\ClassReflector; |
22
|
|
|
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor; |
23
|
|
|
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor; |
24
|
|
|
use Symfony\Component\PropertyInfo\PropertyInfoExtractor; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @author GeLo <[email protected]> |
28
|
|
|
*/ |
29
|
|
|
class ClassMetadataFactory extends AbstractClassMetadataFactory |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var ClassMetadataLoaderInterface |
33
|
|
|
*/ |
34
|
|
|
private $loader; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ClassMetadataLoaderInterface $loader |
38
|
|
|
*/ |
39
|
1512 |
|
public function __construct(ClassMetadataLoaderInterface $loader) |
40
|
|
|
{ |
41
|
1512 |
|
$this->loader = $loader; |
42
|
1512 |
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @param ClassMetadataLoaderInterface[] $loaders |
46
|
|
|
* |
47
|
|
|
* @return ClassMetadataFactoryInterface |
48
|
|
|
*/ |
49
|
1492 |
|
public static function create(array $loaders = []) |
50
|
|
|
{ |
51
|
1492 |
|
if (empty($loaders)) { |
52
|
1492 |
|
$extractor = null; |
53
|
|
|
|
54
|
1492 |
|
if (class_exists(PropertyInfoExtractor::class)) { |
55
|
1492 |
|
$extractors = $typeExtractors = [new ReflectionExtractor()]; |
56
|
|
|
|
57
|
1492 |
|
if (class_exists(ClassReflector::class)) { |
58
|
1492 |
|
array_unshift($typeExtractors, new PhpDocExtractor()); |
59
|
746 |
|
} |
60
|
|
|
|
61
|
1492 |
|
$extractor = new PropertyInfoExtractor($extractors, $typeExtractors, [], $extractors); |
62
|
746 |
|
} |
63
|
|
|
|
64
|
1492 |
|
$loaders = [new ReflectionClassMetadataLoader($extractor)]; |
65
|
|
|
|
66
|
1492 |
|
if (class_exists(AnnotationReader::class)) { |
67
|
1492 |
|
$loaders[] = new AnnotationClassMetadataLoader(new AnnotationReader()); |
68
|
746 |
|
} |
69
|
746 |
|
} |
70
|
|
|
|
71
|
1492 |
|
return new static(count($loaders) > 1 ? new ChainClassMetadataLoader($loaders) : array_shift($loaders)); |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* {@inheritdoc} |
76
|
|
|
*/ |
77
|
1104 |
|
protected function fetchClassMetadata($class) |
78
|
|
|
{ |
79
|
1104 |
|
$classMetadata = new ClassMetadata($class); |
80
|
1100 |
|
$found = false; |
81
|
|
|
|
82
|
1100 |
|
if (($parentMetadata = $this->getParentClassMetadata($class)) !== null) { |
83
|
4 |
|
$classMetadata->merge($parentMetadata); |
84
|
4 |
|
$found = true; |
85
|
2 |
|
} |
86
|
|
|
|
87
|
1100 |
|
$found = $this->loader->loadClassMetadata($classMetadata) || $found; |
88
|
|
|
|
89
|
1100 |
|
return $found ? $classMetadata : null; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* @param string $class |
94
|
|
|
* |
95
|
|
|
* @return ClassMetadataInterface|null |
96
|
|
|
*/ |
97
|
1100 |
|
private function getParentClassMetadata($class) |
98
|
|
|
{ |
99
|
1100 |
|
if (($parent = get_parent_class($class)) !== false) { |
100
|
4 |
|
return $this->getClassMetadata($parent); |
101
|
|
|
} |
102
|
1100 |
|
} |
103
|
|
|
} |
104
|
|
|
|
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: