Haanga::Load PHP Method

Load() public static method

Load template. If the template is already compiled, just the compiled PHP file will be included an used. If the template is new, or it had changed, the Haanga compiler is loaded in memory, and the template is compiled.
public static Load ( string $file, array $vars = [], boolean $return = FALSE, array $blocks = [] ) : string | null
$file string @param array $vars @param bool $return @param array $blocks @return string|NULL
$vars array
$return boolean
$blocks array
return string | null
    public static function Load($file, $vars = array(), $return = FALSE, $blocks = array())
    {
        if (empty(self::$cache_dir)) {
            throw new Haanga_Exception("Cache dir or template dir is missing");
        }
        self::$has_compiled = FALSE;
        $tpl = self::getTemplatePath($file);
        $fnc = sha1($tpl);
        $callback = "haanga_" . $fnc;
        if (is_callable($callback)) {
            return $callback($vars, $return, $blocks);
        }
        $php = self::$hash_filename ? $fnc : $file;
        $php = self::$cache_dir . '/' . $php . '.php';
        $check = TRUE;
        if (self::$check_ttl && self::$check_get && self::$check_set) {
            /* */
            if (call_user_func(self::$check_get, $callback)) {
                /* disable checking for the next $check_ttl seconds */
                $check = FALSE;
            } else {
                $result = call_user_func(self::$check_set, $callback, TRUE, self::$check_ttl);
            }
        }
        $mtpl = filemtime($tpl);
        if (!is_file($php) || $check && $mtpl > filemtime($php)) {
            if (!is_file($tpl)) {
                /* There is no template nor compiled file */
                throw new Exception("View {$file} doesn't exists");
            }
            if (!is_dir(dirname($php))) {
                $old = umask(0);
                mkdir(dirname($php), 0777, TRUE);
                umask($old);
            }
            $fp = fopen($php, "a+");
            /* try to block PHP file */
            if (!flock($fp, LOCK_EX | LOCK_NB)) {
                /* couldn't block, another process is already compiling */
                fclose($fp);
                if (is_file($php)) {
                    /*
                     ** if there is an old version of the cache 
                     ** load it 
                     */
                    require $php;
                    if (is_callable($callback)) {
                        return $callback($vars, $return, $blocks);
                    }
                }
                /*
                 ** no luck, probably the template is new
                 ** the compilation will be done, but we won't
                 ** save it (we'll use eval instead)
                 */
                unset($fp);
            }
            /* recompile */
            $compiler = self::getCompiler();
            if (self::$debug) {
                $compiler->setDebug($php . ".dump");
            }
            try {
                $code = $compiler->compile_file($tpl, FALSE, $vars);
            } catch (Exception $e) {
                if (isset($fp)) {
                    /*
                     ** set the $php file as old (to force future
                     ** recompilation)
                     */
                    touch($php, 300, 300);
                    chmod($php, 0777);
                }
                /* re-throw exception */
                throw $e;
            }
            if (isset($fp)) {
                ftruncate($fp, 0);
                // truncate file
                fwrite($fp, "<?php" . $code);
                flock($fp, LOCK_UN);
                // release the lock
                fclose($fp);
                touch($php, $mtpl, $mtpl);
            } else {
                /* local eval */
                eval($code);
            }
            self::$has_compiled = TRUE;
        }
        if (!is_callable($callback)) {
            /* Load the cached PHP file */
            require $php;
            if (!is_callable($callback)) {
                /* 
                  really weird case ($php is empty, another process is compiling
                  the $tpl for the first time), so create a lambda function
                  for the template.
                
                  To be safe we're invalidating its time, because its content 
                  is no longer valid to us
                */
                touch($php, 300, 300);
                chmod($php, 0777);
                // compile temporarily
                $compiler = self::getCompiler();
                $code = $compiler->compile_file($tpl, FALSE, $vars);
                eval($code);
                return $callback($vars, $return, $blocks);
            }
        }
        if (!isset($HAANGA_VERSION) || $HAANGA_VERSION != HAANGA_VERSION) {
            touch($php, 300, 300);
            chmod($php, 0777);
        }
        return $callback($vars, $return, $blocks);
    }

Usage Example

コード例 #1
0
ファイル: comment_edit.php プロジェクト: manelio/woolr
function print_edit_form()
{
    global $link, $comment, $current_user, $site_key, $globals;
    if ($current_user->user_level != 'god' && time() - $comment->date > $globals['comment_edit_time']) {
        die;
    }
    // $rows = min(40, max(substr_count($comment->content, "\n") * 2, 8));
    echo '<div class="commentform">' . "\n";
    //echo '<form action="'.htmlspecialchars($_SERVER['PHP_SELF']).'" class="comment" method="post" enctype="multipart/form-data">'."\n";
    echo '<form action="' . htmlspecialchars($_SERVER['REQUEST_URI']) . '" class="comment" method="post" enctype="multipart/form-data">' . "\n";
    echo '<input type="hidden" name="process" value="editcomment" />' . "\n";
    echo '<input type="hidden" name="key" value="' . md5($comment->randkey . $site_key) . '" />' . "\n";
    echo '<input type="hidden" name="id" value="' . $comment->id . '" />' . "\n";
    echo '<fieldset><legend>' . _('editar comentario') . '</legend>' . "\n";
    print_simpleformat_buttons('edit-comment-' . $comment->id);
    echo '<div style="clear: right"><textarea name="comment_content" class="droparea" id="edit-comment-' . $comment->id . '" rows="5">' . $comment->content . '</textarea></div>' . "\n";
    echo '<input class="button" type="submit" name="submit" value="' . _('modificar comentario') . '" />' . "\n";
    // Allow gods to put "admin" comments which does not allow votes
    if ($current_user->user_level == 'god') {
        if ($comment->type == 'admin') {
            $checked = 'checked="true"';
        }
        echo '&nbsp;&nbsp;&nbsp;&nbsp;<label><strong>' . _('admin') . ' </strong><input name="type" type="checkbox" value="admin" ' . $checked . '/></label>' . "\n";
    }
    $vars = compact('link', 'comment');
    Haanga::Load('comment_edit.html', $vars);
    echo '</fieldset>' . "\n";
    echo '</form>' . "\n";
    echo "</div>\n";
}
All Usage Examples Of Haanga::Load