Skip to content

Commit a5e59dd

Browse files
committed
Optimized expression compiler names (#5)
1 parent 6d9ee8b commit a5e59dd

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

src/Generator/AbstractTypeGenerator.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public static function getWrappedType($name)
8484
{
8585
return isset(self::$wrappedTypes[$name]) ? self::$wrappedTypes[$name] : null;
8686
}
87-
87+
8888
protected function generateParentClassName(array $config)
8989
{
9090
return $this->shortenClassName(self::$typeSystems[$config['type']]);
@@ -109,18 +109,18 @@ protected function generateClassDocBlock(array $config)
109109
EOF;
110110
}
111111

112-
protected function varExportFromArrayValue(array $values, $key, $default = 'null')
112+
protected function varExportFromArrayValue(array $values, $key, $default = 'null', array $compilerNames = [])
113113
{
114114
if (!isset($values[$key])) {
115115
return $default;
116116
}
117117

118-
$code = $this->varExport($values[$key], $default);
118+
$code = $this->varExport($values[$key], $default, $compilerNames);
119119

120120
return $code;
121121
}
122122

123-
protected function varExport($var, $default = null)
123+
protected function varExport($var, $default = null, array $compilerNames = [])
124124
{
125125
switch (true) {
126126
case is_array($var):
@@ -133,7 +133,7 @@ protected function varExport($var, $default = null)
133133
return "[" . implode(", ", $r) . "]";
134134

135135
case $this->isExpression($var):
136-
return $code = $this->getExpressionLanguage()->compile($var);
136+
return $code = $this->getExpressionLanguage()->compile($var, $compilerNames);
137137

138138
case is_object($var):
139139
return $default;
@@ -155,7 +155,7 @@ protected function processFromArray(array $values, $templatePrefix)
155155
return '[' . $this->prefixCodeWithSpaces($code, 2) . "\n<spaces>]";
156156
}
157157

158-
protected function callableCallbackFromArrayValue(array $value, $key, $argDefinitions = null, $default = 'null')
158+
protected function callableCallbackFromArrayValue(array $value, $key, $argDefinitions = null, $default = 'null', array $compilerNames = null)
159159
{
160160
if (!isset($value[$key])) {
161161
return $default;
@@ -177,13 +177,14 @@ protected function callableCallbackFromArrayValue(array $value, $key, $argDefini
177177
return $code;
178178
}
179179
} elseif ($this->isExpression($value[$key])) {
180-
preg_match_all('@\$([a-z_][a-z0-9_]+)@i', $argDefinitions, $matches);
181-
182-
$argNames = isset($matches[1]) ? $matches[1] : [];
180+
if (null === $compilerNames) {
181+
preg_match_all('@\$([a-z_][a-z0-9_]+)@i', $argDefinitions, $matches);
182+
$compilerNames = isset($matches[1]) ? $matches[1] : [];
183+
}
183184
$code = sprintf(
184185
$code,
185186
$this->shortenClassFromCode($argDefinitions),
186-
$this->getExpressionLanguage()->compile($value[$key], $argNames)
187+
$this->getExpressionLanguage()->compile($value[$key], $compilerNames)
187188
);
188189

189190
return $code;

src/Generator/TypeGenerator.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,10 +60,10 @@ protected function generateDefaultValue(array $value)
6060

6161
protected function generateType(array $value)
6262
{
63+
$type = 'null';
64+
6365
if (isset($value['type'])) {
6466
$type = sprintf('function () <closureUseStatements>{ return %s; }', $this->typeAlias2String($value['type']));
65-
} else {
66-
$type = 'null';
6767
}
6868

6969
return $type;

0 commit comments

Comments
 (0)