Haanga_Compiler::generate_variable_name PHP Method

generate_variable_name() public method

Get variable name {{{
public generate_variable_name ( $variable, $special = true )
    function generate_variable_name($variable, $special = true)
    {
        if (is_array($variable)) {
            switch ($variable[0]) {
                case 'forloop':
                    if (!$special) {
                        return array('var' => $variable);
                    }
                    if (!$this->forid) {
                        throw new Exception("Invalid forloop reference outside of a loop");
                    }
                    switch ($variable[1]['object']) {
                        case 'counter':
                            $this->forloop[$this->forid]['counter'] = TRUE;
                            $variable = 'forcounter1_' . $this->forid;
                            break;
                        case 'counter0':
                            $this->forloop[$this->forid]['counter0'] = TRUE;
                            $variable = 'forcounter0_' . $this->forid;
                            break;
                        case 'last':
                            $this->forloop[$this->forid]['counter'] = TRUE;
                            $this->forloop[$this->forid]['last'] = TRUE;
                            $variable = 'islast_' . $this->forid;
                            break;
                        case 'first':
                            $this->forloop[$this->forid]['first'] = TRUE;
                            $variable = 'isfirst_' . $this->forid;
                            break;
                        case 'revcounter':
                            $this->forloop[$this->forid]['revcounter'] = TRUE;
                            $variable = 'revcount_' . $this->forid;
                            break;
                        case 'revcounter0':
                            $this->forloop[$this->forid]['revcounter0'] = TRUE;
                            $variable = 'revcount0_' . $this->forid;
                            break;
                        case 'parentloop':
                            unset($variable[1]);
                            $this->forid--;
                            $variable = $this->generate_variable_name(array_values($variable));
                            $variable = $variable['var'];
                            $this->forid++;
                            break;
                        default:
                            throw new Exception("Unexpected forloop.{$variable[1]}");
                    }
                    /* no need to escape it */
                    $this->var_is_safe = TRUE;
                    break;
                case 'block':
                    if (!$special) {
                        return array('var' => $variable);
                    }
                    if ($this->in_block == 0) {
                        throw new Exception("Can't use block.super outside a block");
                    }
                    if (!$this->subtemplate) {
                        throw new Exception("Only subtemplates can call block.super");
                    }
                    /* no need to escape it */
                    $this->var_is_safe = TRUE;
                    return Haanga_AST::str(self::$block_var);
                    break;
                default:
                    /* choose array or objects */
                    if ($special) {
                        // this section is resolved on the parser.y
                        return array('var' => $variable);
                    }
                    for ($i = 1; $i < count($variable); $i++) {
                        $var_part = array_slice($variable, 0, $i);
                        $def_arr = TRUE;
                        if (is_array($variable[$i])) {
                            if (isset($variable[$i]['class'])) {
                                // no type guess for static properties
                                continue;
                            }
                            if (isset($variable[$i]['object'])) {
                                $def_arr = FALSE;
                            }
                            if (!Haanga_AST::is_var($variable[$i])) {
                                $variable[$i] = current($variable[$i]);
                            } else {
                                $variable[$i] = $this->generate_variable_name($variable[$i]['var']);
                            }
                        }
                        $is_obj = $this->var_is_object($var_part, 'unknown');
                        if ($is_obj === TRUE || $is_obj == 'unknown' && !$def_arr) {
                            $variable[$i] = array('object' => $variable[$i]);
                        }
                    }
                    break;
            }
        } else {
            if (isset($this->var_alias[$variable])) {
                $variable = $this->var_alias[$variable]['var'];
            }
        }
        return hvar($variable)->getArray();
    }