* @author Kornel LesiƄski * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License * @version SVN: $Id: State.php 610 2009-05-24 00:32:13Z kornel $ * @link http://phptal.org/ */ require_once 'PHPTAL/Tales.php'; /** * @package PHPTAL * @subpackage Php */ class PHPTAL_Php_State { private $_debug = false; private $_talesMode = 'tales'; private $_encoding = 'UTF-8'; private $_outputMode = PHPTAL::XHTML; private $cache_basename = '/tmp/phptal'; /** * used by codegenerator to pass information from PHPTAL class. Don't use otherwise. */ public function setCacheFilesBaseName($name) { $this->cache_basename = $name; } /** * used by codewriter to get information for phptal:cache */ public function getCacheFilesBaseName() { return $this->cache_basename; } /** * controlled by phptal:debug */ public function setDebug($bool) { $old = $this->_debug; $this->_debug = $bool; return $old; } /** * if true, add additional diagnostic information to generated code */ public function isDebugOn() { return $this->_debug; } /** * Sets new and returns old TALES mode. * Valid modes are 'tales' and 'php' * * @param string $mode * @return string */ public function setTalesMode($mode) { $old = $this->_talesMode; $this->_talesMode = $mode; return $old; } public function getTalesMode() { return $this->_talesMode; } /** * must be same as input's encoding and can't change. */ public function setEncoding($enc) { $this->_encoding = $enc; } /** * encoding used for both template input and output */ public function getEncoding() { return $this->_encoding; } /** * @param $mode one of PHPTAL::XHTML, PHPTAL::XML, PHPTAL::HTML5 */ public function setOutputMode($mode) { $this->_outputMode = $mode; } /** * Syntax rules to follow in generated code * * @return one of PHPTAL::XHTML, PHPTAL::XML, PHPTAL::HTML5 */ public function getOutputMode() { return $this->_outputMode; } /** * compile TALES expression according to current talesMode * @return string with PHP code or array with expressions for TalesChainExecutor */ public function evaluateExpression($expression) { if ($this->_talesMode === 'php') { return PHPTAL_Php_TalesInternal::php($expression); } return PHPTAL_Php_TalesInternal::compileToPHPStatements($expression,false); } /** * compile TALES expression according to current talesMode * @return string with PHP code */ private function compileTalesToPHPExpression($expression) { if ($this->_talesMode === 'php') { return PHPTAL_Php_TalesInternal::php($expression); } return PHPTAL_Php_TalesInternal::compileToPHPExpression($expression,false); } /** * returns PHP code that generates given string, including dynamic replacements */ public function interpolateTalesVarsInString($string) { if ($this->_talesMode == 'tales') { return PHPTAL_Php_TalesInternal::string($string); } // replace ${var} found in expression while (preg_match('/(?getEncoding()); return $this->compileTalesToPHPExpression($src); } /** * callback for interpolation of TALES with structure keyword, i.e. output without HTML-escapes, * but input with HTML-escapes. */ private function _interpolateTalesVarsHTMLStructure($matches) { return 'stringify($this->_interpolateTalesVars($matches[1])).' ?>'; } /** * callback for interpolation of TALES with structure keyword, i.e. input and output without HTML-escapes. */ private function _interpolateTalesVarsCDATAStructure($matches) { return 'stringify($this->compileTalesToPHPExpression($matches[1])).' ?>'; } /** * callback for interpolating TALES with HTML-escaping */ private function _interpolateTalesVarsHTML($matches) { return 'htmlchars($this->_interpolateTalesVars($matches[1])).' ?>'; } /** * callback for interpolating TALES with CDATA escaping */ private function _interpolateTalesVarsCDATA($matches) { $code = $this->compileTalesToPHPExpression($matches[1]); // quite complex for an "unescaped" section, isn't it? if ($this->getOutputMode() === PHPTAL::HTML5) { return "stringify($code).") ?>"; } elseif ($this->getOutputMode() === PHPTAL::XHTML) { // both XML and HMTL, because people will inevitably send it as text/html :( return "stringify($code)." ,array(']]>'=>']]]]>',''<\\/')) ?>"; } else { return "',']]]]>', ".$this->stringify($code).") ?>"; } } /** * replaces ${} in string, expecting HTML-encoded input and HTML-escapes output */ public function interpolateTalesVarsInHtml($src) { // uses lookback assertion to exclude $${} $result = preg_replace_callback('/(?