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\ReferenceInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class DefinitionBuilder |
21
|
|
|
* |
22
|
|
|
* @author Ruslan Molodyko <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class DefinitionBuilder extends AbstractDefinition |
25
|
|
|
{ |
26
|
|
|
/** @var ClassDefinition[] Definition collection */ |
27
|
|
|
protected $definitionCollection = []; |
28
|
|
|
/** @var ParameterBuilder */ |
29
|
|
|
protected $parameterBuilder; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* DefinitionBuilder constructor. |
33
|
|
|
* |
34
|
|
|
* @param ParameterBuilder $parameterBuilder |
35
|
|
|
* @param AbstractDefinition|null $parentDefinition |
36
|
|
|
*/ |
37
|
18 |
|
public function __construct(ParameterBuilder $parameterBuilder, AbstractDefinition $parentDefinition = null) |
38
|
|
|
{ |
39
|
18 |
|
parent::__construct($parentDefinition); |
40
|
|
|
|
41
|
18 |
|
$this->parameterBuilder = $parameterBuilder; |
42
|
18 |
|
$this->parameterBuilder->setParentDefinition($this); |
43
|
18 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Define parameters |
47
|
|
|
* |
48
|
|
|
* @param string $name |
49
|
|
|
* @param ReferenceInterface $reference |
50
|
|
|
* @return ParameterBuilderInterface |
51
|
|
|
* @throws ParameterAlreadyExistsException |
52
|
|
|
*/ |
53
|
3 |
|
public function defineParameter(string $name, ReferenceInterface $reference): ParameterBuilderInterface |
54
|
|
|
{ |
55
|
3 |
|
return $this->parameterBuilder->defineParameter($name, $reference); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Add new class definition |
60
|
|
|
* |
61
|
|
|
* @param $className |
62
|
|
|
* @param string $serviceName |
63
|
|
|
* @return ClassBuilderInterface |
64
|
|
|
* @throws ClassDefinitionAlreadyExistsException |
65
|
|
|
*/ |
66
|
16 |
|
public function addDefinition($className, string $serviceName = null): ClassBuilderInterface |
67
|
|
|
{ |
68
|
|
|
// Check if class already exists |
69
|
16 |
|
if ($this->hasDefinition($className)) { |
70
|
1 |
|
throw new ClassDefinitionAlreadyExistsException(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
// Create new definition |
74
|
16 |
|
$classDefinition = new ClassDefinition($this); |
75
|
16 |
|
$classDefinition->setClassName($className); |
76
|
16 |
|
if ($serviceName) { |
|
|
|
|
77
|
2 |
|
$classDefinition->setServiceName($serviceName); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
// Register definition |
81
|
16 |
|
$this->definitionCollection[$className] = $classDefinition; |
82
|
|
|
|
83
|
16 |
|
return $classDefinition; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* When definition for class name is exists in collection |
88
|
|
|
* |
89
|
|
|
* @param $className |
90
|
|
|
* @return bool |
91
|
|
|
*/ |
92
|
16 |
|
public function hasDefinition($className): bool |
93
|
|
|
{ |
94
|
16 |
|
return array_key_exists($className, $this->definitionCollection); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @return ClassDefinition[] |
99
|
|
|
*/ |
100
|
10 |
|
public function getDefinitionCollection(): array |
101
|
|
|
{ |
102
|
10 |
|
return $this->definitionCollection; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* @return ReferenceInterface[] |
107
|
|
|
*/ |
108
|
4 |
|
public function getParameterCollection(): array |
109
|
|
|
{ |
110
|
4 |
|
return $this->parameterBuilder->getParameterCollection(); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: