Exemplo n.º 1
0
 public function parse($tokens)
 {
     self::$tokens = $tokens;
     ob_start();
     while (self::$tokenIndex < count($tokens)) {
         if (isset($token)) {
             unset($token);
         }
         $token = $tokens[self::$tokenIndex];
         switch ($token['type']) {
             case 'nativetag':
             case 'customtag':
                 $func = $this->lookupTag($token['tag'], $token['type']);
                 if ($func) {
                     if ($token['mode'] == 'start') {
                         ob_start();
                         $token['tagscope'] = array('localscope' => array(), 'startindex' => self::$tokenIndex);
                         self::$tagStack[] =& $token;
                         $this->executeTag($func, $token, $tagscope);
                     } elseif ($token['mode'] == 'end') {
                         $token['parenttag'] =& array_pop(self::$tagStack);
                         $token['tagscope'] =& $token['parenttag']['tagscope'];
                         $token['attributes']['generatedContent'] = ob_get_clean();
                         if ($token['parenttag']['tag'] != $token['tag']) {
                             throw new CFML_Syntax_Exception("Tag nesting error - Expected </{$token['parenttag']['tag']}> but found </{$token['tag']}>");
                         }
                         $this->executeTag($func, $token);
                         echo $token['attributes']['generatedContent'];
                     } else {
                         $token['tagscope'] = array('localscope' => array(), 'startindex' => self::$tokenIndex);
                         self::$tagStack[] =& $token;
                         $this->executeTag($func, $token);
                         array_pop(self::$tagStack);
                     }
                 } else {
                     throw new CFML_Method_Exception("Custom tag not recognised: {$token['tag']}");
                 }
                 break;
             case 'text':
                 if ($this->suppressWhiteSpace) {
                     echo trim($token['text']);
                 } else {
                     echo $token['text'];
                 }
                 break;
         }
         self::$tokenIndex++;
     }
     return ob_get_clean();
 }
Exemplo n.º 2
0
 public function parse($tokens, $mode = 'runtime')
 {
     if ($mode == 'admin') {
         $fields = new FieldSet();
     }
     if ($mode == 'tags') {
         $tags = array();
     }
     self::$tokens = $tokens;
     ob_start();
     while (self::$tokenIndex < count($tokens)) {
         if (isset($token)) {
             unset($token);
         }
         $token = $tokens[self::$tokenIndex];
         $tag = CFMLTag::createFromToken($token);
         if (!$tag) {
             throw new CFML_Method_Exception("Tag not recognised: {$token['tag']}");
         }
         if (($mode == 'admin' || $mode == 'tags') && $token['type'] == 'tag') {
             $token['type'] = 'admin';
             if ($token['mode'] == 'end') {
                 self::$tokenIndex++;
                 continue;
             }
         }
         if ($token['type'] == 'tag') {
             switch ($token['mode']) {
                 case 'start':
                     ob_start();
                     $tag->startIndex = self::$tokenIndex;
                     self::$tagStack[] = $tag;
                     break;
                 case 'end':
                     $tag->startTag = array_pop(self::$tagStack);
                     $tag->tagScope =& $tag->startTag->tagScope;
                     $tag->generatedContent = ob_get_clean();
                     if ($tag->startTag->name != $tag->name) {
                         throw new CFML_Syntax_Exception("Tag nesting error - Expected </{$token['startTag']['tag']}> but found </{$token['tag']}>");
                     }
                     break;
             }
             $this->executeTag($tag, $token);
             if ($tag->generatedContent) {
                 echo $tag->generatedContent;
             }
         } elseif ($token['type'] == 'admin') {
             if ($mode == 'tags') {
                 // Get all tags in the template for decoding by another script
                 $tags[] = $tag;
             } else {
                 // Get CMS form fields for all tags in the template
                 $tagFields = $tag->getCMSFields();
                 if ($tagFields && $tagFields->Count()) {
                     foreach ($tagFields as $tagField) {
                         $fields->push($tagField);
                     }
                 }
             }
         } else {
             preg_match_all('/#\\s*(\\w+)\\s*#/', $token['text'], $matches, PREG_SET_ORDER);
             foreach ($matches as $match) {
                 $var = $match[1];
                 $replacement = '';
                 if (isset(CFML::$variables[$match[1]])) {
                     $replacement =& CFML::$variables[$match[1]];
                 } elseif (is_a(CFML::$fallback, 'ViewableData') && CFML::$fallback->hasMethod($var)) {
                     $replacement = CFML::$fallback->{$var}();
                 } elseif (is_a(CFML::$fallback, 'ViewableData') && CFML::$fallback->hasField($var)) {
                     $replacement = CFML::$fallback->getField($var);
                 }
                 $token['text'] = str_replace($match[0], $replacement, $token['text']);
             }
             if ($this->suppressWhiteSpace) {
                 echo trim($token['text']);
             } else {
                 echo $token['text'];
             }
         }
         self::$tokenIndex++;
     }
     if ($mode == 'admin') {
         ob_end_clean();
         return $fields;
     } elseif ($mode == 'tags') {
         ob_end_clean();
         return $tags;
     } else {
         return ob_get_clean();
     }
 }
Exemplo n.º 3
0
 public function parse($tokens, $mode = 'runtime')
 {
     if ($mode == 'admin') {
         $fields = new FieldSet();
     }
     self::$tokens = $tokens;
     ob_start();
     while (self::$tokenIndex < count($tokens)) {
         if (isset($token)) {
             unset($token);
         }
         $token = $tokens[self::$tokenIndex];
         $tag = CFMLTag::createFromToken($token);
         if (!$tag) {
             throw new CFML_Method_Exception("Tag not recognised: {$token['tag']}");
         }
         if ($mode == 'admin') {
             if ($token['type'] == 'tag') {
                 $token['type'] = 'admin';
             }
         }
         if ($token['type'] == 'tag') {
             switch ($token['mode']) {
                 case 'start':
                     ob_start();
                     $tag->startIndex = self::$tokenIndex;
                     self::$tagStack[] = $tag;
                     break;
                 case 'end':
                     $tag->parentTag = array_pop(self::$tagStack);
                     $tag->tagScope =& $tag->parentTag->tagScope;
                     $tag->generatedContent = ob_get_clean();
                     if ($tag->parentTag->name != $tag->name) {
                         throw new CFML_Syntax_Exception("Tag nesting error - Expected </{$token['parenttag']['tag']}> but found </{$token['tag']}>");
                     }
                     break;
             }
             $this->executeTag($tag, $token);
             if ($tag->generatedContent) {
                 echo $tag->generatedContent;
             }
         } elseif ($token['type'] == 'admin') {
             // Ghetto scaffolder
             $tagFields = $tag->getCMSFields();
             if ($tagFields && $tagFields->Count()) {
                 foreach ($tagFields as $tagField) {
                     $fields->push($tagField);
                 }
             }
         } else {
             preg_match_all('/#\\s*([^#]+)\\s*#/', $token['text'], $matches, PREG_SET_ORDER);
             foreach ($matches as $match) {
                 if (isset(CFML::$variables[$match[1]])) {
                     $replacement =& CFML::$variables[$match[1]];
                 } else {
                     throw new CFML_Parser_Exception("Undefined variable {$match[1]}");
                 }
                 $token['text'] = str_replace($match[0], $replacement, $token['text']);
             }
             if ($this->suppressWhiteSpace) {
                 echo trim($token['text']);
             } else {
                 echo $token['text'];
             }
         }
         self::$tokenIndex++;
     }
     if ($mode == 'admin') {
         return $fields;
     } else {
         return ob_get_clean();
     }
 }