protected function resolveParentVariable(Expressions\GetAttribute $expr)
 {
     $depth = 0;
     while ($expr instanceof Expressions\GetAttribute && $expr->getAttributeName() === '_parent') {
         $depth++;
         $expr = $expr->getExpression();
     }
     if (!$expr instanceof Expressions\Variable || $expr->getVariableName() !== '_parent') {
         throw new \InvalidArgumentException("You cannot do a `_parent` dereference on anything but `_parent`!");
     }
     $depth++;
     return $depth;
 }
Exemplo n.º 2
0
 /**
  * This method reduces a (recursive) GetAttribute expression to a TemplateFunctions::fetchProperty() MethodCall expression
  * @param Expressions\GetAttribute $expr
  * @return Expressions\MethodCall
  */
 protected function convertGetAttributeToFetchPropertyCall(Expressions\GetAttribute $expr)
 {
     $baseExpr = $expr->getExpression();
     if ($baseExpr instanceof Expressions\Variable && !in_array($baseExpr->getVariableName(), ['_parent', '_top', '_'])) {
         // make sure we use fetchProperty() to dereference the variable, instead of doing a normal array dereference
         $properties = [new Expressions\Variable('_'), new Expressions\Scalar($baseExpr->getVariableName())];
     } else {
         $properties = [$baseExpr];
     }
     while ($expr instanceof Expressions\GetAttribute) {
         // don't pass the "magic" attributes to fetchProperty()
         if (in_array($expr->getAttributeName(), ['_parent', '_top'])) {
             $properties[] = $expr;
             $expr = $expr->getExpression();
             continue;
         }
         $properties[] = new Expressions\Scalar($expr->getAttributeName());
         $expr = $expr->getExpression();
     }
     return new Expressions\MethodCall('\\Plitz\\Bindings\\Blitz\\TemplateFunctions::fetchProperty', $properties);
 }