summaryrefslogtreecommitdiff
path: root/framework/Web/UI/TTemplateManager.php
blob: 2703500a3d87aa70fc09c2d86e1ffd23020b8b83 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php

class TTemplateManager extends TComponent implements IModule
{
	const TEMPLATE_FILE_EXT='.tpl';
	const TEMPLATE_CACHE_PREFIX='prado:template:';
	private $_application;
	/**
	 * @var string module ID
	 */
	private $_id;

	public function init($application,$config)
	{
		$this->_application=$application;
	}

	/**
	 * @return string id of this module
	 */
	public function getID()
	{
		return $this->_id;
	}

	/**
	 * @param string id of this module
	 */
	public function setID($value)
	{
		$this->_id=$value;
	}

	public function getTemplateByClassName($className)
	{
		$class=new ReflectionClass($className);
		$tplFile=dirname($class->getFileName()).'/'.$className.self::TEMPLATE_FILE_EXT;
		return $this->getTemplateByFileName($tplFile);
	}

	public function getTemplateByFileName($fileName)
	{
		if(($fileName=realpath($fileName))!==false && is_file($fileName))
		{
			if(($cache=$this->_application->getCache())===null)
				return new TTemplate(file_get_contents($fileName),dirname($fileName));
			else
			{
				$array=$cache->get(self::TEMPLATE_CACHE_PREFIX.$fileName);
				if(is_array($array))
				{
					list($template,$timestamp)=$array;
					if(filemtime($fileName)<$timestamp)
						return $template;
				}
				$template=new TTemplate(file_get_contents($fileName),dirname($fileName));
				$cache->set(self::TEMPLATE_CACHE_PREFIX.$fileName,array($template,time()));
				return $template;
			}
		}
		else
			return null;
	}
}

?>