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