|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* See class comment |
|
4
|
|
|
* |
|
5
|
|
|
* PHP Version 5 |
|
6
|
|
|
* |
|
7
|
|
|
* @category Netresearch |
|
8
|
|
|
* @package Netresearch\Kite |
|
9
|
|
|
* @subpackage ExpressionLanguage |
|
10
|
|
|
* @author Christian Opitz <[email protected]> |
|
11
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
12
|
|
|
* @link http://www.netresearch.de |
|
13
|
|
|
*/ |
|
14
|
|
|
|
|
15
|
|
|
namespace Netresearch\Kite\ExpressionLanguage; |
|
16
|
|
|
use Netresearch\Kite\Task; |
|
17
|
|
|
|
|
18
|
|
|
use Symfony\Component\Console\Question\ChoiceQuestion; |
|
19
|
|
|
use Symfony\Component\Console\Question\ConfirmationQuestion; |
|
20
|
|
|
use Symfony\Component\Console\Question\Question; |
|
21
|
|
|
use Symfony\Component\ExpressionLanguage\ParserCache\ParserCacheInterface; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* Extension of Symfonies expression language to inject our custom |
|
25
|
|
|
* lexer and register some functions |
|
26
|
|
|
* |
|
27
|
|
|
* @category Netresearch |
|
28
|
|
|
* @package Netresearch\Kite |
|
29
|
|
|
* @subpackage ExpressionLanguage |
|
30
|
|
|
* @author Christian Opitz <[email protected]> |
|
31
|
|
|
* @license http://www.netresearch.de Netresearch Copyright |
|
32
|
|
|
* @link http://www.netresearch.de |
|
33
|
|
|
*/ |
|
34
|
|
|
class ExpressionLanguage extends \Symfony\Component\ExpressionLanguage\ExpressionLanguage |
|
35
|
|
|
{ |
|
36
|
|
|
/** |
|
37
|
|
|
* @var array |
|
38
|
|
|
*/ |
|
39
|
|
|
protected $expressionResults = []; |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var string |
|
43
|
|
|
*/ |
|
44
|
|
|
const VARIABLES_KEY = 'variables'; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* ExpressionLanguage constructor. |
|
48
|
|
|
* |
|
49
|
|
|
* @param ParserCacheInterface|null $cache The cache |
|
50
|
|
|
* @param array $providers Providers |
|
51
|
|
|
*/ |
|
52
|
|
|
public function __construct(ParserCacheInterface $cache = null, array $providers = array()) |
|
53
|
|
|
{ |
|
54
|
|
|
parent::__construct($cache, $providers); |
|
55
|
|
|
|
|
56
|
|
|
$reflectionProperty = new \ReflectionProperty('\Symfony\Component\ExpressionLanguage\ExpressionLanguage', 'lexer'); |
|
57
|
|
|
$reflectionProperty->setAccessible(true); |
|
58
|
|
|
$reflectionProperty->setValue($this, new Lexer()); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* Reevaluate parents evaluation results as expressions could be nested |
|
63
|
|
|
* |
|
64
|
|
|
* Don't parse expression strings which were the final result of an evaluation |
|
65
|
|
|
* ( As for example, given a variable "char" that is "\{", |
|
66
|
|
|
* the result of "{char}" would be "{". Reevaluating this is 1. not intended |
|
67
|
|
|
* and would 2. lead to an error ) |
|
68
|
|
|
* |
|
69
|
|
|
* @param string|\Symfony\Component\ExpressionLanguage\Expression $expression The expression |
|
70
|
|
|
* @param array $values The values |
|
71
|
|
|
* |
|
72
|
|
|
* @return string|mixed |
|
73
|
|
|
*/ |
|
74
|
|
|
public function evaluate($expression, $values = []) |
|
75
|
|
|
{ |
|
76
|
|
|
if (is_string($expression) |
|
77
|
|
|
&& !in_array($expression, $this->expressionResults, true) |
|
78
|
|
|
&& preg_match($couldBeExpressionPattern = '/(^|[^\\\\])\{/', $expression) |
|
79
|
|
|
) { |
|
80
|
|
|
do { |
|
81
|
|
|
$expression = parent::evaluate($expression, $values); |
|
82
|
|
|
if (!is_string($expression)) { |
|
83
|
|
|
break; |
|
84
|
|
|
} |
|
85
|
|
|
if (!preg_match($couldBeExpressionPattern, $expression)) { |
|
86
|
|
|
$expression = str_replace(['\\{', '\\}'], ['{', '}'], $expression); |
|
87
|
|
|
if (strpos($expression, '{') !== false) { |
|
88
|
|
|
$this->expressionResults[] = $expression; |
|
89
|
|
|
} |
|
90
|
|
|
break; |
|
91
|
|
|
} |
|
92
|
|
|
} while (true); |
|
93
|
|
|
} |
|
94
|
|
|
return $expression; |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* Ask a question |
|
100
|
|
|
* |
|
101
|
|
|
* @param Task $task The task on which the question was asked |
|
102
|
|
|
* @param Question $question The question |
|
103
|
|
|
* |
|
104
|
|
|
* @return mixed The answer |
|
105
|
|
|
*/ |
|
106
|
|
|
protected function ask(Task $task, $question) |
|
107
|
|
|
{ |
|
108
|
|
|
$console = $task->console; |
|
109
|
|
|
return $console->getHelper('question')->ask( |
|
|
|
|
|
|
110
|
|
|
$console->getInput(), |
|
111
|
|
|
$console->getOutput(), |
|
112
|
|
|
$question |
|
113
|
|
|
); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Register functions |
|
118
|
|
|
* |
|
119
|
|
|
* @return void |
|
120
|
|
|
*/ |
|
121
|
|
|
protected function registerFunctions() |
|
122
|
|
|
{ |
|
123
|
|
|
parent::registerFunctions(); |
|
124
|
|
|
$this->register( |
|
125
|
|
|
'confirm', |
|
126
|
|
|
function ($question) { |
|
|
|
|
|
|
127
|
|
|
}, |
|
128
|
|
|
function (array $values, $question) { |
|
129
|
|
|
return $this->ask($values[self::VARIABLES_KEY], new ConfirmationQuestion("<question>$question</question> [y] ")); |
|
130
|
|
|
} |
|
131
|
|
|
); |
|
132
|
|
|
$this->register( |
|
133
|
|
|
'answer', |
|
134
|
|
|
function ($question) { |
|
|
|
|
|
|
135
|
|
|
}, |
|
136
|
|
|
function (array $values, $question) { |
|
137
|
|
|
return $this->ask($values[self::VARIABLES_KEY], new Question("<question>$question</question> ")); |
|
138
|
|
|
} |
|
139
|
|
|
); |
|
140
|
|
|
$this->register( |
|
141
|
|
|
'choose', |
|
142
|
|
|
function ($question) { |
|
|
|
|
|
|
143
|
|
|
}, |
|
144
|
|
|
function (array $values, $question, array $choices) { |
|
145
|
|
|
return $this->ask($values[self::VARIABLES_KEY], new ChoiceQuestion("<question>$question</question> ", $choices)); |
|
146
|
|
|
} |
|
147
|
|
|
); |
|
148
|
|
|
$this->register( |
|
149
|
|
|
'isset', |
|
150
|
|
|
function ($var) { |
|
|
|
|
|
|
151
|
|
|
}, |
|
152
|
|
|
function (array $values, $var) { |
|
153
|
|
|
return $values[self::VARIABLES_KEY]->has($var); |
|
154
|
|
|
} |
|
155
|
|
|
); |
|
156
|
|
|
$this->register( |
|
157
|
|
|
'empty', |
|
158
|
|
|
function ($var) { |
|
|
|
|
|
|
159
|
|
|
}, |
|
160
|
|
|
function (array $values, $var) { |
|
161
|
|
|
return $values[self::VARIABLES_KEY]->has($var) && $values[self::VARIABLES_KEY]->get($var); |
|
162
|
|
|
} |
|
163
|
|
|
); |
|
164
|
|
|
$this->register( |
|
165
|
|
|
'get', |
|
166
|
|
|
function () { |
|
167
|
|
|
}, |
|
168
|
|
|
function (array $values, $var) { |
|
169
|
|
|
return $values[self::VARIABLES_KEY]->get($var); |
|
170
|
|
|
} |
|
171
|
|
|
); |
|
172
|
|
|
$this->register( |
|
173
|
|
|
'set', |
|
174
|
|
|
function () { |
|
175
|
|
|
}, |
|
176
|
|
|
function (array $values, $var, $value) { |
|
177
|
|
|
$values[self::VARIABLES_KEY]->set($var, $value); |
|
178
|
|
|
return $value; |
|
179
|
|
|
} |
|
180
|
|
|
); |
|
181
|
|
|
$this->register( |
|
182
|
|
|
'replace', |
|
183
|
|
|
function () { |
|
184
|
|
|
}, |
|
185
|
|
|
function (array $values, $search, $replace, $subject, $regex = false) { |
|
186
|
|
|
if ($regex) { |
|
187
|
|
|
return preg_replace($search, $replace, $subject); |
|
188
|
|
|
} else { |
|
189
|
|
|
return str_replace($search, $replace, $subject); |
|
190
|
|
|
} |
|
191
|
|
|
} |
|
192
|
|
|
); |
|
193
|
|
|
} |
|
194
|
|
|
} |
|
195
|
|
|
?> |
|
|
|
|
|
|
196
|
|
|
|
If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.
Let’s take a look at an example:
Our function
my_functionexpects aPostobject, and outputs the author of the post. The base classPostreturns a simple string and outputting a simple string will work just fine. However, the child classBlogPostwhich is a sub-type ofPostinstead decided to return anobject, and is therefore violating the SOLID principles. If aBlogPostwere passed tomy_function, PHP would not complain, but ultimately fail when executing thestrtouppercall in its body.