<?php
/**
* Main template class
* @example:
* <code>
* <?php
* $tpl = new Template();
* $tpl->somevar = "some value";
* $tpl->applyTemplate(templatefile);
* </code>
*
* @package frea-framework
* @version 1.0RC
* @copyright 2009 frea-framework
* @author Dawid Kraczkowski hide@address.com
* @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License
*
*/
class Template
{
/**
* Directory path to cache folder
*
* @var string
*/
static private $cacheDirectory;
/**
* Directory path to template folder
*
* @var unknown_type
*/
static private $templateDirectory;
/**
* Context holder
*
* @var Template_VariableContainer
*/
private $variableContainer;
/**
* Constructor
*
*/
public function __construct()
{
if (function_exists('sys_get_temp_dir') and empty(self::$cacheDirectory)) #check for system temp directory
{
self::setCacheDirectory(sys_get_temp_dir());
}
elseif (substr(PHP_OS, 0, 3) == 'WIN') #windows
{
if (file_exists('c:\\WINNT\\Temp\\'))
{
self::setCacheDirectory('c:\\WINNT\\Temp');
}
else
{
self::setCacheDirectory('c:\\WINDOWS\\Temp\\');
}
}
//create context
$this->variableContainer = new Template_VariableContainer();
}
/**
* Gets assigned variable from context
*
* @param string $name
*/
public function __get($name)
{
$this->variableContainer->$name;
}
/**
* assign variable to a context
*
* @param string $name
* @param mixed $value
*/
public function __set($name,$value)
{
$this->variableContainer->$name = $value;
}
/**
* parse given template
*
* @param string $filename
* @throws Template_Exception
*/
public function applyTemplate($filename)
{
//check for cachedir
if(empty(self::$cacheDirectory) or !is_dir(self::$cacheDirectory))
throw new Template_Exception("You must define proper cache directory!");
//check for templatefile
if(!is_readable($filename))
{
if(!is_readable(self::$templateDirectory.$filename))
throw new Template_Exception("Cannot read from file {$filename}");
else
$filename = self::$templateDirectory.$filename;
}
//handle cache file
$cachefile = self::getCacheDirecotry().str_replace(DIRECTORY_SEPARATOR,"_",$filename);
if(is_readable($cachefile) && filemtime($cachefile)>filemtime($filename))
{
$eval = eval("?>".file_get_contents($cachefile));
}
else
{
$cp = new Template_Dom_Parser($filename);
$cb = new Template_CodeBuilder($cp->parse(),$cachefile);
$eval = $cb->build();
}
echo eval("?>".$eval);
}
/**
* import to template external template
* file
*
* @param string $path
* @return string
*/
static public function import($path)
{
$cachefile = self::getCacheDirecotry().str_replace(DIRECTORY_SEPARATOR,"_",$path);
if(!is_readable($cachefile) or filemtime($cachefile)<=filemtime($path))
{
$cp = new Template_Dom_Parser($path);
$cb = new Template_CodeBuilder($cp->parse(),$cachefile);
$cb->build();
}
return eval("?>".file_get_contents($cachefile));
}
/**
* sets directory path for cached files
*
* @param string $directory
*/
static public function setCacheDirectory($directory)
{
$p = strlen($directory)-1;
if(substr($directory,$p) != DIRECTORY_SEPARATOR)
$directory .= DIRECTORY_SEPARATOR;
self::$cacheDirectory = $directory;
}
/**
* gets cache directory path
*
* @return string
*/
static public function getCacheDirecotry()
{
return self::$cacheDirectory;
}
/**
* sets template direcorty
*
* @param string $directory
*/
static public function setTemplateDirectory($directory)
{
$p = strlen($directory)-1;
if(substr($directory,$p) != DIRECTORY_SEPARATOR)
$directory .= DIRECTORY_SEPARATOR;
self::$templateDirectory = $directory;
}
/**
* gets template direcotry
*
* @return string
*/
static public function getTemplateDirectory()
{
return self::$templateDirectory;
}
}