1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco\ServiceProvider; |
4
|
|
|
|
5
|
|
|
use TheCodingMachine\Yaco\Definition\DumpableInterface; |
6
|
|
|
use TheCodingMachine\Yaco\Definition\InlineEntry; |
7
|
|
|
use TheCodingMachine\Yaco\Definition\InlineEntryInterface; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Fetches a service from the service-providers registry. |
11
|
|
|
*/ |
12
|
|
|
class ServiceFromRegistryDefinition implements DumpableInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* The identifier of the instance in the container. |
16
|
|
|
* |
17
|
|
|
* @var string|null |
18
|
|
|
*/ |
19
|
|
|
private $identifier; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* The key of the service provider in the registry. |
23
|
|
|
* |
24
|
|
|
* @var int |
25
|
|
|
*/ |
26
|
|
|
private $serviceProviderKey; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var CallbackWrapperDefinition |
30
|
|
|
*/ |
31
|
|
|
private $callbackWrapperDefinition; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
private $serviceName; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string|null $identifier |
40
|
|
|
* @param string $serviceName |
41
|
|
|
* @param int $serviceProviderKey |
42
|
|
|
* @param CallbackWrapperDefinition|null $callbackWrapperDefinition |
43
|
|
|
*/ |
44
|
|
|
public function __construct($identifier, $serviceName, $serviceProviderKey, CallbackWrapperDefinition $callbackWrapperDefinition = null) |
45
|
|
|
{ |
46
|
|
|
$this->identifier = $identifier; |
47
|
|
|
$this->serviceName = $serviceName; |
48
|
|
|
$this->serviceProviderKey = $serviceProviderKey; |
49
|
|
|
$this->callbackWrapperDefinition = $callbackWrapperDefinition; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns the identifier for this object in the container. |
54
|
|
|
* If null, classes consuming this definition should assume the definition must be inlined. |
55
|
|
|
* |
56
|
|
|
* @return string|null |
57
|
|
|
*/ |
58
|
|
|
public function getIdentifier() |
59
|
|
|
{ |
60
|
|
|
return $this->identifier; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @return int |
65
|
|
|
*/ |
66
|
|
|
public function getServiceProviderKey() |
67
|
|
|
{ |
68
|
|
|
return $this->serviceProviderKey; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return CallbackWrapperDefinition |
73
|
|
|
*/ |
74
|
|
|
public function getCallbackWrapperDefinition() |
75
|
|
|
{ |
76
|
|
|
return $this->callbackWrapperDefinition; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getServiceName() |
83
|
|
|
{ |
84
|
|
|
return $this->serviceName; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Returns an InlineEntryInterface object representing the PHP code necessary to generate |
89
|
|
|
* the container entry. |
90
|
|
|
* |
91
|
|
|
* @param string $containerVariable The name of the variable that allows access to the container instance. For instance: "$container", or "$this->container" |
92
|
|
|
* @param array $usedVariables An array of variables that are already used and that should not be used when generating this code. |
93
|
|
|
* |
94
|
|
|
* @return InlineEntryInterface |
95
|
|
|
*/ |
96
|
|
|
public function toPhpCode($containerVariable, array $usedVariables = array()) |
97
|
|
|
{ |
98
|
|
|
$previousCode = ''; |
99
|
|
|
if ($this->callbackWrapperDefinition) { |
100
|
|
|
$previousCode = ', '.$this->callbackWrapperDefinition->toPhpCode($containerVariable, $usedVariables)->getExpression(); |
101
|
|
|
} |
102
|
|
|
$code = sprintf('$this->registry->createService(%s, %s, $this->delegateLookupContainer%s)', var_export($this->serviceProviderKey, true), |
103
|
|
|
var_export($this->serviceName, true), $previousCode); |
104
|
|
|
|
105
|
|
|
return new InlineEntry($code, null, $usedVariables); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|