Skip to content

Commit

Permalink
Use options system to allow future other options
Browse files Browse the repository at this point in the history
  • Loading branch information
kylekatarnls committed Apr 5, 2020
1 parent 3a25196 commit 6d3137e
Showing 1 changed file with 41 additions and 25 deletions.
66 changes: 41 additions & 25 deletions src/JsPhpize/Compiler/Compiler.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

class Compiler
{
const DOT_DISABLED = 1;

use DyiadeTrait;
use InterpolationTrait;

Expand Down Expand Up @@ -205,12 +207,12 @@ protected function visitDyiade(Dyiade $dyiade, $indent)
return $leftHand . ' ' . $dyiade->operator . ' ' . $rightHand;
}

protected function mapNodesArray($array, $indent, $pattern = null, $dotDisabled = false)
protected function mapNodesArray($array, $indent, $pattern = null, $options = 0)
{
$visitNode = [$this, 'visitNode'];

return array_map(function ($value) use ($visitNode, $indent, $pattern, $dotDisabled) {
$value = $visitNode($value, $indent, $dotDisabled);
return array_map(function ($value) use ($visitNode, $indent, $pattern, $options) {
$value = $visitNode($value, $indent, $options);

if ($pattern) {
$value = sprintf($pattern, $value);
Expand All @@ -220,17 +222,23 @@ protected function mapNodesArray($array, $indent, $pattern = null, $dotDisabled
}, $array);
}

protected function visitNodesArray($array, $indent, $glue = '', $pattern = null, $dotDisabled = false)
protected function visitNodesArray($array, $indent, $glue = '', $pattern = null, $options = 0)
{
return implode($glue, $this->mapNodesArray($array, $indent, $pattern, $dotDisabled));
return implode($glue, $this->mapNodesArray($array, $indent, $pattern, $options));
}

protected function visitFunctionCall(FunctionCall $functionCall, $indent)
{
$function = $functionCall->function;
$arguments = $functionCall->arguments;
$applicant = $functionCall->applicant;
$arguments = $this->visitNodesArray($arguments, $indent, ', ', null, $function instanceof Variable && $function->name === 'isset');
$arguments = $this->visitNodesArray(
$arguments,
$indent,
', ',
null,
$function instanceof Variable && $function->name === 'isset' ? static::DOT_DISABLED : 0
);
$dynamicCall = $this->visitNode($function, $indent) . '(' . $arguments . ')';

if ($function instanceof Variable && count($function->children) === 0) {
Expand Down Expand Up @@ -286,14 +294,14 @@ protected function visitInstruction(Instruction $group, $indent)
}, $group->instructions));
}

public function visitNode(Node $node, $indent, $dotDisabled = false)
public function visitNode(Node $node, $indent, $options = 0)
{
$method = preg_replace(
'/^(.+\\\\)?([^\\\\]+)$/',
'visit$2',
get_class($node)
);
$php = method_exists($this, $method) ? $this->$method($node, $indent, $dotDisabled) : '';
$php = method_exists($this, $method) ? $this->$method($node, $indent, $options) : '';

if ($node instanceof Value) {
$php = $node->getBefore() . $php . $node->getAfter();
Expand All @@ -314,33 +322,41 @@ protected function visitTernary(Ternary $ternary, $indent)
' : ' . $this->visitNode($ternary->falseValue, $indent);
}

protected function handleVariableChildren(DynamicValue $dynamicValue, $indent, $php, $dotDisabled = false)
protected function handleVariableChildren(DynamicValue $dynamicValue, $indent, $php, $options = 0)
{
$children = $dynamicValue->children;

if (count($children)) {
$arguments = $this->mapNodesArray($children, $indent, null, $dotDisabled);
array_unshift($arguments, $php);
$dot = $this->engine->getHelperName('dot');

if ($dotDisabled) {
$lastChild = end($children);
$dotChild = $lastChild instanceof Constant && $lastChild->dotChild;
$lastChild = array_pop($arguments);
}
return $this->wrapVariableChildren($children, $indent, $php, $options);
}

$php = $this->helperWrap($dot, $arguments);
return $php;
}

if ($dotDisabled) {
$pattern = $dotChild ? '%s->{%s}' : '%s[%s]';
$php = sprintf($pattern, $php, $lastChild);
}
protected function wrapVariableChildren($children, $indent, $php, $options)
{
$arguments = $this->mapNodesArray($children, $indent);
array_unshift($arguments, $php);
$dot = $this->engine->getHelperName('dot');
$dotDisabled = $options & static::DOT_DISABLED;

if ($dotDisabled) {
$lastChild = end($children);
$dotChild = $lastChild instanceof Constant && $lastChild->dotChild;
$lastChild = array_pop($arguments);
}

$php = $this->helperWrap($dot, $arguments);

if ($dotDisabled) {
$pattern = $dotChild ? '%s->{%s}' : '%s[%s]';
$php = sprintf($pattern, $php, $lastChild);
}

return $php;
}

protected function visitVariable(Variable $variable, $indent, $dotDisabled = false)
protected function visitVariable(Variable $variable, $indent, $options = 0)
{
$name = $variable->name;
if (in_array($name, ['Math', 'RegExp'])) {
Expand All @@ -353,7 +369,7 @@ protected function visitVariable(Variable $variable, $indent, $dotDisabled = fal
$name = '$' . $name;
}

return $this->handleVariableChildren($variable, $indent, $name, $dotDisabled);
return $this->handleVariableChildren($variable, $indent, $name, $options);
}

public function compile(Block $block, $indent = '')
Expand Down

0 comments on commit 6d3137e

Please sign in to comment.