|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of the Phalcon Framework. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Phalcon Team <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE.txt |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
* |
|
11
|
|
|
* Implementation of this file has been influenced by AuraPHP |
|
12
|
|
|
* |
|
13
|
|
|
* @link https://github.com/auraphp/Aura.Di |
|
14
|
|
|
* @license https://github.com/auraphp/Aura.Di/blob/4.x/LICENSE |
|
15
|
|
|
*/ |
|
16
|
|
|
|
|
17
|
|
|
declare(strict_types=1); |
|
18
|
|
|
|
|
19
|
|
|
namespace Phalcon\Container\Resolver; |
|
20
|
|
|
|
|
21
|
|
|
use Phalcon\Container\Exception\NoSuchProperty; |
|
22
|
|
|
use Phalcon\Container\Injection\LazyNew; |
|
23
|
|
|
use ReflectionException; |
|
24
|
|
|
use ReflectionParameter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* This extension of the Resolver additionally auto-resolves unspecified |
|
28
|
|
|
* constructor params according to their typehints; use with caution as it can |
|
29
|
|
|
* be very difficult to debug. |
|
30
|
|
|
* |
|
31
|
|
|
* @property ValueObject $types |
|
32
|
|
|
*/ |
|
33
|
|
|
class AutoResolver extends Resolver |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* Auto-resolve these typehints to these values. |
|
37
|
|
|
* |
|
38
|
|
|
* @var ValueObject |
|
39
|
|
|
*/ |
|
40
|
|
|
protected ValueObject $types; |
|
|
|
|
|
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* AutoResolver constructor. |
|
44
|
|
|
* |
|
45
|
|
|
* @param Reflector $reflector |
|
46
|
|
|
*/ |
|
47
|
8 |
|
public function __construct(Reflector $reflector) |
|
48
|
|
|
{ |
|
49
|
8 |
|
parent::__construct($reflector); |
|
50
|
|
|
|
|
51
|
8 |
|
$this->types = new ValueObject(); |
|
52
|
8 |
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* Auto-resolves params typehinted to classes. |
|
56
|
|
|
* |
|
57
|
|
|
* @param ReflectionParameter $rparam A parameter reflection. |
|
58
|
|
|
* @param string $class The class name to return values for. |
|
59
|
|
|
* @param array $parent The parent unified params. |
|
60
|
|
|
* |
|
61
|
|
|
* @return LazyNew|mixed|DefaultValueParameter|UnresolvedParameter |
|
62
|
|
|
* @throws ReflectionException |
|
63
|
|
|
* @throws NoSuchProperty |
|
64
|
|
|
*/ |
|
65
|
4 |
|
protected function getUnifiedParameter(ReflectionParameter $rparam, string $class, array $parent) |
|
66
|
|
|
{ |
|
67
|
4 |
|
$unified = parent::getUnifiedParameter($rparam, $class, $parent); |
|
68
|
|
|
|
|
69
|
|
|
// already resolved? |
|
70
|
4 |
|
if (!$unified instanceof UnresolvedParameter && !$unified instanceof DefaultValueParameter) { |
|
71
|
1 |
|
return $unified; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// use an explicit auto-resolution? |
|
75
|
3 |
|
$rtype = $rparam->getClass(); |
|
76
|
3 |
|
if ($rtype && $this->types->has($rtype->name)) { |
|
77
|
1 |
|
return $this->types->get($rtype->name); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
3 |
|
if ($unified instanceof DefaultValueParameter) { |
|
81
|
2 |
|
return $unified; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
// use a lazy-new-instance of the typehinted class? |
|
85
|
2 |
|
if ($rtype && $rtype->isInstantiable()) { |
|
86
|
1 |
|
return new LazyNew($this, new Blueprint($rtype->name)); |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
// $unified is still an UnresolvedParam |
|
90
|
1 |
|
return $unified; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* @return ValueObject |
|
95
|
|
|
*/ |
|
96
|
3 |
|
public function types(): ValueObject |
|
97
|
|
|
{ |
|
98
|
3 |
|
return $this->types; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|