1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of DivineNii opensource projects. |
7
|
|
|
* |
8
|
|
|
* PHP version 7.4 and above required |
9
|
|
|
* |
10
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
11
|
|
|
* @copyright 2021 DivineNii (https://divinenii.com/) |
12
|
|
|
* @license https://opensource.org/licenses/BSD-3-Clause License |
13
|
|
|
* |
14
|
|
|
* For the full copyright and license information, please view the LICENSE |
15
|
|
|
* file that was distributed with this source code. |
16
|
|
|
*/ |
17
|
|
|
|
18
|
|
|
namespace Rade\DI\Definitions; |
19
|
|
|
|
20
|
|
|
use PhpParser\Node\Expr; |
21
|
|
|
use PhpParser\Node\Stmt\Return_; |
22
|
|
|
use Rade\DI\Exceptions\ContainerResolutionException; |
23
|
|
|
use Rade\DI\Exceptions\ServiceCreationException; |
24
|
|
|
use Rade\DI\Resolver; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Represents a definition service that shouldn't be resolved. |
28
|
|
|
* |
29
|
|
|
* @author Divine Niiquaye Ibok <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class ValueDefinition implements DefinitionInterface, ShareableDefinitionInterface, DepreciableDefinitionInterface |
32
|
|
|
{ |
33
|
|
|
use Traits\DeprecationTrait; |
|
|
|
|
34
|
|
|
use Traits\VisibilityTrait; |
35
|
|
|
|
36
|
|
|
/** @var mixed */ |
37
|
|
|
private $value; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Definition constructor. |
41
|
|
|
* |
42
|
|
|
* @param mixed $value |
43
|
|
|
*/ |
44
|
|
|
public function __construct($value, bool $shared = true) |
45
|
|
|
{ |
46
|
|
|
$this->replace($value); |
47
|
|
|
$this->shared = $shared; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Replace the existing value. |
52
|
|
|
* |
53
|
|
|
* @param mixed $value |
54
|
|
|
* |
55
|
|
|
* @return $this |
56
|
|
|
*/ |
57
|
|
|
public function replace($value) |
58
|
|
|
{ |
59
|
|
|
if ($value instanceof DefinitionInterface) { |
60
|
|
|
throw new ServiceCreationException(\sprintf('A definition entity must not be an instance of "%s".', DefinitionInterface::class)); |
61
|
|
|
} elseif ($value instanceof \PhpParser\Node && !$value instanceof Expr) { |
62
|
|
|
throw new ServiceCreationException(\sprintf('A definition entity must be an instance of "%s".', Expr::class)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
$this->value = $value; |
66
|
|
|
|
67
|
|
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritdoc} |
72
|
|
|
*/ |
73
|
|
|
public function getEntity() |
74
|
|
|
{ |
75
|
|
|
return $this->value; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
|
|
public function build(string $id, Resolver $resolver) |
82
|
|
|
{ |
83
|
|
|
$builder = $resolver->getBuilder(); |
84
|
|
|
|
85
|
|
|
if ($this->abstract) { |
86
|
|
|
throw new ContainerResolutionException(\sprintf('Resolving an abstract definition %s is not allowed.', $id)); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
if (!empty($this->deprecation)) { |
90
|
|
|
$deprecation = $this->triggerDeprecation($id, $builder); |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
if (\is_array($value = $this->value)) { |
94
|
|
|
$value = $resolver->resolveArguments($value); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
if (null === $builder) { |
98
|
|
|
return !\is_array($value) && $this->lazy ? $resolver->resolve($value) : $value; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$defNode = $builder->method($resolver->createMethod($id))->makeProtected(); |
102
|
|
|
|
103
|
|
|
if ($value instanceof \PhpParser\Node) { |
104
|
|
|
if ($value instanceof Expr\Array_) { |
105
|
|
|
$defNode->setReturnType('array'); |
106
|
|
|
} elseif ($value instanceof Expr\New_) { |
107
|
|
|
$defNode->setReturnType($value->class->toString()); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} elseif (\PHP_MAJOR_VERSION >= 8) { |
110
|
|
|
$defNode->setReturnType('mixed'); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if (isset($deprecation)) { |
114
|
|
|
$defNode->addStmt($deprecation); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
if ($this->lazy) { |
118
|
|
|
$lazyMethod = \is_array($value) ? 'resolveArguments' : 'resolve'; |
119
|
|
|
$createdValue = $builder->methodCall($builder->propertyFetch($builder->var('this'), 'resolver'), $lazyMethod, [$value]); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
if ($this->shared) { |
123
|
|
|
$createdValue = $this->triggerSharedBuild($id, $createdValue ?? $builder->val($value), $builder); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
return $defNode->addStmt(new Return_($createdValue ?? $builder->val($value))); |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|