1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco\Definition; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This class represents an instance declared using a call to a method (a factory) from an existing service. |
7
|
|
|
* The method can be passed any number of arguments. |
8
|
|
|
*/ |
9
|
|
|
class FactoryCallDefinition 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, or a fully qualified class name. |
20
|
|
|
* |
21
|
|
|
* @var ReferenceInterface |
22
|
|
|
*/ |
23
|
|
|
private $factory; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* The name of the method to be called. |
27
|
|
|
* |
28
|
|
|
* @var string |
29
|
|
|
*/ |
30
|
|
|
private $methodName; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* A list of arguments passed to the constructor. |
34
|
|
|
* |
35
|
|
|
* @var array Array of scalars or ReferenceInterface, or array mixing scalars, arrays, and ReferenceInterface |
36
|
|
|
*/ |
37
|
|
|
private $methodArguments = array(); |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructs an factory 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 ReferenceInterface $factory A pointer to the service that the factory method will be called upon, or a fully qualified class name |
44
|
|
|
* @param string $methodName The name of the factory method |
45
|
|
|
* @param array $methodArguments The parameters of the factory method |
46
|
|
|
*/ |
47
|
|
|
public function __construct($identifier, $factory, $methodName, array $methodArguments = []) |
48
|
|
|
{ |
49
|
|
|
$this->identifier = $identifier; |
50
|
|
|
$this->factory = $factory; |
51
|
|
|
$this->methodName = $methodName; |
52
|
|
|
$this->methodArguments = $methodArguments; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the identifier of the instance. |
57
|
|
|
* |
58
|
|
|
* @return string |
59
|
|
|
*/ |
60
|
|
|
public function getIdentifier() |
61
|
|
|
{ |
62
|
|
|
return $this->identifier; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Returns a pointer to the service that the factory method will be called upon, or a fully qualified class name. |
67
|
|
|
* |
68
|
|
|
* @return ReferenceInterface|string |
69
|
|
|
*/ |
70
|
|
|
public function getFactory() |
71
|
|
|
{ |
72
|
|
|
return $this->factory; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the name of the factory method. |
77
|
|
|
* |
78
|
|
|
* @return string |
79
|
|
|
*/ |
80
|
|
|
public function getMethodName() |
81
|
|
|
{ |
82
|
|
|
return $this->methodName; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Returns the parameters of the factory method. |
87
|
|
|
* |
88
|
|
|
* @return array |
89
|
|
|
*/ |
90
|
|
|
public function getMethodArguments() |
91
|
|
|
{ |
92
|
|
|
return $this->methodArguments; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Adds an argument to the method. |
97
|
|
|
* |
98
|
|
|
* @param mixed $argument |
99
|
|
|
*/ |
100
|
|
|
public function addMethodArgument($argument) |
101
|
|
|
{ |
102
|
|
|
$this->methodArguments[] = $argument; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Returns an InlineEntryInterface object representing the PHP code necessary to generate |
107
|
|
|
* the container entry. |
108
|
|
|
* |
109
|
|
|
* @param string $containerVariable The name of the variable that allows access to the container instance. For instance: "$container", or "$this->container" |
110
|
|
|
* @param array $usedVariables An array of variables that are already used and that should not be used when generating this code. |
111
|
|
|
* |
112
|
|
|
* @return InlineEntryInterface |
113
|
|
|
*/ |
114
|
|
|
public function toPhpCode($containerVariable, array $usedVariables = array()) |
115
|
|
|
{ |
116
|
|
|
$dumpedArguments = ValueUtils::dumpArguments($this->methodArguments, $containerVariable, $usedVariables); |
117
|
|
|
$prependedCode = $dumpedArguments->getStatements(); |
118
|
|
|
if ($this->factory instanceof ReferenceInterface) { |
119
|
|
|
$code = sprintf("%s->get(%s)->%s(%s);\n", $containerVariable, var_export($this->factory->getTarget(), true), $this->methodName, $dumpedArguments->getExpression()); |
120
|
|
|
} else { |
121
|
|
|
$code = sprintf("%s::%s(%s);\n", $this->factory, $this->methodName, $dumpedArguments->getExpression()); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
return new InlineEntry($code, $prependedCode, $usedVariables); |
125
|
|
|
} |
126
|
|
|
} |
127
|
|
|
|