1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oro\Bundle\LayoutBundle\Layout\Processor; |
4
|
|
|
|
5
|
|
|
use Oro\Component\ConfigExpression\AssemblerInterface; |
6
|
|
|
use Oro\Component\ConfigExpression\ExpressionInterface; |
7
|
|
|
use Oro\Component\Layout\ContextInterface; |
8
|
|
|
use Oro\Component\Layout\DataAccessorInterface; |
9
|
|
|
use Oro\Component\Layout\OptionValueBag; |
10
|
|
|
|
11
|
|
|
use Oro\Bundle\LayoutBundle\Layout\Encoder\ConfigExpressionEncoderRegistry; |
12
|
|
|
|
13
|
|
|
class ConfigExpressionProcessor |
14
|
|
|
{ |
15
|
|
|
const ARRAY_IS_REGULAR = 0; |
16
|
|
|
const ARRAY_IS_EXPRESSION = 1; |
17
|
|
|
const ARRAY_IS_EXPRESSION_STARTED_WITH_BACKSLASH = -1; |
18
|
|
|
|
19
|
|
|
/** @var AssemblerInterface */ |
20
|
|
|
protected $expressionAssembler; |
21
|
|
|
|
22
|
|
|
/** @var ConfigExpressionEncoderRegistry */ |
23
|
|
|
protected $encoderRegistry; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param AssemblerInterface $expressionAssembler |
27
|
|
|
* @param ConfigExpressionEncoderRegistry $encoderRegistry |
28
|
|
|
*/ |
29
|
|
|
public function __construct( |
30
|
|
|
AssemblerInterface $expressionAssembler, |
31
|
|
|
ConfigExpressionEncoderRegistry $encoderRegistry |
32
|
|
|
) { |
33
|
|
|
$this->expressionAssembler = $expressionAssembler; |
34
|
|
|
$this->encoderRegistry = $encoderRegistry; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param array $values |
39
|
|
|
* @param ContextInterface $context |
40
|
|
|
* @param DataAccessorInterface $data |
41
|
|
|
* @param bool $evaluate |
42
|
|
|
* @param string $encoding |
43
|
|
|
*/ |
44
|
|
|
public function processExpressions( |
45
|
|
|
array &$values, |
46
|
|
|
ContextInterface $context, |
47
|
|
|
DataAccessorInterface $data, |
48
|
|
|
$evaluate, |
49
|
|
|
$encoding |
50
|
|
|
) { |
51
|
|
|
if (!$evaluate && $encoding === null) { |
52
|
|
|
return; |
53
|
|
|
} |
54
|
|
|
foreach ($values as $key => &$value) { |
55
|
|
|
if (is_array($value)) { |
56
|
|
|
if (!empty($value)) { |
57
|
|
|
switch ($this->checkArrayValue($value)) { |
58
|
|
|
case self::ARRAY_IS_REGULAR: |
59
|
|
|
$this->processExpressions($value, $context, $data, $evaluate, $encoding); |
60
|
|
|
break; |
61
|
|
|
case self::ARRAY_IS_EXPRESSION: |
62
|
|
|
$value = $this->processExpression( |
63
|
|
|
$this->expressionAssembler->assemble($value), |
|
|
|
|
64
|
|
|
$context, |
65
|
|
|
$data, |
66
|
|
|
$evaluate, |
67
|
|
|
$encoding |
68
|
|
|
); |
69
|
|
|
break; |
70
|
|
|
case self::ARRAY_IS_EXPRESSION_STARTED_WITH_BACKSLASH: |
71
|
|
|
// the backslash (\) at the begin of the array key should be removed |
72
|
|
|
$value = [substr(key($value), 1) => reset($value)]; |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
} elseif ($value instanceof OptionValueBag) { |
77
|
|
|
foreach ($value->all() as $action) { |
78
|
|
|
$args = $action->getArguments(); |
79
|
|
|
$this->processExpressions($args, $context, $data, $evaluate, $encoding); |
80
|
|
|
foreach ($args as $index => $arg) { |
81
|
|
|
$action->setArgument($index, $arg); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} elseif ($value instanceof ExpressionInterface) { |
85
|
|
|
$value = $this->processExpression($value, $context, $data, $evaluate, $encoding); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param ExpressionInterface $expr |
92
|
|
|
* @param ContextInterface $context |
93
|
|
|
* @param DataAccessorInterface $data |
94
|
|
|
* @param bool $evaluate |
95
|
|
|
* @param string $encoding |
96
|
|
|
* |
97
|
|
|
* @return mixed|string |
98
|
|
|
*/ |
99
|
|
|
protected function processExpression( |
100
|
|
|
ExpressionInterface $expr, |
101
|
|
|
ContextInterface $context, |
102
|
|
|
DataAccessorInterface $data, |
103
|
|
|
$evaluate, |
104
|
|
|
$encoding |
105
|
|
|
) { |
106
|
|
|
return $evaluate |
107
|
|
|
? $expr->evaluate(['context' => $context, 'data' => $data]) |
108
|
|
|
: $this->encoderRegistry->getEncoder($encoding)->encodeExpr($expr); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $value |
113
|
|
|
* |
114
|
|
|
* @return int the checking result |
115
|
|
|
* 0 - the value is regular array |
116
|
|
|
* 1 - the value is an expression |
117
|
|
|
* -1 - the value is an array with one item and its key starts with "\@" |
118
|
|
|
* which should be replaces with "@" |
119
|
|
|
*/ |
120
|
|
|
protected function checkArrayValue($value) |
121
|
|
|
{ |
122
|
|
|
if (count($value) === 1) { |
123
|
|
|
reset($value); |
124
|
|
|
$k = key($value); |
125
|
|
|
if (is_string($k)) { |
126
|
|
|
$pos = strpos($k, '@'); |
127
|
|
|
if ($pos === 0) { |
128
|
|
|
// expression |
129
|
|
|
return self::ARRAY_IS_EXPRESSION; |
130
|
|
|
} elseif ($pos === 1 && $k[0] === '\\') { |
131
|
|
|
// the backslash (\) at the begin of the array key should be removed |
132
|
|
|
return self::ARRAY_IS_EXPRESSION_STARTED_WITH_BACKSLASH; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
// regular array |
138
|
|
|
return self::ARRAY_IS_REGULAR; |
139
|
|
|
} |
140
|
|
|
} |
141
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: