|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Ray\Di; |
|
6
|
|
|
|
|
7
|
|
|
use Exception; |
|
|
|
|
|
|
8
|
|
|
use LogicException; |
|
9
|
|
|
use Ray\Di\Exception\Unbound; |
|
10
|
|
|
use ReflectionMethod; |
|
11
|
|
|
|
|
12
|
|
|
use function call_user_func_array; |
|
13
|
|
|
use function is_callable; |
|
14
|
|
|
|
|
15
|
|
|
final class SetterMethod implements AcceptInterface |
|
16
|
|
|
{ |
|
17
|
|
|
private readonly string $method; |
|
18
|
|
|
private readonly Arguments $arguments; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* Is optional binding ? |
|
22
|
|
|
*/ |
|
23
|
|
|
private bool $isOptional = false; |
|
24
|
|
|
|
|
25
|
|
|
public function __construct(ReflectionMethod $method, Name $name) |
|
26
|
|
|
{ |
|
27
|
|
|
$this->method = $method->name; |
|
|
|
|
|
|
28
|
|
|
$this->arguments = new Arguments($method, $name); |
|
|
|
|
|
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param object $instance |
|
33
|
|
|
* |
|
34
|
|
|
* @throws Unbound |
|
35
|
|
|
* @throws Exception |
|
36
|
|
|
*/ |
|
37
|
|
|
public function __invoke($instance, Container $container): void |
|
38
|
|
|
{ |
|
39
|
|
|
try { |
|
40
|
|
|
$parameters = $this->arguments->inject($container); |
|
41
|
|
|
} catch (Unbound $unbound) { |
|
42
|
|
|
if ($this->isOptional) { |
|
43
|
|
|
return; |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
throw $unbound; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
$callable = [$instance, $this->method]; |
|
50
|
|
|
if (! is_callable($callable)) { |
|
51
|
|
|
throw new LogicException(); // @codeCoverageIgnore |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
call_user_func_array($callable, $parameters); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function setOptional(): void |
|
58
|
|
|
{ |
|
59
|
|
|
$this->isOptional = true; |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** @inheritDoc */ |
|
63
|
|
|
public function accept(VisitorInterface $visitor): void |
|
64
|
|
|
{ |
|
65
|
|
|
try { |
|
66
|
|
|
$visitor->visitSetterMethod($this->method, $this->arguments); |
|
67
|
|
|
} catch (Unbound $unbound) { |
|
68
|
|
|
if ($this->isOptional) { |
|
69
|
|
|
// Return when no dependency given and @Inject(optional=true) annotated to setter method. |
|
70
|
|
|
return; |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// Throw exception when no dependency given and @Inject(optional=false) annotated to setter method. |
|
74
|
|
|
throw $unbound; |
|
75
|
|
|
} |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
Let?s assume that you have a directory layout like this:
. |-- OtherDir | |-- Bar.php | `-- Foo.php `-- SomeDir `-- Foo.phpand let?s assume the following content of
Bar.php:If both files
OtherDir/Foo.phpandSomeDir/Foo.phpare loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.phpHowever, as
OtherDir/Foo.phpdoes not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: