1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Persistence\Mapping; |
6
|
|
|
|
7
|
|
|
use Doctrine\Common\Reflection\RuntimePublicReflectionProperty; |
8
|
|
|
use Doctrine\Common\Reflection\TypedNoDefaultReflectionProperty; |
9
|
|
|
use ReflectionClass; |
10
|
|
|
use ReflectionException; |
11
|
|
|
use ReflectionMethod; |
12
|
|
|
use ReflectionProperty; |
13
|
|
|
use function array_key_exists; |
14
|
|
|
use function class_exists; |
15
|
|
|
use function class_parents; |
16
|
|
|
use function phpversion; |
17
|
|
|
use function version_compare; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* PHP Runtime Reflection Service. |
21
|
|
|
*/ |
22
|
|
|
class RuntimeReflectionService implements ReflectionService |
23
|
|
|
{ |
24
|
|
|
/** @var bool */ |
25
|
|
|
private $supportsTypedPropertiesWorkaround; |
26
|
|
|
|
27
|
7 |
|
public function __construct() |
|
|
|
|
28
|
|
|
{ |
29
|
7 |
|
$this->supportsTypedPropertiesWorkaround = |
30
|
7 |
|
version_compare(phpversion(), '7.4.0') >= 0; |
31
|
7 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
|
|
|
|
34
|
|
|
* {@inheritDoc} |
35
|
|
|
*/ |
|
|
|
|
36
|
2 |
|
public function getParentClasses(string $class) |
37
|
|
|
{ |
38
|
2 |
|
if (! class_exists($class)) { |
39
|
1 |
|
throw MappingException::nonExistingClass($class); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
return class_parents($class); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
|
|
|
|
46
|
|
|
* {@inheritDoc} |
47
|
|
|
*/ |
|
|
|
|
48
|
1 |
|
public function getClassShortName(string $class) |
49
|
|
|
{ |
50
|
1 |
|
$reflectionClass = new ReflectionClass($class); |
51
|
|
|
|
52
|
1 |
|
return $reflectionClass->getShortName(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
|
|
|
|
56
|
|
|
* {@inheritDoc} |
57
|
|
|
*/ |
|
|
|
|
58
|
1 |
|
public function getClassNamespace(string $class) |
59
|
|
|
{ |
60
|
1 |
|
$reflectionClass = new ReflectionClass($class); |
61
|
|
|
|
62
|
1 |
|
return $reflectionClass->getNamespaceName(); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
|
|
|
|
66
|
|
|
* {@inheritDoc} |
67
|
|
|
*/ |
|
|
|
|
68
|
1 |
|
public function getClass(string $class) |
69
|
|
|
{ |
70
|
1 |
|
return new ReflectionClass($class); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
|
|
|
|
74
|
|
|
* {@inheritDoc} |
75
|
|
|
*/ |
|
|
|
|
76
|
1 |
|
public function getAccessibleProperty(string $class, string $property) |
77
|
|
|
{ |
78
|
1 |
|
$reflectionProperty = new ReflectionProperty($class, $property); |
79
|
|
|
|
80
|
1 |
|
if ($reflectionProperty->isPublic()) { |
81
|
1 |
|
$reflectionProperty = new RuntimePublicReflectionProperty($class, $property); |
82
|
1 |
|
} else if ($this->supportsTypedPropertiesWorkaround && ! array_key_exists($property, $this->getClass($class)->getDefaultProperties())) { |
83
|
|
|
$reflectionProperty = new TypedNoDefaultReflectionProperty($class, $property); |
84
|
|
|
} |
85
|
|
|
|
86
|
1 |
|
$reflectionProperty->setAccessible(true); |
87
|
|
|
|
88
|
1 |
|
return $reflectionProperty; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
1 |
|
public function hasPublicMethod(string $class, string $method) |
95
|
|
|
{ |
96
|
|
|
try { |
97
|
1 |
|
$reflectionMethod = new ReflectionMethod($class, $method); |
98
|
1 |
|
} catch (ReflectionException $e) { |
99
|
1 |
|
return false; |
100
|
|
|
} |
101
|
|
|
|
102
|
1 |
|
return $reflectionMethod->isPublic(); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|