1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace TheCodingMachine\Yaco\Definition; |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* |
7
|
|
|
*/ |
8
|
|
|
class InlineEntry implements InlineEntryInterface |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var string |
12
|
|
|
*/ |
13
|
|
|
private $expression; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
private $statements; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string[] |
22
|
|
|
*/ |
23
|
|
|
private $usedVariables; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var bool |
27
|
|
|
*/ |
28
|
|
|
private $lazyEvaluation; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $expression |
32
|
|
|
* @param string $statements |
33
|
|
|
* @param string[] $usedVariables |
34
|
|
|
* @param bool $lazyEvaluation |
35
|
|
|
*/ |
36
|
|
|
public function __construct($expression, $statements, array $usedVariables, $lazyEvaluation = true) |
37
|
|
|
{ |
38
|
|
|
$this->expression = $expression; |
39
|
|
|
$this->statements = $statements; |
40
|
|
|
$this->usedVariables = $usedVariables; |
41
|
|
|
$this->lazyEvaluation = $lazyEvaluation; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Returns a list of PHP statements (ending with a ;) that are necessary to |
46
|
|
|
* build the entry. |
47
|
|
|
* For instance, these are valid PHP statements:. |
48
|
|
|
* |
49
|
|
|
* "$service = new MyService($container->get('my_dependency')); |
50
|
|
|
* $service->setStuff('foo');" |
51
|
|
|
* |
52
|
|
|
* Can be null or empty if no statements need to be returned. |
53
|
|
|
* |
54
|
|
|
* @return string|null |
55
|
|
|
*/ |
56
|
|
|
public function getStatements() |
57
|
|
|
{ |
58
|
|
|
return $this->statements; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Returns the PHP expression representing the entry. |
63
|
|
|
* This must be a string representing a valid PHP expression, |
64
|
|
|
* with no ending ;. |
65
|
|
|
* |
66
|
|
|
* For instance, "$service" is a valid PHP expression. |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
|
|
public function getExpression() |
71
|
|
|
{ |
72
|
|
|
return $this->expression; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the list of variables used in the process of creating this |
77
|
|
|
* entry definition. These variables should not be used by other |
78
|
|
|
* definitions in the same scope. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
|
|
public function getUsedVariables() |
83
|
|
|
{ |
84
|
|
|
return $this->usedVariables; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* If true, the entry will be evaluated when the `get` method is called (this is the default) |
89
|
|
|
* If false, the entry will be evaluated as soon as the container is constructed. This is useful |
90
|
|
|
* for entries that contain only parameters like strings, or constants. |
91
|
|
|
* |
92
|
|
|
* @return bool |
93
|
|
|
*/ |
94
|
|
|
public function isLazilyEvaluated() |
95
|
|
|
{ |
96
|
|
|
return $this->lazyEvaluation; |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|