1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Definition\Dependency; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\{ |
7
|
|
|
Definition\Name, |
8
|
|
|
Definition\Dependency, |
9
|
|
|
Services, |
10
|
|
|
Exception\ArgumentNotProvided, |
11
|
|
|
Exception\ArgumentNotDefined, |
12
|
|
|
Exception\NameNotNamespaced, |
13
|
|
|
Exception\ReferenceNotFound, |
14
|
|
|
Compilation\Dependency\Parameter as CompiledParameter |
15
|
|
|
}; |
16
|
|
|
use Innmind\Immutable\Str; |
17
|
|
|
|
18
|
|
|
final class Parameter |
19
|
|
|
{ |
20
|
|
|
private $name; |
21
|
|
|
private $value; |
22
|
|
|
private $reference; |
23
|
|
|
|
24
|
33 |
|
private function __construct(Name $name) |
25
|
|
|
{ |
26
|
33 |
|
$this->name = $name; |
27
|
33 |
|
} |
28
|
|
|
|
29
|
33 |
|
public static function fromValue(Name $name, $value): self |
30
|
|
|
{ |
31
|
33 |
|
if (!is_string($value)) { |
32
|
13 |
|
$self = new self($name); |
33
|
13 |
|
$self->value = $value; |
34
|
|
|
|
35
|
13 |
|
return $self; |
36
|
|
|
} |
37
|
|
|
|
38
|
28 |
|
$value = Str::of($value); |
39
|
|
|
|
40
|
28 |
|
if ((string) $value->substring(0, 1) !== '$') { |
41
|
8 |
|
$self = new self($name); |
42
|
8 |
|
$self->value = (string) $value; |
43
|
|
|
|
44
|
8 |
|
return $self; |
45
|
|
|
} |
46
|
|
|
|
47
|
27 |
|
$self = new self($name); |
48
|
27 |
|
$self->reference = new Name((string) $value->substring(1)); |
49
|
|
|
|
50
|
27 |
|
return $self; |
51
|
|
|
} |
52
|
|
|
|
53
|
16 |
|
public function name(): Name |
54
|
|
|
{ |
55
|
16 |
|
return $this->name; |
56
|
|
|
} |
57
|
|
|
|
58
|
17 |
|
public function resolve(Services $services) |
59
|
|
|
{ |
60
|
17 |
|
if (!$this->reference instanceof Name) { |
61
|
4 |
|
return $this->value; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
try { |
65
|
15 |
|
return $services->arguments()->get($this->reference); |
66
|
11 |
|
} catch (ArgumentNotProvided $e) { |
67
|
3 |
|
if ($e->argument()->hasDefault()) { |
68
|
1 |
|
return $services->build($e->argument()->default()); |
69
|
|
|
} |
70
|
|
|
|
71
|
2 |
|
if ($e->argument()->optional()) { |
72
|
1 |
|
return null; |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
|
throw $e; |
76
|
8 |
|
} catch (ArgumentNotDefined $e) { |
77
|
|
|
//pass |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
try { |
81
|
8 |
|
return $services->dependencies()->build($this->reference); |
82
|
6 |
|
} catch (ReferenceNotFound $e) { |
83
|
|
|
//pass |
84
|
|
|
} |
85
|
|
|
|
86
|
6 |
|
return $services->build($this->reference); |
87
|
|
|
} |
88
|
|
|
|
89
|
7 |
|
public function refersTo(Dependency $dependeny): bool |
90
|
|
|
{ |
91
|
7 |
|
if (!$this->reference instanceof Name) { |
92
|
2 |
|
return false; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
try { |
96
|
6 |
|
$root = $this->reference->root(); |
97
|
1 |
|
} catch (NameNotNamespaced $e) { |
98
|
1 |
|
return false; |
99
|
|
|
} |
100
|
|
|
|
101
|
5 |
|
if (!$root->equals($dependeny->name())) { |
102
|
4 |
|
return false; |
103
|
|
|
} |
104
|
|
|
|
105
|
4 |
|
return $dependeny->has($this->reference->withoutRoot()); |
106
|
|
|
} |
107
|
|
|
|
108
|
6 |
|
public function compile(): CompiledParameter |
109
|
|
|
{ |
110
|
6 |
|
if (!$this->reference instanceof Name) { |
111
|
5 |
|
return CompiledParameter::raw($this->name, $this->value); |
112
|
|
|
} |
113
|
|
|
|
114
|
5 |
|
return CompiledParameter::reference($this->name, $this->reference); |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|