1
|
|
|
<?php |
2
|
|
|
declare(strict_types = 1); |
3
|
|
|
|
4
|
|
|
namespace Innmind\Compose\Definition\Service\Constructor; |
5
|
|
|
|
6
|
|
|
use Innmind\Compose\{ |
7
|
|
|
Definition\Service\Constructor, |
8
|
|
|
Exception\ValueNotSupported, |
9
|
|
|
Lazy, |
10
|
|
|
Compilation\Service\Constructor as CompiledConstructor, |
11
|
|
|
Compilation\Service\Constructor\Factory as CompiledFactory, |
12
|
|
|
Compilation\Service\Argument as CompiledArgument |
13
|
|
|
}; |
14
|
|
|
use Innmind\Immutable\{ |
15
|
|
|
Str, |
16
|
|
|
Sequence |
17
|
|
|
}; |
18
|
|
|
|
19
|
|
|
final class Factory implements Constructor |
20
|
|
|
{ |
21
|
|
|
private $class; |
22
|
|
|
private $method; |
23
|
|
|
|
24
|
4 |
|
private function __construct(string $class, string $method) |
25
|
|
|
{ |
26
|
4 |
|
$this->class = $class; |
27
|
4 |
|
$this->method = $method; |
28
|
4 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* {@inheritdoc} |
32
|
|
|
*/ |
33
|
19 |
|
public static function fromString(Str $value): Constructor |
34
|
|
|
{ |
35
|
19 |
|
if (!$value->matches('~^\S+::\S+$~')) { |
36
|
15 |
|
throw new ValueNotSupported((string) $value); |
37
|
|
|
} |
38
|
|
|
|
39
|
4 |
|
[$class, $method] = $value->split('::'); |
40
|
|
|
|
41
|
4 |
|
return new self((string) $class, (string) $method); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* {@inheritdoc} |
46
|
|
|
*/ |
47
|
|
|
public function __invoke(...$arguments): object |
48
|
|
|
{ |
49
|
2 |
|
$arguments = Sequence::of(...$arguments)->map(static function($argument) { |
50
|
2 |
|
if ($argument instanceof Lazy) { |
51
|
1 |
|
return $argument->load(); |
52
|
|
|
} |
53
|
|
|
|
54
|
2 |
|
return $argument; |
55
|
2 |
|
}); |
56
|
|
|
|
57
|
2 |
|
return [$this->class, $this->method](...$arguments); |
58
|
|
|
} |
59
|
|
|
|
60
|
1 |
|
public function compile(CompiledArgument ...$arguments): CompiledConstructor |
61
|
|
|
{ |
62
|
1 |
|
return new CompiledFactory($this->class, $this->method, ...$arguments); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
public function __toString(): string |
66
|
|
|
{ |
67
|
1 |
|
return sprintf('%s::%s', $this->class, $this->method); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|