1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco\Definition; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class represents an instance declared using the "new" keyword followed by an optional list of |
7
|
|
|
* method calls and properties assignations. |
8
|
|
|
*/ |
9
|
|
|
class ObjectDefinition implements DumpableInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* The identifier of the instance in the container. |
13
|
|
|
* |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
private $identifier; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* The fully qualified class name of this instance. |
20
|
|
|
* |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
private $className; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* A list of arguments passed to the constructor. |
27
|
|
|
* |
28
|
|
|
* @var array Array of scalars or ReferenceInterface, or array mixing scalars, arrays, and ReferenceInterface |
29
|
|
|
*/ |
30
|
|
|
private $constructorArguments = array(); |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A list of actions to be executed (can be either a method call or a public property assignation). |
34
|
|
|
* |
35
|
|
|
* @var ActionInterface[] |
36
|
|
|
*/ |
37
|
|
|
private $actions = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructs an instance definition. |
41
|
|
|
* |
42
|
|
|
* @param string|null $identifier The identifier of the instance in the container. Can be null if the instance is anonymous (declared inline of other instances) |
43
|
|
|
* @param string $className The fully qualified class name of this instance. |
44
|
|
|
* @param array $constructorArguments A list of constructor arguments. |
45
|
|
|
*/ |
46
|
|
|
public function __construct($identifier, $className, array $constructorArguments = array()) |
47
|
|
|
{ |
48
|
|
|
$this->identifier = $identifier; |
49
|
|
|
$this->className = '\\'.ltrim($className, '\\'); |
50
|
|
|
$this->constructorArguments = $constructorArguments; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Returns the identifier of the instance. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
|
|
public function getIdentifier() |
59
|
|
|
{ |
60
|
|
|
return $this->identifier; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getClassName() |
67
|
|
|
{ |
68
|
|
|
return $this->className; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
|
|
public function getConstructorParameters() |
75
|
|
|
{ |
76
|
|
|
return $this->constructorArguments; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Adds an argument to the list of arguments to be passed to the constructor. |
81
|
|
|
* |
82
|
|
|
* @param mixed $argument |
83
|
|
|
* |
84
|
|
|
* @return self |
85
|
|
|
*/ |
86
|
|
|
public function addConstructorArgument($argument) |
87
|
|
|
{ |
88
|
|
|
$this->constructorArguments[] = $argument; |
89
|
|
|
|
90
|
|
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Adds a method call. |
95
|
|
|
* |
96
|
|
|
* @param string $methodName |
97
|
|
|
* @param array $arguments |
98
|
|
|
* |
99
|
|
|
* @return MethodCall |
100
|
|
|
*/ |
101
|
|
|
public function addMethodCall($methodName, array $arguments = array()) |
102
|
|
|
{ |
103
|
|
|
$this->actions[] = $methodCall = new MethodCall($methodName, $arguments); |
104
|
|
|
|
105
|
|
|
return $methodCall; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Adds a method call. |
110
|
|
|
* |
111
|
|
|
* @param string $propertyName |
112
|
|
|
* @param mixed $value |
113
|
|
|
* |
114
|
|
|
* @return self |
115
|
|
|
*/ |
116
|
|
|
public function setProperty($propertyName, $value) |
117
|
|
|
{ |
118
|
|
|
$this->actions[] = new PropertyAssignment($propertyName, $value); |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Returns an InlineEntryInterface object representing the PHP code necessary to generate |
125
|
|
|
* the container entry. |
126
|
|
|
* |
127
|
|
|
* @param string $containerVariable The name of the variable that allows access to the container instance. For instance: "$container", or "$this->container" |
128
|
|
|
* @param array $usedVariables An array of variables that are already used and that should not be used when generating this code. |
129
|
|
|
* |
130
|
|
|
* @return InlineEntryInterface |
131
|
|
|
*/ |
132
|
|
|
public function toPhpCode($containerVariable, array $usedVariables = array()) |
133
|
|
|
{ |
134
|
|
|
if ($this->identifier !== null) { |
135
|
|
|
$variableName = $this->getIdentifier(); |
136
|
|
|
} else { |
137
|
|
|
$variableName = $this->className; |
138
|
|
|
} |
139
|
|
|
$variableName = VariableUtils::getNextAvailableVariableName(lcfirst($variableName), $usedVariables); |
140
|
|
|
|
141
|
|
|
$usedVariables[] = $variableName; |
142
|
|
|
$dumpedArguments = ValueUtils::dumpArguments($this->constructorArguments, $containerVariable, $usedVariables); |
143
|
|
|
$prependedCode = $dumpedArguments->getStatements(); |
144
|
|
|
$code = sprintf("%s = new %s(%s);\n", $variableName, $this->className, $dumpedArguments->getExpression()); |
145
|
|
|
foreach ($this->actions as $action) { |
146
|
|
|
$inlineCode = $action->toPhpCode($variableName, $containerVariable, $usedVariables); |
147
|
|
|
$code .= $inlineCode->getStatements()."\n"; |
148
|
|
|
$usedVariables = $inlineCode->getUsedVariables(); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
return new InlineEntry($variableName, $prependedCode.$code, $usedVariables); |
152
|
|
|
} |
153
|
|
|
} |
154
|
|
|
|