1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Yiisoft\Definitions; |
6
|
|
|
|
7
|
|
|
use Psr\Container\ContainerInterface; |
8
|
|
|
use Yiisoft\Definitions\Contract\DefinitionInterface; |
9
|
|
|
|
10
|
|
|
final class ServiceDefinition implements DefinitionInterface |
11
|
|
|
{ |
12
|
|
|
private array $constructorArguments = []; |
13
|
|
|
private array $calls = []; |
14
|
|
|
|
15
|
|
|
private function __construct(private string $class) |
16
|
|
|
{ |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public static function for(string $class): self |
20
|
|
|
{ |
21
|
|
|
return new self($class); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
public function constructor(array $arguments): self |
25
|
|
|
{ |
26
|
|
|
$this->constructorArguments = $arguments; |
27
|
|
|
return $this; |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
public function call(string $method, array $arguments = []): self |
31
|
|
|
{ |
32
|
|
|
$this->calls[$method . '()'] = $arguments; |
33
|
|
|
return $this; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
public function calls(array $methods): self |
37
|
|
|
{ |
38
|
|
|
foreach ($methods as $method => $arguments) { |
39
|
|
|
$this->call($method, $arguments); |
40
|
|
|
} |
41
|
|
|
return $this; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function set(string $property, mixed $value): self |
45
|
|
|
{ |
46
|
|
|
$this->calls['$' . $property] = $value; |
47
|
|
|
return $this; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
public function sets(array $properties): self |
51
|
|
|
{ |
52
|
|
|
foreach ($properties as $property => $value) { |
53
|
|
|
$this->set($property, $value); |
54
|
|
|
} |
55
|
|
|
return $this; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function resolve(ContainerInterface $container): mixed |
59
|
|
|
{ |
60
|
|
|
$config = [ |
61
|
|
|
ArrayDefinition::CLASS_NAME => $this->class, |
62
|
|
|
ArrayDefinition::CONSTRUCTOR => $this->constructorArguments, |
63
|
|
|
...$this->calls, |
64
|
|
|
]; |
65
|
|
|
return ArrayDefinition::fromConfig($config)->resolve($container); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function merge(self $other) |
|
|
|
|
69
|
|
|
{ |
70
|
|
|
// TBD |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.