コード例 #1
0
ファイル: PhpCompiler.php プロジェクト: mcuelenaere/plitz
 public function printBlock(Expression $value)
 {
     if ($value instanceof Expressions\MethodCall && $value->getMethodName() === 'assignVar') {
         fwrite($this->output, '<?php $' . $this->currentContext . '[');
         $this->expression($value->getArguments()[0]);
         fwrite($this->output, '] = ');
         $this->expression($value->getArguments()[1]);
         fwrite($this->output, '; ?>');
         return;
     } else {
         if ($value instanceof Expressions\MethodCall && $value->getMethodName() === 'include') {
             $this->resolveIncludeCall($value);
             return;
         }
     }
     parent::printBlock($this->escapeExpression($value));
 }
コード例 #2
0
 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]);
 }
コード例 #3
0
ファイル: JsCompiler.php プロジェクト: mcuelenaere/plitz
 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));
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #4
0
ファイル: PhpCompiler.php プロジェクト: mcuelenaere/plitz
 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));
                         }
                     }
                 }
             }
         }
     }
 }
コード例 #5
0
ファイル: JsCompiler.php プロジェクト: mcuelenaere/plitz
 public function printBlock(Expression $value)
 {
     if ($value instanceof Expressions\MethodCall && $value->getMethodName() === 'assignVar') {
         $this->codeEmitter->lineNoEnding($this->currentContext . '[');
         $this->expression($value->getArguments()[0]);
         $this->codeEmitter->raw('] = ');
         $this->expression($value->getArguments()[1]);
         $this->codeEmitter->raw(';')->newline();
         return;
     } else {
         if ($value instanceof Expressions\MethodCall && $value->getMethodName() === 'include') {
             $this->resolveIncludeCall($value);
             return;
         }
     }
     parent::printBlock($this->escapeExpression($value));
 }
コード例 #6
0
ファイル: BlitzCompiler.php プロジェクト: mcuelenaere/plitz
 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));
                         }
                     }
                 }
             }
         }
     }
 }