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; |
9
|
|
|
|
10
|
|
|
use samsonframework\container\exception\ClassDefinitionAlreadyExistsException; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class DefinitionBuilder |
14
|
|
|
* |
15
|
|
|
* @author Ruslan Molodyko <[email protected]> |
16
|
|
|
*/ |
17
|
|
|
class DefinitionBuilder extends AbstractDefinition |
18
|
|
|
{ |
19
|
|
|
/** @var ClassDefinition[] Definition collection */ |
20
|
|
|
protected $definitionCollection = []; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Add new class definition |
24
|
|
|
* |
25
|
|
|
* @param $className |
26
|
|
|
* @param string $serviceName |
27
|
|
|
* @return ClassBuilderInterface |
28
|
|
|
* @throws ClassDefinitionAlreadyExistsException |
29
|
|
|
*/ |
30
|
5 |
|
public function addDefinition($className, string $serviceName = null): ClassBuilderInterface |
31
|
|
|
{ |
32
|
|
|
// Check if class already exists |
33
|
5 |
|
if (array_key_exists($className, $this->definitionCollection)) { |
34
|
|
|
throw new ClassDefinitionAlreadyExistsException(); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
// Create new definition |
38
|
5 |
|
$classDefinition = new ClassDefinition($this); |
39
|
5 |
|
$classDefinition->setClassName($className); |
40
|
5 |
|
if ($serviceName) { |
|
|
|
|
41
|
2 |
|
$classDefinition->setServiceName($serviceName); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
// Register definition |
45
|
5 |
|
$this->definitionCollection[$className] = $classDefinition; |
46
|
|
|
|
47
|
5 |
|
return $classDefinition; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Convert to metadata collection |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
public function toMetadataCollection(): array |
56
|
|
|
{ |
57
|
|
|
$metadataCollection = []; |
58
|
|
|
foreach ($this->definitionCollection as $classDefinition) { |
59
|
|
|
$metadataCollection[$classDefinition->getClassName()] = $classDefinition->toMetadata(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
return $metadataCollection; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
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: