Location: PHPKode > scripts > Damn Small MVC > damn-small-mvc/lib/class.mvc.inc
<?
/////////////////////////////////////////////////////////////
//  SimpleMVC framework v0.9 beta (c) Gregory A. Rozanoff, 2005
//  Copiright (C) 2003, 2004, Gregory A. Rozanoff
//
//  This library is free software; you can redistribute it and/or
//  modify it under the terms of the GNU Lesser General Public
//  License as published by the Free Software Foundation; either
//  version 2.1 of the License, or (at your option) any later version.
//  This library is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
//  Lesser General Public License for more details.
//  You should have received a copy of the GNU Lesser General Public
//  License along with this library; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// 
/////////////////////////////////////////////////////////////

class mvc extends config {

 function mvc() {
	list($path, $this->_ARGS) = explode("?", $_SERVER['REQUEST_URI']);
	$this->_URI = explode("/", $path);
	array_shift($this->_URI);
	ob_start($this->GZIP ? 'ob_gzhandler' : NULL);
	ob_implicit_flush(FALSE);
 }

 function execute() {
	$_PATH = $this->_URI;
	require $this->WEB_INF."main.php";
	$_SELF = $this->_parse($_RUN);
	if (is_array($_HEADER))
		foreach ($_HEADER as $hdr) header($hdr);
	require $this->WEB_INF."lib/".$_SELF.".php";
	if ($_ERROR) $this->_error(403, $_ERROR);
	$tmpl = $_TMPL ? $_TMPL : $_SELF;
	if ($this->DEBUG) $this->_compile($tmpl);
	@$this->_container($_ROOT, $tmpl);
 }

 function done() {
	ob_end_flush();
	unset($this);
 }

/////////////////////////////////////////////////////////////
//
//  Private methods
//
/////////////////////////////////////////////////////////////

 function _container($_ROOT, $_TMPL) {		// View Container
	if (is_array($_ROOT))
		foreach ($_ROOT as $key => $val) $$key = $val;
	require $this->CACHE_PATH.md5($_TMPL);
 }

 function _compile($name) {			// Compile PHTML::Template tags
	$tpl = $this->_summon($name);
	while (preg_match("/<TMPL_INCLUDE\s+([^\s>]+)\s*>/i", $tpl, $regs)) {
		str_replace($regs[0], "<?include '".md5($regs[1])."';?>", $tpl);
		$this->_compile($regs[1]);
	}
	$tpl = preg_replace("/<TMPL_(GM)?TIME\s+([^>]+)\s*>/i", "<?echo \\1date('\\2');?>", $tpl);
	$tpl = preg_replace("/<TMPL_TIMESTAMP\s*>/i", "<?echo time().'000';?>", $tpl);
	$tpl = preg_replace("/<TMPL_VER\s*>/i", "<?echo '".$this->VERSION."';?>", $tpl);
	$tpl = preg_replace("/<TMPL_VAR\s+(\w+)\s*>/i", "<?echo \$\\1;?>", $tpl);
	$tpl = preg_replace("/<TMPL_LOOP\s+(\w+)\s*>/i", "<?if(is_array(\$\\1))foreach(\$\\1 as \$_\\1){if(is_array(\$_\\1))foreach(\$_\\1 as \$key=>\$val) \$\$key=\$val;unset(\$_\\1);?>", $tpl);
	$tpl = preg_replace("/<TMPL_IF\s+([^\s>]+)\s*>/i", "<?if(\$\\1){?>", $tpl);
	$tpl = preg_replace("/<TMPL_UNLESS\s+([^\s>]+)\s*>/i", "<?if(!\$\\1){?>", $tpl);
	$tpl = preg_replace("/<TMPL_ELSE>/i", "<?}else{?>", $tpl);
	$tpl = preg_replace("/<TMPL_SWITCH\s+([^\s>]+)\s*>/i", "<?switch(\$\\1){default:?>", $tpl);
	$tpl = preg_replace("/<TMPL_CASE\s+([^\s>]+)\s*>/i", "<?break;case '\\1':?>", $tpl);
	$tpl = preg_replace("/<\/TMPL_SWITCH>/i", "<?break;}?>", $tpl);
	$tpl = preg_replace("/<\/TMPL_(IF|UNLESS|LOOP)>/i", "<?}?>", $tpl);
	$tpl = preg_replace("/<\/?TMPL_([^>]+)>/i", "", $tpl);
	$tpl = preg_replace("/\?>([\r\t\n\s]*?)<\?/si", "", $tpl);
	// Save compiled template
	$temp_name = tempnam($this->CACHE_PATH, "tmpl-");
	$fp = fopen($temp_name, 'w'); fputs($fp, $tpl); fclose($fp);
	$this->_store($temp_name, $name);	
 }

 function _store($src, $dst, $umask = '0111') {
	umask($umask);
	$cache = $this->CACHE_PATH.md5($dst);
	if (file_exists($cache)) {
		rename($cache, $cache."~");
		rename($src, $cache);
		unlink($cache."~");
	} else  rename($src, $cache);
 }

 function _parse($run) {			// Parse URI
	$item = array_shift($this->_URI);
	if ($item == '' && isset($run["#"])) return $run["#"];
	elseif ('array' == gettype($run[$item])) return $this->_parse($run[$item]);
	elseif ($item != '' && isset($run["*"])) {
		array_unshift($this->_URI, $item);
		return $run["*"];
	} else $this->_error(404, "<b>{$_SERVER['REQUEST_URI']}</b> not found on this site.");
 }

 function _summon($tpl) {
	$path = $this->WEB_INF."tpl/".$tpl.".tpl";
	if ($f = @fopen($path, 'rb')) {
		$content = fread($f, filesize($path));
		fclose($f);
	} else $this->_error(403, "Template <b>$tpl</b> not found.");
	return ereg_replace("\r", "", $content);
 }

 function _error($err, $msg) {			// HTTP error wrapper
	ob_end_clean();
	switch ($err) {
		case 404:
			header("HTTP/1.0 404 Not Found");
			header("Status: 404 Not Found");
			break;
		default:
			header("HTTP/1.0 403 Forbidden");
			header("Status: 403 Forbidden");
			break;
	}
	die ("<pre><h1>$err error:</h1><ul><li>$msg</li></ul><hr />{$this->VERSION}</pre>");
 }
 
}
?>
Return current item: Damn Small MVC