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