protected function resolveIncludeCall(Expressions\MethodCall $expression) { assert($expression->getArguments()[0] instanceof Expressions\Scalar); $filename = $this->basePath . DIRECTORY_SEPARATOR . $expression->getArguments()[0]->getValue(); $filehandle = fopen($filename, "r"); assert(is_resource($filehandle)); // make sure we don't write a new function header + footer $oldDisableFunctionBoilerPlate = $this->disableFunctionBoilerplate; $this->disableFunctionBoilerplate = true; $lexer = new Lexer($filehandle, $filename); $parser = new Parser($lexer->lex(), $this); $this->comment('@@START OF TEMPLATE ' . $filename . ' @@'); $parser->parse(); $this->comment('@@END OF TEMPLATE ' . $filename . ' @@'); $this->disableFunctionBoilerplate = $oldDisableFunctionBoilerPlate; }
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); }