|
1
|
|
|
<?php declare(strict_types = 1); |
|
2
|
|
|
/** |
|
3
|
|
|
* Created by PhpStorm. |
|
4
|
|
|
* User: root |
|
5
|
|
|
* Date: 02.08.16 |
|
6
|
|
|
* Time: 0:46. |
|
7
|
|
|
*/ |
|
8
|
|
|
namespace samsonframework\container\definition\builder; |
|
9
|
|
|
|
|
10
|
|
|
use samsonframework\container\definition\AbstractDefinition; |
|
11
|
|
|
use samsonframework\container\definition\ClassBuilderInterface; |
|
12
|
|
|
use samsonframework\container\definition\ClassDefinition; |
|
13
|
|
|
use samsonframework\container\definition\exception\ClassDefinitionAlreadyExistsException; |
|
14
|
|
|
use samsonframework\container\definition\parameter\exception\ParameterAlreadyExistsException; |
|
15
|
|
|
use samsonframework\container\definition\parameter\ParameterBuilder; |
|
16
|
|
|
use samsonframework\container\definition\parameter\ParameterBuilderInterface; |
|
17
|
|
|
use samsonframework\container\definition\reference\ClassReference; |
|
18
|
|
|
use samsonframework\container\definition\reference\ReferenceDependencyInterface; |
|
19
|
|
|
use samsonframework\container\definition\reference\ReferenceInterface; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Class DefinitionBuilder |
|
23
|
|
|
* |
|
24
|
|
|
* @author Ruslan Molodyko <[email protected]> |
|
25
|
|
|
*/ |
|
26
|
|
|
class DefinitionBuilder extends AbstractDefinition |
|
27
|
|
|
{ |
|
28
|
|
|
/** @var ClassDefinition[] Definition collection */ |
|
29
|
|
|
protected $definitionCollection = []; |
|
30
|
|
|
/** @var ParameterBuilder */ |
|
31
|
|
|
protected $parameterBuilder; |
|
32
|
|
|
/** @var array Collection of interface => [classes]*/ |
|
33
|
|
|
protected $implementors = []; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* DefinitionBuilder constructor. |
|
37
|
|
|
* |
|
38
|
|
|
* @param ParameterBuilder $parameterBuilder |
|
39
|
|
|
* @param AbstractDefinition|null $parentDefinition |
|
40
|
|
|
*/ |
|
41
|
18 |
|
public function __construct(ParameterBuilder $parameterBuilder, AbstractDefinition $parentDefinition = null) |
|
42
|
|
|
{ |
|
43
|
18 |
|
parent::__construct($parentDefinition); |
|
44
|
|
|
|
|
45
|
18 |
|
$this->parameterBuilder = $parameterBuilder; |
|
46
|
18 |
|
$this->parameterBuilder->setParentDefinition($this); |
|
47
|
18 |
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Define parameters |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $name |
|
53
|
|
|
* @param ReferenceInterface $reference |
|
54
|
|
|
* @return ParameterBuilderInterface |
|
55
|
|
|
* @throws ParameterAlreadyExistsException |
|
56
|
|
|
*/ |
|
57
|
3 |
|
public function defineParameter(string $name, ReferenceInterface $reference): ParameterBuilderInterface |
|
58
|
|
|
{ |
|
59
|
3 |
|
return $this->parameterBuilder->defineParameter($name, $reference); |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Define interface implementors |
|
64
|
|
|
* |
|
65
|
|
|
* @param string $interfaceName |
|
66
|
|
|
* @param ReferenceInterface $class |
|
67
|
|
|
* @return DefinitionBuilder |
|
68
|
|
|
*/ |
|
69
|
|
|
public function defineImplementors(string $interfaceName, ReferenceInterface $class): DefinitionBuilder |
|
70
|
|
|
{ |
|
71
|
|
|
$this->implementors[$interfaceName] = $class; |
|
72
|
|
|
|
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Add new class definition |
|
78
|
|
|
* |
|
79
|
|
|
* @param $className |
|
80
|
|
|
* @param string $serviceName |
|
81
|
|
|
* @return ClassBuilderInterface |
|
82
|
|
|
* @throws \InvalidArgumentException |
|
83
|
|
|
* @throws ClassDefinitionAlreadyExistsException |
|
84
|
|
|
*/ |
|
85
|
16 |
|
public function addDefinition($className, string $serviceName = null): ClassBuilderInterface |
|
86
|
|
|
{ |
|
87
|
|
|
// Check if class already exists |
|
88
|
16 |
|
if ($this->hasDefinition($className)) { |
|
89
|
1 |
|
throw new ClassDefinitionAlreadyExistsException(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// Create new definition |
|
93
|
16 |
|
$classDefinition = new ClassDefinition($this); |
|
94
|
16 |
|
$classDefinition->setClassName(new ClassReference($className)); |
|
95
|
16 |
|
if ($serviceName) { |
|
|
|
|
|
|
96
|
2 |
|
$classDefinition->setServiceName($serviceName); |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
// Register definition |
|
100
|
16 |
|
$this->definitionCollection[$className] = $classDefinition; |
|
101
|
|
|
|
|
102
|
16 |
|
return $classDefinition; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* When definition for class name is exists in collection |
|
107
|
|
|
* |
|
108
|
|
|
* @param $className |
|
109
|
|
|
* @return bool |
|
110
|
|
|
*/ |
|
111
|
16 |
|
public function hasDefinition($className): bool |
|
112
|
|
|
{ |
|
113
|
16 |
|
return array_key_exists($className, $this->definitionCollection); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @return ClassDefinition[] |
|
118
|
|
|
*/ |
|
119
|
10 |
|
public function getDefinitionCollection(): array |
|
120
|
|
|
{ |
|
121
|
10 |
|
return $this->definitionCollection; |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @return ReferenceInterface[] |
|
126
|
|
|
*/ |
|
127
|
4 |
|
public function getParameterCollection(): array |
|
128
|
|
|
{ |
|
129
|
4 |
|
return $this->parameterBuilder->getParameterCollection(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
public function getImplementors(): array |
|
136
|
|
|
{ |
|
137
|
|
|
return $this->implementors; |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: