Exemple #1
0
 /**
  * The compile method.
  *
  * @params   object Fly_Flexy
  * @params   string|false string to compile of false to use a file.
  * @return   string   filename of template
  * @access   public
  */
 function compile(&$flexy, $string = false)
 {
     // read the entire file into one variable
     // note this should be moved to new Fly_Flexy_Token
     // and that can then manage all the tokens in one place..
     global $_FLY_FLEXY_COMPILER;
     $this->currentTemplate = $flexy->currentTemplate;
     $gettextStrings =& $_FLY_FLEXY_COMPILER['gettextStrings'];
     $gettextStrings = array();
     // reset it.
     if (@$this->options['debug']) {
         echo "compiling template {$flexy->currentTemplate}<BR>";
     }
     // reset the elements.
     $flexy->_elements = array();
     // replace this with a singleton??
     $GLOBALS['_FLY_FLEXY']['currentOptions'] = $this->options;
     $GLOBALS['_FLY_FLEXY']['elements'] = array();
     $GLOBALS['_FLY_FLEXY']['filename'] = $flexy->currentTemplate;
     $GLOBALS['_FLY_FLEXY']['prefixOutput'] = '';
     $GLOBALS['_FLY_FLEXY']['compiledTemplate'] = $flexy->compiledTemplate;
     // initialize Translation 2, and
     $this->initializeTranslator();
     // load the template!
     $data = $string;
     $res = false;
     if ($string === false) {
         $data = file_get_contents($flexy->currentTemplate);
     }
     // PRE PROCESS {_(.....)} translation markers.
     if (strpos($data, '{_(') !== false) {
         $data = $this->preProcessTranslation($data);
     }
     // Tree generation!!!
     if (!$this->options['forceCompile'] && isset($_FLY_FLEXY_COMPILER['cache'][md5($data)])) {
         $res = $_FLY_FLEXY_COMPILER['cache'][md5($data)];
     } else {
         $tokenizer = new Fly_Flexy_Tokenizer($data);
         $tokenizer->fileName = $flexy->currentTemplate;
         //$tokenizer->debug=1;
         $tokenizer->options['ignore_html'] = $this->options['nonHTML'];
         require_once 'Fly/Flexy/Token.php';
         $res = Fly_Flexy_Token::buildTokens($tokenizer);
         if ($res instanceof PEAR_Error) {
             return $res;
         }
         $_FLY_FLEXY_COMPILER['cache'][md5($data)] = $res;
     }
     // technically we shouldnt get here as we dont cache errors..
     if ($res instanceof PEAR_Error) {
         return $res;
     }
     // turn tokens into Template..
     //var_dump($res);exit();
     //echo $this->toString($res);exit();
     $data = $res->compile($this);
     if ($data instanceof PEAR_Error) {
         return $data;
     }
     $data = $GLOBALS['_FLY_FLEXY']['prefixOutput'] . $data;
     if ($flexy->options['debug'] > 1) {
         echo "<B>Result: </B><PRE>" . htmlspecialchars($data) . "</PRE><BR>\n";
     }
     if ($this->options['nonHTML']) {
         $data = str_replace("?>\n", "?>\n\n", $data);
     }
     // at this point we are into writing stuff...
     if ($flexy->options['compileToString']) {
         if ($flexy->options['debug']) {
             echo "<B>Returning string:<BR>\n";
         }
         $flexy->elements = $GLOBALS['_FLY_FLEXY']['elements'];
         return $data;
     }
     // error checking?
     $file = $flexy->compiledTemplate;
     if (isset($flexy->options['output.block'])) {
         list($file, $part) = explode('#', $file);
     }
     if ($cfp = fopen($file, 'w')) {
         if ($flexy->options['debug']) {
             echo "<B>Writing: </B>{$file}<BR>\n";
         }
         fwrite($cfp, $data);
         fclose($cfp);
         chmod($file, 0775);
         // make the timestamp of the two items match.
         clearstatcache();
         touch($file, filemtime($flexy->currentTemplate));
         if ($file != $flexy->compiledTemplate) {
             chmod($flexy->compiledTemplate, 0775);
             // make the timestamp of the two items match.
             clearstatcache();
             touch($flexy->compiledTemplate, filemtime($flexy->currentTemplate));
         }
     } else {
         return Fly_Flexy::raiseError('Fly_Flexy::failed to write to ' . $flexy->compiledTemplate, FLY_FLEXY_ERROR_FILE, FLY_FLEXY_ERROR_RETURN);
     }
     // gettext strings
     if (file_exists($flexy->getTextStringsFile)) {
         unlink($flexy->getTextStringsFile);
     }
     if ($gettextStrings && ($cfp = fopen($flexy->getTextStringsFile, 'w'))) {
         fwrite($cfp, serialize(array_unique($gettextStrings)));
         fclose($cfp);
         chmod($flexy->getTextStringsFile, 0664);
     }
     // elements
     if (file_exists($flexy->elementsFile)) {
         unlink($flexy->elementsFile);
     }
     if ($GLOBALS['_FLY_FLEXY']['elements'] && ($cfp = fopen($flexy->elementsFile, 'w'))) {
         fwrite($cfp, serialize($GLOBALS['_FLY_FLEXY']['elements']));
         fclose($cfp);
         chmod($flexy->elementsFile, 0664);
         // now clear it.
     }
     return true;
 }
Exemple #2
0
 /**
  * Build the child array for each element.
  * RECURSIVE FUNCTION!!!!
  * @param   int  id of node to add children to.
  *
  * @access   public
  * @static
  */
 public static function buildChildren($id)
 {
     global $_FLY_FLEXY_TOKEN;
     $base =& $_FLY_FLEXY_TOKEN['tokens'][$id];
     $base->children = array();
     $start = $base->id + 1;
     $end = $base->close->id;
     for ($i = $start; $i < $end; $i++) {
         //echo "{$base->id}:{$base->tag} ADDING {$i}{$_FLY_FLEXY_TOKEN['tokens'][$i]->tag}<BR>";
         //if ($base->id == 1176) {
         //    echo "<PRE>";print_r($_FLY_FLEXY_TOKEN['tokens'][$i]);
         // }
         $base->children[] =& $_FLY_FLEXY_TOKEN['tokens'][$i];
         if (isset($_FLY_FLEXY_TOKEN['tokens'][$i]) && is_object($_FLY_FLEXY_TOKEN['tokens'][$i]->close)) {
             // if the close id is greater than my id - ignore it! -
             if ($_FLY_FLEXY_TOKEN['tokens'][$i]->close->id > $end) {
                 continue;
             }
             Fly_Flexy_Token::buildChildren($i);
             $i = $_FLY_FLEXY_TOKEN['tokens'][$i]->close->id;
         }
     }
 }