예제 #1
0
 public function getJavascriptToken()
 {
     $shouldIndent = false;
     $indentPost = true;
     $value = $this->_value;
     switch ($this->_code) {
         case T_ECHO:
             $value = 'this.append';
             break;
         case T_VARIABLE:
             $value = ltrim($value, '$');
             // strip $
             //        $tmp = $this->getPreviousElement();
             //        if( $tmp && $tmp->getCode() == T_STRING && $tmp->getValue() == 'var ' ) {
             //
             //        } else if( $value !== 'this' ) {
             //          $value = 'this.get("' . $value . '")';
             //        }
             break;
         case T_OBJECT_OPERATOR:
             $value = '.';
             break;
         case T_INLINE_HTML:
             $value = 'this.append(' . json_encode($this->_value) . ');';
             $shouldIndent = true;
             break;
         case T_STRING:
             $p = $this->getPreviousElement();
             $pp = $p->getPreviousElement();
             $n = $this->getNextElement();
             if ($p && $p->getCode() == T_OBJECT_OPERATOR && $pp && $pp->getValue() == '$this') {
                 if ($n && $n->getCode() == T_WLG_PAREN_START) {
                     // function call
                 } else {
                     // object property
                     $value = 'get("' . $value . '")';
                 }
             }
             break;
         case T_WLG_STATEMENT_SEPARATOR:
             if (!$this->getParent() || !$this->getParent()->getParent() || !$this->getParent()->getParent()->getParent() || $this->getParent()->getParent()->getParent()->getCode() != T_FOR) {
                 $shouldIndent = true;
             } else {
                 $value .= ' ';
             }
             if ($this->getPreviousElement()->getCode() == T_WLG_STATEMENT_SEPARATOR) {
                 $value = '';
             }
             break;
         case T_ELSEIF:
             $value = 'else if';
             break;
         case T_WLG_BRACE_START:
             $tmp = $this->getPreviousElement();
             if ($tmp && in_array($tmp->getCode(), array(T_ELSE, T_ELSEIF, T_WLG_PAREN_STOP))) {
                 $value = ' ' . $value;
             }
             $shouldIndent = true;
             self::$_indentLevel++;
             break;
         case T_WLG_BRACE_STOP:
             $tmp = $this->getNextElement();
             if ($tmp && in_array($tmp->getCode(), array(T_ELSE, T_ELSEIF))) {
                 $value .= ' ';
             } else {
                 $shouldIndent = true;
             }
             self::$_indentLevel--;
             break;
         case T_WLG_PAREN_START:
             // For arrays, translate to braces
             $par = $this->getParent();
             if ($par && $par->getCode() == T_WLG_EXPRESSION) {
                 $par = $par->getParent();
             }
             if ($par && $par->getCode() == T_ARRAY) {
                 $value = '{';
                 self::$_indentLevel++;
                 $shouldIndent = true;
             } else {
                 $tmp = $this->getPreviousElement();
                 if ($tmp && in_array($tmp->getCode(), array(T_IF, T_FOR, T_FOREACH, T_ELSEIF, T_WHILE))) {
                     $value .= ' ';
                 }
             }
             break;
         case T_WLG_PAREN_STOP:
             // For arrays, translate to braces
             $par = $this->getParent();
             if ($par && $par->getCode() == T_WLG_EXPRESSION) {
                 $par = $par->getParent();
             }
             if ($par && $par->getCode() == T_ARRAY) {
                 self::$_indentLevel--;
                 $shouldIndent = true;
                 $indentPost = false;
                 $value = '}';
             } else {
                 $tmp = $this->getNextElement();
                 if ($tmp && $tmp->getCode() == T_WLG_BRACE_START) {
                     $value = ' ' . $value;
                 }
             }
             break;
         case T_DOUBLE_ARROW:
             $value = ' ' . ':' . ' ';
             break;
         case T_DOUBLE_COLON:
             $value = '.';
             break;
         case T_WLG_LIST_SEPARATOR:
             $par = $this->getParent();
             if ($par && $par->getCode() == T_WLG_PAREN_START) {
                 $par = $par->getParent();
             }
             if ($par && $par->getCode() == T_WLG_EXPRESSION) {
                 $par = $par->getParent();
             }
             if ($par && $par->getCode() == T_ARRAY) {
                 $shouldIndent = true;
             } else {
                 $value .= ' ';
             }
             break;
         case T_ARRAY:
             $value = '';
             // ignore
             break;
         case T_FOREACH:
             $value = 'for';
             // le sigh
             $tmp = $this->getFirstChild();
             if ($tmp) {
                 $tmp2 = $tmp->getNextSibling();
                 $tmp = $tmp->getFirstChild();
             }
             if ($tmp) {
                 $ref = $tmp->getChildByCode(T_AS);
                 $ref2 = $tmp->getChildByCode(T_DOUBLE_ARROW);
                 if ($ref) {
                     $before = $tmp->getChildrenBefore($ref);
                     if ($ref2) {
                         $after = $tmp->getChildrenBetween($ref, $ref2, false);
                         $end = $tmp->getChildrenAfter($ref2);
                     } else {
                         $end = null;
                         $after = $tmp->getChildrenAfter($ref);
                     }
                     if (!empty($before) && !empty($after)) {
                         $tmp->removeChildren($before);
                         $tmp->removeChildren($after);
                         $tmp->removeChildren($ref);
                         // Now add them back backwards -_-
                         $tmpArr = array_merge(array(Engine_Template_Token::factory(array(T_STRING, 'var '))), $after, array($ref), $before);
                         $tmp->setChildren($tmpArr);
                     }
                 }
             }
             break;
         case T_AS:
             $value = ' ' . 'in' . ' ';
             break;
         case T_OPEN_TAG:
         case T_CLOSE_TAG:
             //case T_WLG_KEYWORD_START:
         //case T_WLG_KEYWORD_START:
         case T_ENDDECLARE:
         case T_ENDFOR:
         case T_ENDFOREACH:
         case T_ENDIF:
         case T_ENDSWITCH:
         case T_ENDWHILE:
             throw new Exception('Translation error! ' . $this->getName());
             break;
     }
     if ($shouldIndent) {
         $tmp = $this->getNextElement();
         $adj = '';
         if ($tmp && $tmp->getCode() == T_WLG_BRACE_STOP) {
             $adj = "\n" . str_pad('', (self::$_indentLevel - 1) * 2, ' ');
         } else {
             $adj = "\n" . str_pad('', self::$_indentLevel * 2, ' ');
         }
         if ($indentPost) {
             $value .= $adj;
         } else {
             $value = $adj . $value;
         }
     }
     return $value;
 }
예제 #2
0
 public static function getJavascriptToken($node)
 {
     $value = $node->getValue();
     $code = $node->getCode();
     $indent = 0;
     switch ($code) {
         case T_ARRAY:
             $value = '';
             // ignore
             break;
         case T_CLOSE_TAG:
             $value = '';
             // . "\n";
             //$indent = 1;
             break;
         case T_DOC_COMMENT:
             $value = '';
             break;
         case T_DOUBLE_ARROW:
             $value = ' ' . ':' . ' ';
             break;
         case T_DOUBLE_COLON:
             $value = '.';
             break;
         case T_ECHO:
             $value = 'this.append';
             break;
         case T_ELSE:
             $value = ' else ';
             break;
         case T_ELSEIF:
             $value = ' else if ';
             break;
         case T_EMPTY:
             $value = 'this.empty';
             break;
         case T_FOREACH:
             $expression = $node->getNextSibling();
             if ($expression && $expression->getCode() == T_WLG_EXPRESSION) {
                 $block = $expression->getNextSibling();
                 if ($block && $block->getCode() == T_WLG_BLOCK) {
                     // Process the expression
                     $refPStart = $expression->getChildByCode(T_WLG_PAREN_START);
                     $refPStop = $expression->getChildByCode(T_WLG_PAREN_STOP);
                     $refAs = $expression->getChildByCode(T_AS);
                     $refArrow = $expression->getChildByCode(T_DOUBLE_ARROW);
                     if (!$refAs) {
                         throw new Exception('Translation error!');
                     }
                     $expr1 = $expression->getChildrenBetween($refPStart, $refAs, false);
                     if ($refArrow) {
                         $expr3 = $expression->getChildrenBetween($refAs, $refArrow, false);
                         $expr2 = $expression->getChildrenBetween($refArrow, $refPStop, false);
                     } else {
                         $expr3 = null;
                         $expr2 = $expression->getChildrenBetween($refAs, $refPStop, false);
                     }
                     $expression->removeChildren($refAs);
                     if ($refArrow) {
                         $expression->removeChildren($refArrow);
                     }
                     $expression->removeChildren($expr2);
                     if ($expr3) {
                         $expression->removeChildren($expr3);
                     }
                     // Ugly hack
                     $expression->addChild(Engine_Template_Token::fromInteger(T_OBJECT_OPERATOR, '->'));
                     $expression->addChild(Engine_Template_Token::fromInteger(T_STRING, 'each(function('));
                     foreach ($expr2 as $t) {
                         $expression->addChild($t);
                     }
                     if ($expr3) {
                         $expression->addChild(Engine_Template_Token::fromInteger(T_WLG_LIST_SEPARATOR, ','));
                         foreach ($expr3 as $t) {
                             $expression->addChild($t);
                         }
                     }
                     $expression->addChild(Engine_Template_Token::fromInteger(T_STRING, ') '));
                     $block->getChildByCode(T_WLG_BRACE_STOP)->addChild(Engine_Template_Token::fromInteger(T_STRING, '.bind(this)'));
                     $block->getChildByCode(T_WLG_BRACE_STOP)->addChild(Engine_Template_Token::fromInteger(T_STRING, ')'));
                     $block->getChildByCode(T_WLG_BRACE_STOP)->addChild(Engine_Template_Token::fromInteger(T_WLG_STATEMENT_SEPARATOR, ';'));
                     //            $expression->getParent()->addChildAfter(Engine_Template_Token::fromInteger(T_OBJECT_OPERATOR, '->'), $expression);
                     //            $expression->getParent()->addChildAfter(Engine_Template_Token::fromInteger(T_STRING, '->'), $expression);
                     //            echo $expression->getParent()->outputDebug();
                     //            die();
                     $value = '';
                 }
             }
             break;
         case T_INLINE_HTML:
             $value = 'this.append(' . json_encode($value) . ');';
             //$value .= "\n";
             $indent = 1;
             break;
         case T_ISSET:
             $value = 'this.isset';
             break;
         case T_OBJECT_OPERATOR:
             $value = '.';
             break;
         case T_OPEN_TAG:
             $value = '';
             break;
         case T_STRING:
             $prev = $node->getPreviousElement();
             if ($prev && $prev->getCode() == T_OBJECT_OPERATOR) {
                 $prev2 = $prev->getPreviousElement();
                 if ($prev2 && $prev2->getCode() == T_VARIABLE && $prev2->getValue() == '$this') {
                     // Check if next is paren
                     $next = $node->getNextElement();
                     if ($next && $next->getCode() != T_WLG_PAREN_START) {
                         $value = 'get("' . $value . '")';
                     } else {
                         if (!in_array($value, array('append', 'print', 'assign', 'log', 'empty', 'isset', 'isJavascript'))) {
                             // Inject actual function name as first argument
                             // Only add comma if there are other children
                             if (count($next->getParent()->getChildren()) > 0 && $next->getNextElement()->getCode() != T_WLG_PAREN_STOP) {
                                 $next->getParent()->addChildAfter(Engine_Template_Token::fromInteger(T_WLG_LIST_SEPARATOR, ','), $next);
                             }
                             $next->getParent()->addChildAfter(Engine_Template_Token::fromInteger(T_CONSTANT_ENCAPSED_STRING, '"' . $value . '"'), $next);
                             // Set self to call
                             $value = 'call';
                         }
                     }
                 }
             }
             break;
         case T_WHITESPACE:
             if (true) {
                 // Skip?
                 $value = '';
             } else {
                 // Truncate to a single space?
                 if (strlen($value) > 1 && $value[0] == ' ') {
                     $value = ' ';
                 }
             }
             break;
         case T_WLG_BRACE_START:
             self::$_indentLevel++;
             $indent = 1;
             break;
         case T_WLG_BRACE_STOP:
             self::$_indentLevel--;
             $tmp = $node->getNextElement();
             if ($tmp && in_array($tmp->getCode(), array(T_ELSE, T_ELSEIF))) {
             } else {
                 $indent = 1;
             }
             break;
         case T_WLG_LIST_SEPARATOR:
             $value .= ' ';
             break;
         case T_WLG_PAREN_START:
             // If parent.previous was an array, change to {
             if ($node->getParent() && ($tmp = $node->getParent()->getPreviousElement()) && $tmp->getCode() == T_ARRAY) {
                 $value = '{';
             }
             break;
         case T_WLG_PAREN_STOP:
             // If parent.previous was an array, change to }
             if ($node->getParent() && ($tmp = $node->getParent()->getPreviousElement()) && $tmp->getCode() == T_ARRAY) {
                 $value = '}';
             }
             break;
         case T_WLG_STATEMENT_SEPARATOR:
             if ($node->getParent() && ($tmp = $node->getParent()->getPreviousElement()) && $tmp->getCode() == T_FOR) {
                 // ignore
             } else {
                 $prev = $node->getPreviousElement();
                 if ($prev && in_array($prev->getCode(), array(T_WLG_STATEMENT_SEPARATOR, T_WLG_BRACE_START, T_WLG_BRACE_STOP, T_WLG_KEYWORD_START))) {
                     $value = '';
                 }
                 $indent = 1;
             }
             break;
         case T_VARIABLE:
             $value = ltrim($value, '$');
             // strip $
             break;
     }
     // Indent
     if ($indent) {
         // Hack to fix closing brace
         $tmp = $node->getNextElement();
         if ($tmp) {
             if ($tmp->getCode() == T_OPEN_TAG) {
                 $tmp = $tmp->getNextElement();
             }
             //        if( $tmp->getCode() != T_WLG_BRACE_STOP ) {
             //          $tmp = null;
             //        }
         }
         if ($tmp && $tmp->getCode() == T_WLG_BRACE_STOP) {
             $adj = "\n" . str_pad('', (self::$_indentLevel - 1) * 2, ' ');
         } else {
             $adj = "\n" . str_pad('', self::$_indentLevel * 2, ' ');
         }
         if ($indent === -1) {
             $value = $adj . $value;
         } else {
             if ($indent === 1) {
                 $value .= $adj;
             }
         }
     }
     return $value;
 }