1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco; |
4
|
|
|
|
5
|
|
|
use Interop\Container\Definition\AliasDefinitionInterface; |
6
|
|
|
use Interop\Container\Definition\DefinitionInterface; |
7
|
|
|
use Interop\Container\Definition\FactoryCallDefinitionInterface; |
8
|
|
|
use Interop\Container\Definition\ObjectDefinitionInterface; |
9
|
|
|
use Interop\Container\Definition\ParameterDefinitionInterface; |
10
|
|
|
use Interop\Container\Definition\ReferenceInterface; |
11
|
|
|
use TheCodingMachine\Yaco\Definition\AliasDefinition; |
12
|
|
|
use TheCodingMachine\Yaco\Definition\DumpableInterface; |
13
|
|
|
use TheCodingMachine\Yaco\Definition\FactoryCallDefinition; |
14
|
|
|
use TheCodingMachine\Yaco\Definition\ObjectDefinition; |
15
|
|
|
use TheCodingMachine\Yaco\Definition\ParameterDefinition; |
16
|
|
|
use TheCodingMachine\Yaco\Definition\Reference; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* This class is in charge of converting definitions from the Interop\Container\DefinitionInterface to the |
20
|
|
|
* internal DumpableInterface format. |
21
|
|
|
*/ |
22
|
|
|
class DefinitionConverter implements DefinitionConverterInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* @param DefinitionInterface $definition |
26
|
|
|
* |
27
|
|
|
* @return DumpableInterface |
28
|
|
|
*/ |
29
|
|
|
public function convert(DefinitionInterface $definition) |
30
|
|
|
{ |
31
|
|
|
if ($definition instanceof ObjectDefinitionInterface) { |
32
|
|
|
$yacoObjectDefinition = new ObjectDefinition($definition->getIdentifier(), |
33
|
|
|
$definition->getClassName(), |
34
|
|
|
$this->convertArguments($definition->getConstructorArguments())); |
35
|
|
|
|
36
|
|
|
foreach ($definition->getPropertyAssignments() as $assignment) { |
37
|
|
|
$yacoObjectDefinition->setProperty($assignment->getPropertyName(), $this->convertValue($assignment->getValue())); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
foreach ($definition->getMethodCalls() as $methodCall) { |
41
|
|
|
$yacoObjectDefinition->addMethodCall($methodCall->getMethodName(), $this->convertArguments($methodCall->getArguments())); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
return $yacoObjectDefinition; |
45
|
|
|
} elseif ($definition instanceof FactoryCallDefinitionInterface) { |
46
|
|
|
return new FactoryCallDefinition($definition->getIdentifier(), |
47
|
|
|
$this->convertValue($definition->getFactory()), |
48
|
|
|
$definition->getMethodName(), |
49
|
|
|
$this->convertArguments($definition->getArguments())); |
50
|
|
|
} elseif ($definition instanceof ParameterDefinitionInterface) { |
51
|
|
|
return new ParameterDefinition($definition->getIdentifier(), $definition->getValue()); |
52
|
|
|
} elseif ($definition instanceof AliasDefinitionInterface) { |
53
|
|
|
return new AliasDefinition($definition->getIdentifier(), $definition->getTarget()); |
54
|
|
|
} else { |
55
|
|
|
throw new \RuntimeException(sprintf('Cannot convert object of type "%s"', get_class($definition))); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @param array $arguments |
61
|
|
|
* |
62
|
|
|
* @return array |
63
|
|
|
*/ |
64
|
|
|
private function convertArguments(array $arguments) |
65
|
|
|
{ |
66
|
|
|
$yacoArguments = []; |
67
|
|
|
foreach ($arguments as $argument) { |
68
|
|
|
$yacoArguments[] = $this->convertValue($argument); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $yacoArguments; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
private function convertValue($value) |
75
|
|
|
{ |
76
|
|
|
if (is_array($value)) { |
77
|
|
|
return $this->convertArray($value); |
78
|
|
|
} elseif ($value instanceof DefinitionInterface) { |
79
|
|
|
return $this->convert($value); |
80
|
|
|
} elseif ($value instanceof ReferenceInterface) { |
81
|
|
|
return $this->convertReference($value); |
82
|
|
|
} elseif (is_object($value) || is_resource($value)) { |
83
|
|
|
throw new \RuntimeException('Unable to convert a definition. Parameters cannot be an object or a resource.'); |
84
|
|
|
} else { |
85
|
|
|
return $value; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
private function convertArray(array $values) |
90
|
|
|
{ |
91
|
|
|
$result = []; |
92
|
|
|
|
93
|
|
|
foreach ($values as $k => $v) { |
94
|
|
|
$result[$k] = $this->convertValue($v); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return $result; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
private function convertReference(ReferenceInterface $reference) |
101
|
|
|
{ |
102
|
|
|
return new Reference($reference->getTarget()); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
|