1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Doctrine\Annotations\Assembler\Constant; |
6
|
|
|
|
7
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\ClassConstantNotFound; |
8
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\ConstantNotAccessible; |
9
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\ConstantResolutionException; |
10
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\InvalidClass; |
11
|
|
|
use Doctrine\Annotations\Assembler\Constant\Exception\StandaloneConstantNotFound; |
12
|
|
|
use Doctrine\Annotations\Metadata\Reflection\ClassReflectionProvider; |
13
|
|
|
use ReflectionException; |
14
|
|
|
use function assert; |
15
|
|
|
use function constant; |
16
|
|
|
use function defined; |
17
|
|
|
use function strpos; |
18
|
|
|
|
19
|
|
|
final class ReflectionConstantResolver implements ConstantResolver |
20
|
|
|
{ |
21
|
|
|
/** @var ClassReflectionProvider */ |
22
|
|
|
private $classReflectionProvider; |
23
|
|
|
|
24
|
18 |
|
public function __construct(ClassReflectionProvider $classReflectionProvider) |
25
|
|
|
{ |
26
|
18 |
|
$this->classReflectionProvider = $classReflectionProvider; |
27
|
18 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @return mixed |
31
|
|
|
* |
32
|
|
|
* @throws ConstantResolutionException |
33
|
|
|
*/ |
34
|
7 |
|
public function resolveClassOrInterfaceConstant(string $holderName, string $constantName) |
35
|
|
|
{ |
36
|
|
|
try { |
37
|
7 |
|
$classReflection = $this->classReflectionProvider->getClassReflection($holderName); |
38
|
1 |
|
} catch (ReflectionException $e) { |
39
|
1 |
|
throw InvalidClass::new($holderName, $constantName); |
40
|
|
|
} |
41
|
|
|
|
42
|
6 |
|
$constantReflection = $classReflection->getReflectionConstant($constantName); |
43
|
|
|
|
44
|
6 |
|
if ($constantReflection === false) { |
45
|
1 |
|
throw ClassConstantNotFound::new($holderName, $constantName); |
46
|
|
|
} |
47
|
|
|
|
48
|
5 |
|
if (! $constantReflection->isPublic()) { |
49
|
1 |
|
throw ConstantNotAccessible::new($holderName, $constantName); |
50
|
|
|
} |
51
|
|
|
|
52
|
4 |
|
return $constantReflection->getValue(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return mixed |
57
|
|
|
* |
58
|
|
|
* @throws ConstantResolutionException |
59
|
|
|
*/ |
60
|
3 |
|
public function resolveStandaloneConstant(string $constantName) |
61
|
|
|
{ |
62
|
3 |
|
assert(strpos($constantName, '::') === false); |
63
|
|
|
|
64
|
3 |
|
if (! defined($constantName)) { |
65
|
|
|
throw StandaloneConstantNotFound::new($constantName); |
66
|
|
|
} |
67
|
|
|
|
68
|
3 |
|
return constant($constantName); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|