1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of Spiral Framework package. |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Spiral\Attributes\Internal\Instantiator; |
13
|
|
|
|
14
|
|
|
use Doctrine\Common\Annotations\NamedArgumentConstructorAnnotation as MarkerInterface; |
15
|
|
|
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor as MarkerAnnotation; |
16
|
|
|
use Spiral\Attributes\AttributeReader; |
17
|
|
|
use Spiral\Attributes\NamedArgumentConstructor; |
18
|
|
|
use Spiral\Attributes\ReaderInterface; |
19
|
|
|
|
20
|
|
|
final class Facade implements InstantiatorInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var DoctrineInstantiator |
24
|
|
|
*/ |
25
|
|
|
private $doctrine; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var NamedArgumentsInstantiator |
29
|
|
|
*/ |
30
|
|
|
private $named; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var ReaderInterface |
34
|
|
|
*/ |
35
|
|
|
private $reader; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param ReaderInterface|null $reader |
39
|
|
|
*/ |
40
|
|
|
public function __construct(ReaderInterface $reader = null) |
41
|
|
|
{ |
42
|
|
|
$this->reader = $reader ?? new AttributeReader($this); |
43
|
|
|
$this->doctrine = new DoctrineInstantiator(); |
44
|
|
|
$this->named = new NamedArgumentsInstantiator(); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
|
|
public function instantiate(\ReflectionClass $attr, array $arguments, \Reflector $context = null): object |
51
|
|
|
{ |
52
|
|
|
if ($this->isNamedArguments($attr)) { |
53
|
|
|
return $this->named->instantiate($attr, $arguments, $context); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
return $this->doctrine->instantiate($attr, $arguments, $context); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param \ReflectionClass $class |
61
|
|
|
* @return bool |
62
|
|
|
*/ |
63
|
|
|
private function isNamedArguments(\ReflectionClass $class): bool |
64
|
|
|
{ |
65
|
|
|
return $this->providesNamedArgumentsInterface($class) || |
66
|
|
|
$this->providesNamedArgumentsAttribute($class) |
67
|
|
|
; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param \ReflectionClass $class |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
private function providesNamedArgumentsAttribute(\ReflectionClass $class): bool |
75
|
|
|
{ |
76
|
|
|
if (\class_exists(MarkerAnnotation::class)) { |
77
|
|
|
return $this->reader->firstClassMetadata($class, MarkerAnnotation::class) !== null; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return $this->reader->firstClassMetadata($class, NamedArgumentConstructor::class); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param \ReflectionClass $class |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
private function providesNamedArgumentsInterface(\ReflectionClass $class): bool |
88
|
|
|
{ |
89
|
|
|
return \is_subclass_of($class->getName(), MarkerInterface::class); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|