Skip to content

Commit

Permalink
Optimized expression compiler names (#5)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcg-web committed May 9, 2016
1 parent 6d9ee8b commit a5e59dd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
21 changes: 11 additions & 10 deletions src/Generator/AbstractTypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ public static function getWrappedType($name)
{
return isset(self::$wrappedTypes[$name]) ? self::$wrappedTypes[$name] : null;
}

protected function generateParentClassName(array $config)
{
return $this->shortenClassName(self::$typeSystems[$config['type']]);
Expand All @@ -109,18 +109,18 @@ protected function generateClassDocBlock(array $config)
EOF;
}

protected function varExportFromArrayValue(array $values, $key, $default = 'null')
protected function varExportFromArrayValue(array $values, $key, $default = 'null', array $compilerNames = [])
{
if (!isset($values[$key])) {
return $default;
}

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

return $code;
}

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

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

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

protected function callableCallbackFromArrayValue(array $value, $key, $argDefinitions = null, $default = 'null')
protected function callableCallbackFromArrayValue(array $value, $key, $argDefinitions = null, $default = 'null', array $compilerNames = null)
{
if (!isset($value[$key])) {
return $default;
Expand All @@ -177,13 +177,14 @@ protected function callableCallbackFromArrayValue(array $value, $key, $argDefini
return $code;
}
} elseif ($this->isExpression($value[$key])) {
preg_match_all('@\$([a-z_][a-z0-9_]+)@i', $argDefinitions, $matches);

$argNames = isset($matches[1]) ? $matches[1] : [];
if (null === $compilerNames) {
preg_match_all('@\$([a-z_][a-z0-9_]+)@i', $argDefinitions, $matches);
$compilerNames = isset($matches[1]) ? $matches[1] : [];
}
$code = sprintf(
$code,
$this->shortenClassFromCode($argDefinitions),
$this->getExpressionLanguage()->compile($value[$key], $argNames)
$this->getExpressionLanguage()->compile($value[$key], $compilerNames)
);

return $code;
Expand Down
4 changes: 2 additions & 2 deletions src/Generator/TypeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,10 @@ protected function generateDefaultValue(array $value)

protected function generateType(array $value)
{
$type = 'null';

if (isset($value['type'])) {
$type = sprintf('function () <closureUseStatements>{ return %s; }', $this->typeAlias2String($value['type']));
} else {
$type = 'null';
}

return $type;
Expand Down

0 comments on commit a5e59dd

Please sign in to comment.