1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TomPHP\ContainerConfigurator; |
4
|
|
|
|
5
|
|
|
use Assert\Assertion; |
6
|
|
|
use InvalidArgumentException; |
7
|
|
|
use TomPHP\ContainerConfigurator\Exception\InvalidConfigException; |
8
|
|
|
|
9
|
|
|
final class ServiceDefinition |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @var string |
13
|
|
|
*/ |
14
|
|
|
private $name; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var string |
18
|
|
|
*/ |
19
|
|
|
private $class; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var bool |
23
|
|
|
*/ |
24
|
|
|
private $isSingleton; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var bool |
28
|
|
|
*/ |
29
|
|
|
private $isFactory; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
private $arguments; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var array |
38
|
|
|
*/ |
39
|
|
|
private $methods; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param string $name |
43
|
|
|
* @param array $config |
44
|
|
|
* @param bool $singletonDefault |
45
|
|
|
* |
46
|
|
|
* @throws InvalidArgumentException |
47
|
|
|
* @throws InvalidConfigException |
48
|
|
|
*/ |
49
|
|
|
public function __construct($name, array $config, $singletonDefault = false) |
50
|
|
|
{ |
51
|
|
|
Assertion::string($name); |
52
|
|
|
Assertion::boolean($singletonDefault); |
53
|
|
|
|
54
|
|
|
$this->name = $name; |
55
|
|
|
$this->class = $this->className($name, $config); |
56
|
|
|
$this->isSingleton = isset($config['singleton']) ? $config['singleton'] : $singletonDefault; |
57
|
|
|
$this->isFactory = isset($config['factory']); |
58
|
|
|
$this->arguments = isset($config['arguments']) ? $config['arguments'] : []; |
59
|
|
|
$this->methods = isset($config['methods']) ? $config['methods'] : []; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return string |
64
|
|
|
*/ |
65
|
|
|
public function getName() |
66
|
|
|
{ |
67
|
|
|
return $this->name; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
|
|
public function getClass() |
74
|
|
|
{ |
75
|
|
|
return $this->class; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
|
|
public function isSingleton() |
82
|
|
|
{ |
83
|
|
|
return $this->isSingleton; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return bool |
88
|
|
|
*/ |
89
|
|
|
public function isFactory() |
90
|
|
|
{ |
91
|
|
|
return $this->isFactory; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @return array |
96
|
|
|
*/ |
97
|
|
|
public function getArguments() |
98
|
|
|
{ |
99
|
|
|
return $this->arguments; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
|
|
public function getMethods() |
106
|
|
|
{ |
107
|
|
|
return $this->methods; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @param string $name |
112
|
|
|
* @param array $config |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
* |
116
|
|
|
* @throws InvalidConfigException |
117
|
|
|
*/ |
118
|
|
|
private function className($name, array $config) |
119
|
|
|
{ |
120
|
|
|
if (isset($config['class']) && isset($config['factory'])) { |
121
|
|
|
throw InvalidConfigException::fromNameWhenClassAndFactorySpecified($name); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
if (isset($config['class'])) { |
125
|
|
|
return $config['class']; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
if (isset($config['factory'])) { |
129
|
|
|
return $config['factory']; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $name; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|