protected function escapeExpression(Expression $expr)
 {
     // check if we should _not_ escape this expression
     if ($expr instanceof Expressions\MethodCall && $expr->getMethodName() === 'raw') {
         return $expr->getArguments()[0];
     }
     // check if $expr is already wrapped in an escape call
     if ($expr instanceof Expressions\MethodCall && $expr->getMethodName() === 'escape') {
         return $expr;
     }
     // TODO: remove this hack
     if ($expr instanceof Expressions\MethodCall) {
         return $expr;
     }
     // binary expression either return a boolean or a numeric value, so they should never be escaped
     if ($expr instanceof Expressions\Binary) {
         return $expr;
     }
     // TODO: simple expressions can be escaped at compile time
     // wrap in an escape call
     return new Expressions\MethodCall('escape', [$expr]);
 }
Beispiel #2
0
 /**
  * @return string
  */
 public function toString()
 {
     return $this->operation . " " . $this->expression->toString();
 }
Beispiel #3
0
 /**
  * @return string
  */
 public function toString()
 {
     return $this->expression->toString() . "." . $this->attributeName;
 }
Beispiel #4
0
 protected function expression(Expression $expr)
 {
     if ($expr instanceof Expressions\Binary) {
         $this->codeEmitter->raw('(');
         $this->expression($expr->getLeft());
         $this->codeEmitter->raw(' ' . $expr->getOperation() . ' ');
         $this->expression($expr->getRight());
         $this->codeEmitter->raw(')');
     } else {
         if ($expr instanceof Expressions\Unary) {
             $showParens = !$this->canParensBeOmittedFor($expr->getExpression());
             $this->codeEmitter->raw($expr->getOperation());
             if ($showParens) {
                 $this->codeEmitter->raw('(');
             }
             $this->expression($expr->getExpression());
             if ($showParens) {
                 $this->codeEmitter->raw(')');
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 $this->codeEmitter->raw('helpers' . $this->createJsVariableDereference($expr->getMethodName()) . '(');
                 $arguments = $expr->getArguments();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $this->expression($arguments[$i]);
                     if ($i < count($arguments) - 1) {
                         $this->codeEmitter->raw(', ');
                     }
                 }
                 $this->codeEmitter->raw(')');
             } else {
                 if ($expr instanceof Expressions\GetAttribute) {
                     $this->expression($expr->getExpression());
                     $this->codeEmitter->raw($this->createJsVariableDereference($expr->getAttributeName()));
                 } else {
                     if ($expr instanceof Expressions\Variable) {
                         $this->codeEmitter->raw($this->currentContext . $this->createJsVariableDereference($expr->getVariableName()));
                     } else {
                         if ($expr instanceof Expressions\Scalar) {
                             $this->codeEmitter->raw($this->escapeScalar($expr->getValue()));
                         } else {
                             throw new \LogicException("Unknown expression class " . get_class($expr));
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #5
0
 protected function expression(Expression $expr)
 {
     if ($expr instanceof Expressions\Binary) {
         fwrite($this->output, "(");
         $this->expression($expr->getLeft());
         fwrite($this->output, " " . $expr->getOperation() . " ");
         $this->expression($expr->getRight());
         fwrite($this->output, ")");
     } else {
         if ($expr instanceof Expressions\Unary) {
             $showParens = !$this->canParensBeOmittedFor($expr->getExpression());
             fwrite($this->output, $expr->getOperation());
             if ($showParens) {
                 fwrite($this->output, "(");
             }
             $this->expression($expr->getExpression());
             if ($showParens) {
                 fwrite($this->output, ")");
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 fwrite($this->output, $expr->getMethodName() . "(");
                 $arguments = $expr->getArguments();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $this->expression($arguments[$i]);
                     if ($i < count($arguments) - 1) {
                         fwrite($this->output, ", ");
                     }
                 }
                 fwrite($this->output, ")");
             } else {
                 if ($expr instanceof Expressions\GetAttribute) {
                     $this->expression($expr->getExpression());
                     fwrite($this->output, '[' . $this->escapeScalar($expr->getAttributeName()) . ']');
                 } else {
                     if ($expr instanceof Expressions\Variable) {
                         fwrite($this->output, '$' . $this->currentContext . '[' . $this->escapeScalar($expr->getVariableName()) . ']');
                     } else {
                         if ($expr instanceof Expressions\Scalar) {
                             fwrite($this->output, $this->escapeScalar($expr->getValue()));
                         } else {
                             throw new \LogicException("Unknown expression class " . get_class($expr));
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #6
0
 protected function expression(Expression $expr)
 {
     if ($expr instanceof Expressions\Variable) {
         if ($expr->getVariableName() === '_parent') {
             $contextCounter = max(0, $this->contextCounter - 1);
             $context = $contextCounter > 0 ? 'context' . $contextCounter : 'context';
             $this->codeEmitter->raw($context);
         } else {
             if ($expr->getVariableName() === '_top') {
                 $this->codeEmitter->raw('context');
             } else {
                 if ($expr->getVariableName() === '_') {
                     $this->codeEmitter->raw($this->currentContext);
                 } else {
                     parent::expression($expr);
                 }
             }
         }
     } else {
         if ($expr instanceof Expressions\GetAttribute && $expr->getAttributeName() === '_parent') {
             $depth = $this->resolveParentVariable($expr);
             $contextCounter = max(0, $this->contextCounter - $depth);
             $context = $contextCounter > 0 ? 'context' . $contextCounter : 'context';
             $this->codeEmitter->raw($context);
         } else {
             if ($expr instanceof Expressions\MethodCall && $expr->getMethodName() === 'if') {
                 $this->codeEmitter->raw('(');
                 $this->expression($expr->getArguments()[0]);
                 $this->codeEmitter->raw(' ? ');
                 $this->expression($expr->getArguments()[1]);
                 $this->codeEmitter->raw(' : ');
                 if (count($expr->getArguments()) === 3) {
                     $this->expression($expr->getArguments()[2]);
                 } else {
                     $this->codeEmitter->raw('\\"\\"');
                 }
                 $this->codeEmitter->raw(')');
             } else {
                 parent::expression($expr);
             }
         }
     }
 }
Beispiel #7
0
 public function toString()
 {
     return $this->left->toString() . " " . $this->operation . " " . $this->right->toString();
 }
Beispiel #8
0
 protected function expression(Expression $expr, $omitParens = false)
 {
     // TODO: implemente a more intelligent algorithm to decide whether or not to render parentheses
     if ($expr instanceof Expressions\Binary) {
         if (!$omitParens) {
             fwrite($this->output, "(");
         }
         $this->expression($expr->getLeft());
         fwrite($this->output, " " . $expr->getOperation() . " ");
         $this->expression($expr->getRight());
         if (!$omitParens) {
             fwrite($this->output, ")");
         }
     } else {
         if ($expr instanceof Expressions\Unary) {
             $omitParens |= $this->canParensBeOmittedFor($expr->getExpression());
             fwrite($this->output, $expr->getOperation());
             if (!$omitParens) {
                 fwrite($this->output, "(");
             }
             $this->expression($expr->getExpression());
             if (!$omitParens) {
                 fwrite($this->output, ")");
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 fwrite($this->output, $expr->getMethodName() . "(");
                 $arguments = $expr->getArguments();
                 for ($i = 0; $i < count($arguments); $i++) {
                     $this->expression($arguments[$i]);
                     if ($i < count($arguments) - 1) {
                         fwrite($this->output, ", ");
                     }
                 }
                 fwrite($this->output, ")");
             } else {
                 if ($expr instanceof Expressions\GetAttribute) {
                     $this->expression($expr->getExpression());
                     fwrite($this->output, '.' . $expr->getAttributeName());
                 } else {
                     if ($expr instanceof Expressions\Variable) {
                         fwrite($this->output, $expr->getVariableName());
                     } else {
                         if ($expr instanceof Expressions\Scalar) {
                             fwrite($this->output, $this->escapeScalar($expr->getValue()));
                         } else {
                             throw new \LogicException("Unknown expression class " . get_class($expr));
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #9
0
 protected function expression(Expression $expr)
 {
     if ($expr instanceof Expressions\Variable) {
         if ($expr->getVariableName() === '_parent') {
             $contextCounter = max(0, $this->contextCounter - 1);
             $context = $contextCounter > 0 ? 'context' . $contextCounter : 'context';
             fwrite($this->output, '$' . $context);
             return;
         } else {
             if ($expr->getVariableName() === '_top') {
                 fwrite($this->output, '$context');
                 return;
             } else {
                 if ($expr->getVariableName() === '_') {
                     fwrite($this->output, '$' . $this->currentContext);
                     return;
                 } else {
                     // convert variable dereferences into fetchProperty() calls
                     $expr = new Expressions\MethodCall('\\Plitz\\Bindings\\Blitz\\TemplateFunctions::fetchProperty', [new Expressions\Variable('_'), new Expressions\Scalar($expr->getVariableName())]);
                 }
             }
         }
     } else {
         if ($expr instanceof Expressions\GetAttribute) {
             if ($expr->getAttributeName() === '_parent') {
                 $depth = $this->resolveParentVariable($expr);
                 $contextCounter = max(0, $this->contextCounter - $depth);
                 $context = $contextCounter > 0 ? 'context' . $contextCounter : 'context';
                 fwrite($this->output, '$' . $context);
                 return;
             } else {
                 $expr = $this->convertGetAttributeToFetchPropertyCall($expr);
             }
         } else {
             if ($expr instanceof Expressions\MethodCall) {
                 // TODO: resolve method calls
                 if ($expr->getMethodName() === 'if') {
                     fwrite($this->output, '(');
                     $this->expression($expr->getArguments()[0]);
                     fwrite($this->output, ' ? ');
                     $this->expression($expr->getArguments()[1]);
                     fwrite($this->output, ' : ');
                     if (count($expr->getArguments()) === 3) {
                         $this->expression($expr->getArguments()[2]);
                     } else {
                         fwrite($this->output, 'null');
                     }
                     fwrite($this->output, ')');
                     return;
                 } else {
                     if ($expr->getMethodName() === 'escape') {
                         $expr = new Expressions\MethodCall('htmlentities', [$expr->getArguments()[0], new Expressions\Scalar(ENT_QUOTES), new Expressions\Scalar(ini_get('default_charset'))]);
                     } else {
                         // TODO: support configurable method lookup order
                         // resolve method call
                         if (method_exists($this->blitz, $expr->getMethodName())) {
                             // this is a Blitz call, use $blitz variable
                             fwrite($this->output, '$blitz->');
                         } else {
                             if (function_exists($expr->getMethodName())) {
                                 // this is a regular PHP call, do nothing
                             } else {
                                 throw new \RuntimeException("Function {$expr->getMethodName()} could not be resolved");
                             }
                         }
                     }
                 }
             }
         }
     }
     parent::expression($expr);
 }