From d216b3147bc3f37cf2337acab5767c6a4f74aa2e Mon Sep 17 00:00:00 2001 From: emkael Date: Mon, 31 Oct 2016 21:58:33 +0100 Subject: * PHPTAL library --- lib/phptal/PHPTAL.php | 1226 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 1226 insertions(+) create mode 100644 lib/phptal/PHPTAL.php (limited to 'lib/phptal/PHPTAL.php') diff --git a/lib/phptal/PHPTAL.php b/lib/phptal/PHPTAL.php new file mode 100644 index 0000000..1e3a846 --- /dev/null +++ b/lib/phptal/PHPTAL.php @@ -0,0 +1,1226 @@ + + * @author Kornel Lesiński + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +define('PHPTAL_VERSION', '1_3_0'); + +PHPTAL::autoloadRegister(); + +/** + * PHPTAL template entry point. + * + * + * title = 'Welcome here'; + * $tpl->result = range(1, 100); + * ... + * echo $tpl->execute(); + * } + * catch (Exception $e) { + * echo $e; + * } + * ?> + * + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg + * @author Kornel Lesiński + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @link http://phptal.org/ + */ +class PHPTAL +{ + //{{{ + /** + * constants for output mode + * @see setOutputMode() + */ + const XHTML = 11; + const XML = 22; + const HTML5 = 55; + + /** + * @see getPreFilters() + */ + protected $prefilters = array(); + + /** + * Prefilters have been redesigned. Old property is no longer used. + * + * @deprecated + */ + private $_prefilter = 'REMOVED: DO NOT USE'; + protected $_postfilter = null; + + /** + * list of template source repositories given to file source resolver + */ + protected $_repositories = array(); + + /** + * template path (path that has been set, not necessarily loaded) + */ + protected $_path = null; + + /** + * template source resolvers (classes that search for templates by name) + */ + protected $resolvers = array(); + + /** + * template source (only set when not working with file) + */ + protected $_source = null; + + /** + * destination of PHP intermediate file + */ + protected $_codeFile = null; + + /** + * php function generated for the template + */ + protected $_functionName = null; + + /** + * set to true when template is ready for execution + */ + protected $_prepared = false; + + /** + * associative array of phptal:id => PHPTAL_Trigger + */ + protected $_triggers = array(); + + /** + * i18n translator + */ + protected $_translator = null; + + /** + * global execution context + */ + protected $_globalContext = null; + + /** + * current execution context + */ + protected $_context = null; + + /** + * list of on-error caught exceptions + */ + protected $_errors = array(); + + /** + * encoding used throughout + */ + protected $_encoding = 'UTF-8'; + + /** + * type of syntax used in generated templates + */ + protected $_outputMode = PHPTAL::XHTML; + /** + * should all comments be stripped + */ + + // configuration properties + + /** + * don't use code cache + */ + protected $_forceReparse = null; + + /** + * directory where code cache is + */ + private $_phpCodeDestination; + private $_phpCodeExtension = 'php'; + + /** + * number of days + */ + private $_cacheLifetime = 30; + + /** + * 1/x + */ + private $_cachePurgeFrequency = 30; + + /** + * speeds up calls to external templates + */ + private $externalMacroTemplatesCache = array(); + + //}}} + + /** + * PHPTAL Constructor. + * + * @param string $path Template file path. + */ + public function __construct($path=false) + { + $this->_path = $path; + $this->_globalContext = new stdClass(); + $this->_context = new PHPTAL_Context(); + $this->_context->setGlobal($this->_globalContext); + + if (function_exists('sys_get_temp_dir')) { + $this->setPhpCodeDestination(sys_get_temp_dir()); + } elseif (substr(PHP_OS, 0, 3) == 'WIN') { + if (file_exists('c:\\WINNT\\Temp\\')) { + $this->setPhpCodeDestination('c:\\WINNT\\Temp\\'); + } else { + $this->setPhpCodeDestination('c:\\WINDOWS\\Temp\\'); + } + } else { + $this->setPhpCodeDestination('/tmp/'); + } + } + + /** + * create + * returns a new PHPTAL object + * + * @param string $path Template file path. + * + * @return PHPTAL + */ + public static function create($path=false) + { + return new PHPTAL($path); + } + + /** + * Clone template state and context. + * + * @return void + */ + public function __clone() + { + $this->_context = $this->_context->pushContext(); + } + + /** + * Set template from file path. + * + * @param string $path filesystem path, + * or any path that will be accepted by source resolver + * + * @return $this + */ + public function setTemplate($path) + { + $this->_prepared = false; + $this->_functionName = null; + $this->_codeFile = null; + $this->_path = $path; + $this->_source = null; + $this->_context->_docType = null; + $this->_context->_xmlDeclaration = null; + return $this; + } + + /** + * Set template from source. + * + * Should be used only with temporary template sources. + * Use setTemplate() or addSourceResolver() whenever possible. + * + * @param string $src The phptal template source. + * @param string $path Fake and 'unique' template path. + * + * @return $this + */ + public function setSource($src, $path = null) + { + $this->_prepared = false; + $this->_functionName = null; + $this->_codeFile = null; + $this->_source = new PHPTAL_StringSource($src, $path); + $this->_path = $this->_source->getRealPath(); + $this->_context->_docType = null; + $this->_context->_xmlDeclaration = null; + return $this; + } + + /** + * Specify where to look for templates. + * + * @param mixed $rep string or Array of repositories + * + * @return $this + */ + public function setTemplateRepository($rep) + { + if (is_array($rep)) { + $this->_repositories = $rep; + } else { + $this->_repositories[] = $rep; + } + return $this; + } + + /** + * Get template repositories. + * + * @return array + */ + public function getTemplateRepositories() + { + return $this->_repositories; + } + + /** + * Clears the template repositories. + * + * @return $this + */ + public function clearTemplateRepositories() + { + $this->_repositories = array(); + return $this; + } + + /** + * Specify how to look for templates. + * + * @param PHPTAL_SourceResolver $resolver instance of resolver + * + * @return $this + */ + public function addSourceResolver(PHPTAL_SourceResolver $resolver) + { + $this->resolvers[] = $resolver; + return $this; + } + + /** + * Ignore XML/XHTML comments on parsing. + * Comments starting with