diff options
Diffstat (limited to 'lib/phptal/PHPTAL/Php')
31 files changed, 4009 insertions, 0 deletions
diff --git a/lib/phptal/PHPTAL/Php/Attribute.php b/lib/phptal/PHPTAL/Php/Attribute.php new file mode 100644 index 0000000..ceb8a12 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute.php @@ -0,0 +1,98 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * Base class for all PHPTAL attributes. + * + * Attributes are first ordered by PHPTAL then called depending on their + * priority before and after the element printing. + * + * An attribute must implements start() and end(). + * + * @package PHPTAL + * @subpackage Php + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +abstract class PHPTAL_Php_Attribute +{ + const ECHO_TEXT = 'text'; + const ECHO_STRUCTURE = 'structure'; + + /** Attribute value specified by the element. */ + protected $expression; + + /** Element using this attribute (PHPTAL's counterpart of XML node) */ + protected $phpelement; + + /** + * Called before element printing. + */ + abstract function before(PHPTAL_Php_CodeWriter $codewriter); + + /** + * Called after element printing. + */ + abstract function after(PHPTAL_Php_CodeWriter $codewriter); + + function __construct(PHPTAL_Dom_Element $phpelement, $expression) + { + $this->expression = $expression; + $this->phpelement = $phpelement; + } + + /** + * Remove structure|text keyword from expression and stores it for later + * doEcho() usage. + * + * $expression = 'stucture my/path'; + * $expression = $this->extractEchoType($expression); + * + * ... + * + * $this->doEcho($code); + */ + protected function extractEchoType($expression) + { + $echoType = self::ECHO_TEXT; + $expression = trim($expression); + if (preg_match('/^(text|structure)\s+(.*?)$/ism', $expression, $m)) { + list(, $echoType, $expression) = $m; + } + $this->_echoType = strtolower($echoType); + return trim($expression); + } + + protected function doEchoAttribute(PHPTAL_Php_CodeWriter $codewriter, $code) + { + if ($this->_echoType === self::ECHO_TEXT) + $codewriter->doEcho($code); + else + $codewriter->doEchoRaw($code); + } + + protected function parseSetExpression($exp) + { + $exp = trim($exp); + // (dest) (value) + if (preg_match('/^([a-z0-9:\-_]+)\s+(.*?)$/si', $exp, $m)) { + return array($m[1], trim($m[2])); + } + // (dest) + return array($exp, null); + } + + protected $_echoType = PHPTAL_Php_Attribute::ECHO_TEXT; +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php new file mode 100644 index 0000000..5eb3fac --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Attributes.php @@ -0,0 +1,118 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * i18n:attributes + * + * This attribute will allow us to translate attributes of HTML tags, such + * as the alt attribute in the img tag. The i18n:attributes attribute + * specifies a list of attributes to be translated with optional message + * IDs? for each; if multiple attribute names are given, they must be + * separated by semi-colons. Message IDs? used in this context must not + * include whitespace. + * + * Note that the value of the particular attributes come either from the + * HTML attribute value itself or from the data inserted by tal:attributes. + * + * If an attibute is to be both computed using tal:attributes and translated, + * the translation service is passed the result of the TALES expression for + * that attribute. + * + * An example: + * + * <img src="http://foo.com/logo" alt="Visit us" + * tal:attributes="alt here/greeting" + * i18n:attributes="alt" + * /> + * + * + * In this example, let tal:attributes set the value of the alt attribute to + * the text "Stop by for a visit!". This text will be passed to the + * translation service, which uses the result of language negotiation to + * translate "Stop by for a visit!" into the requested language. The example + * text in the template, "Visit us", will simply be discarded. + * + * Another example, with explicit message IDs: + * + * <img src="../icons/uparrow.png" alt="Up" + * i18n:attributes="src up-arrow-icon; alt up-arrow-alttext" + * > + * + * Here, the message ID up-arrow-icon will be used to generate the link to + * an icon image file, and the message ID up-arrow-alttext will be used for + * the "alt" text. + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Attributes extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // split attributes to translate + foreach ($codewriter->splitExpression($this->expression) as $exp) { + list($qname, $key) = $this->parseSetExpression($exp); + + // if the translation key is specified and not empty (but may be '0') + if (strlen($key)) { + // we use it and replace the tag attribute with the result of the translation + $code = $this->_getTranslationCode($codewriter, $key); + } else { + $attr = $this->phpelement->getAttributeNode($qname); + if (!$attr) throw new PHPTAL_TemplateException("Unable to translate attribute $qname, because there is no translation key specified", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + + if ($attr->getReplacedState() === PHPTAL_Dom_Attr::NOT_REPLACED) { + $code = $this->_getTranslationCode($codewriter, $attr->getValue()); + } elseif ($attr->getReplacedState() === PHPTAL_Dom_Attr::VALUE_REPLACED && $attr->getOverwrittenVariableName()) { + // sadly variables won't be interpolated in this translation + $code = 'echo '.$codewriter->escapeCode($codewriter->getTranslatorReference(). '->translate('.$attr->getOverwrittenVariableName().', false)'); + } else { + throw new PHPTAL_TemplateException("Unable to translate attribute $qname, because other TAL attributes are using it", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + } + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteValueWithCode($code); + } + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } + + /** + * @param key - unescaped string (not PHP code) for the key + */ + private function _getTranslationCode(PHPTAL_Php_CodeWriter $codewriter, $key) + { + $code = ''; + if (preg_match_all('/\$\{(.*?)\}/', $key, $m)) { + array_shift($m); + $m = array_shift($m); + foreach ($m as $name) { + $code .= "\n".$codewriter->getTranslatorReference(). '->setVar('.$codewriter->str($name).','.PHPTAL_Php_TalesInternal::compileToPHPExpression($name).');'; // allow more complex TAL expressions + } + $code .= "\n"; + } + + // notice the false boolean which indicate that the html is escaped + // elsewhere looks like an hack doesn't it ? :) + $code .= 'echo '.$codewriter->escapeCode($codewriter->getTranslatorReference().'->translate('.$codewriter->str($key).', false)'); + return $code; + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php new file mode 100644 index 0000000..bad310f --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Data.php @@ -0,0 +1,36 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * i18n:data + * + * Since TAL always returns strings, we need a way in ZPT to translate + * objects, the most obvious case being DateTime objects. The data attribute + * will allow us to specify such an object, and i18n:translate will provide + * us with a legal format string for that object. If data is used, + * i18n:translate must be used to give an explicit message ID, rather than + * relying on a message ID computed from the content. + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Data extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter){} + public function after(PHPTAL_Php_CodeWriter $codewriter){} +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php new file mode 100644 index 0000000..92ece11 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Domain.php @@ -0,0 +1,50 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * i18n:domain + * + * The i18n:domain attribute is used to specify the domain to be used to get + * the translation. If not specified, the translation services will use a + * default domain. The value of the attribute is used directly; it is not + * a TALES expression. + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Domain extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // ensure a domain stack exists or create it + $codewriter->doIf('!isset($_i18n_domains)'); + $codewriter->pushCode('$_i18n_domains = array()'); + $codewriter->doEnd('if'); + + $expression = $codewriter->interpolateTalesVarsInString($this->expression); + + // push current domain and use new domain + $code = '$_i18n_domains[] = '.$codewriter->getTranslatorReference().'->useDomain('.$expression.')'; + $codewriter->pushCode($code); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + // restore domain + $code = $codewriter->getTranslatorReference().'->useDomain(array_pop($_i18n_domains))'; + $codewriter->pushCode($code); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php new file mode 100644 index 0000000..8a8f4e7 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Name.php @@ -0,0 +1,47 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** i18n:name + * + * Name the content of the current element for use in interpolation within + * translated content. This allows a replaceable component in content to be + * re-ordered by translation. For example: + * + * <span i18n:translate=''> + * <span tal:replace='here/name' i18n:name='name' /> was born in + * <span tal:replace='here/country_of_birth' i18n:name='country' />. + * </span> + * + * would cause this text to be passed to the translation service: + * + * "${name} was born in ${country}." + * + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Name extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->pushCode('ob_start()'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->pushCode($codewriter->getTranslatorReference().'->setVar('.$codewriter->str($this->expression).', ob_get_clean())'); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php new file mode 100644 index 0000000..9575fae --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Source.php @@ -0,0 +1,48 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * i18n:source + * + * The i18n:source attribute specifies the language of the text to be + * translated. The default is "nothing", which means we don't provide + * this information to the translation services. + * + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Source extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // ensure that a sources stack exists or create it + $codewriter->doIf('!isset($_i18n_sources)'); + $codewriter->pushCode('$_i18n_sources = array()'); + $codewriter->end(); + + // push current source and use new one + $codewriter->pushCode('$_i18n_sources[] = ' . $codewriter->getTranslatorReference(). '->setSource('.$codewriter->str($this->expression).')'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + // restore source + $code = $codewriter->getTranslatorReference().'->setSource(array_pop($_i18n_sources))'; + $codewriter->pushCode($code); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php new file mode 100644 index 0000000..9cf2a67 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Target.php @@ -0,0 +1,43 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * i18n:target + * + * The i18n:target attribute specifies the language of the translation we + * want to get. If the value is "default", the language negotiation services + * will be used to choose the destination language. If the value is + * "nothing", no translation will be performed; this can be used to suppress + * translation within a larger translated unit. Any other value must be a + * language code. + * + * The attribute value is a TALES expression; the result of evaluating the + * expression is the language code or one of the reserved values. + * + * Note that i18n:target is primarily used for hints to text extraction + * tools and translation teams. If you had some text that should only be + * translated to e.g. German, then it probably shouldn't be wrapped in an + * i18n:translate span. + * + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Target extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter){} + public function after(PHPTAL_Php_CodeWriter $codewriter){} +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php b/lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php new file mode 100644 index 0000000..a0e26c2 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/I18N/Translate.php @@ -0,0 +1,130 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * ZPTInternationalizationSupport + * + * i18n:translate + * + * This attribute is used to mark units of text for translation. If this + * attribute is specified with an empty string as the value, the message ID + * is computed from the content of the element bearing this attribute. + * Otherwise, the value of the element gives the message ID. + * + * + * @package PHPTAL + * @subpackage Php.attribute.i18n + */ +class PHPTAL_Php_Attribute_I18N_Translate extends PHPTAL_Php_Attribute_TAL_Content +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $escape = true; + $this->_echoType = PHPTAL_Php_Attribute::ECHO_TEXT; + if (preg_match('/^(text|structure)(?:\s+(.*)|\s*$)/', $this->expression, $m)) { + if ($m[1]=='structure') { $escape=false; $this->_echoType = PHPTAL_Php_Attribute::ECHO_STRUCTURE; } + $this->expression = isset($m[2])?$m[2]:''; + } + + $this->_prepareNames($codewriter, $this->phpelement); + + // if no expression is given, the content of the node is used as + // a translation key + if (strlen(trim($this->expression)) == 0) { + $key = $this->_getTranslationKey($this->phpelement, !$escape, $codewriter->getEncoding()); + $key = trim(preg_replace('/\s+/sm'.($codewriter->getEncoding()=='UTF-8'?'u':''), ' ', $key)); + if ('' === trim($key)) { + throw new PHPTAL_TemplateException("Empty translation key", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + $code = $codewriter->str($key); + } else { + $code = $codewriter->evaluateExpression($this->expression); + if (is_array($code)) + return $this->generateChainedContent($codewriter, $code); + + $code = $codewriter->evaluateExpression($this->expression); + } + + $codewriter->pushCode('echo '.$codewriter->getTranslatorReference().'->translate('.$code.','.($escape ? 'true':'false').');'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + $codewriter = $executor->getCodeWriter(); + + $escape = !($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE); + $exp = $codewriter->getTranslatorReference()."->translate($exp, " . ($escape ? 'true':'false') . ')'; + if (!$islast) { + $var = $codewriter->createTempVariable(); + $executor->doIf('!phptal_isempty('.$var.' = '.$exp.')'); + $codewriter->pushCode("echo $var"); + $codewriter->recycleTempVariable($var); + } else { + $executor->doElse(); + $codewriter->pushCode("echo $exp"); + } + } + + private function _getTranslationKey(PHPTAL_Dom_Node $tag, $preserve_tags, $encoding) + { + $result = ''; + foreach ($tag->childNodes as $child) { + if ($child instanceof PHPTAL_Dom_Text) { + if ($preserve_tags) { + $result .= $child->getValueEscaped(); + } else { + $result .= $child->getValue($encoding); + } + } elseif ($child instanceof PHPTAL_Dom_Element) { + if ($attr = $child->getAttributeNodeNS('http://xml.zope.org/namespaces/i18n', 'name')) { + $result .= '${' . $attr->getValue() . '}'; + } else { + + if ($preserve_tags) { + $result .= '<'.$child->getQualifiedName(); + foreach ($child->getAttributeNodes() as $attr) { + if ($attr->getReplacedState() === PHPTAL_Dom_Attr::HIDDEN) continue; + + $result .= ' '.$attr->getQualifiedName().'="'.$attr->getValueEscaped().'"'; + } + $result .= '>'.$this->_getTranslationKey($child, $preserve_tags, $encoding) . '</'.$child->getQualifiedName().'>'; + } else { + $result .= $this->_getTranslationKey($child, $preserve_tags, $encoding); + } + } + } + } + return $result; + } + + private function _prepareNames(PHPTAL_Php_CodeWriter $codewriter, PHPTAL_Dom_Node $tag) + { + foreach ($tag->childNodes as $child) { + if ($child instanceof PHPTAL_Dom_Element) { + if ($child->hasAttributeNS('http://xml.zope.org/namespaces/i18n', 'name')) { + $child->generateCode($codewriter); + } else { + $this->_prepareNames($codewriter, $child); + } + } + } + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php new file mode 100644 index 0000000..ef04840 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineMacro.php @@ -0,0 +1,67 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * METAL Specification 1.0 + * + * argument ::= Name + * + * Example: + * + * <p metal:define-macro="copyright"> + * Copyright 2001, <em>Foobar</em> Inc. + * </p> + * + * PHPTAL: + * + * <?php function XXX_macro_copyright($tpl) { ? > + * <p> + * Copyright 2001, <em>Foobar</em> Inc. + * </p> + * <?php } ? > + * + * @package PHPTAL + * @subpackage Php.attribute.metal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_METAL_DefineMacro extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $macroname = strtr(trim($this->expression), '-', '_'); + if (!preg_match('/^[a-z0-9_]+$/i', $macroname)) { + throw new PHPTAL_ParserException('Bad macro name "'.$macroname.'"', + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + + if ($codewriter->functionExists($macroname)) { + throw new PHPTAL_TemplateException("Macro $macroname is defined twice", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + + $codewriter->doFunction($macroname, 'PHPTAL $_thistpl, PHPTAL $tpl'); + $codewriter->doSetVar('$tpl', 'clone $tpl'); + $codewriter->doSetVar('$ctx', '$tpl->getContext()'); + $codewriter->doInitTranslator(); + $codewriter->doXmlDeclaration(true); + $codewriter->doDoctype(true); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('function'); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php new file mode 100644 index 0000000..010849a --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/DefineSlot.php @@ -0,0 +1,70 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * METAL Specification 1.0 + * + * argument ::= Name + * + * Example: + * + * <table metal:define-macro="sidebar"> + * <tr><th>Links</th></tr> + * <tr><td metal:define-slot="links"> + * <a href="/">A Link</a> + * </td></tr> + * </table> + * + * PHPTAL: (access to slots may be renamed) + * + * <?php function XXXX_macro_sidebar($tpl) { ? > + * <table> + * <tr><th>Links</th></tr> + * <tr> + * <?php if (isset($tpl->slots->links)): ? > + * <?php echo $tpl->slots->links ? > + * <?php else: ? > + * <td> + * <a href="/">A Link</a> + * </td></tr> + * </table> + * <?php } ? > + * + * @package PHPTAL + * @subpackage Php.attribute.metal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_METAL_DefineSlot extends PHPTAL_Php_Attribute +{ + private $tmp_var; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $this->tmp_var = $codewriter->createTempVariable(); + + $codewriter->doSetVar($this->tmp_var, $codewriter->interpolateTalesVarsInString($this->expression)); + $codewriter->doIf('$ctx->hasSlot('.$this->tmp_var.')'); + $codewriter->pushCode('$ctx->echoSlot('.$this->tmp_var.')'); + $codewriter->doElse(); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('if'); + + $codewriter->recycleTempVariable($this->tmp_var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php new file mode 100644 index 0000000..2dfda3a --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/FillSlot.php @@ -0,0 +1,148 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * METAL Specification 1.0 + * + * argument ::= Name + * + * Example: + * + * <table metal:use-macro="here/doc1/macros/sidebar"> + * <tr><th>Links</th></tr> + * <tr><td metal:fill-slot="links"> + * <a href="http://www.goodplace.com">Good Place</a><br> + * <a href="http://www.badplace.com">Bad Place</a><br> + * <a href="http://www.otherplace.com">Other Place</a> + * </td></tr> + * </table> + * + * PHPTAL: + * + * 1. evaluate slots + * + * <?php ob_start(); ? > + * <td> + * <a href="http://www.goodplace.com">Good Place</a><br> + * <a href="http://www.badplace.com">Bad Place</a><br> + * <a href="http://www.otherplace.com">Other Place</a> + * </td> + * <?php $tpl->slots->links = ob_get_contents(); ob_end_clean(); ? > + * + * 2. call the macro (here not supported) + * + * <?php echo phptal_macro($tpl, 'master_page.html/macros/sidebar'); ? > + * + * + * @package PHPTAL + * @subpackage Php.attribute.metal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_METAL_FillSlot extends PHPTAL_Php_Attribute +{ + private static $uid = 0; + private $function_name; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + if ($this->shouldUseCallback()) { + $function_base_name = 'slot_'.preg_replace('/[^a-z0-9]/', '_', $this->expression).'_'.(self::$uid++); + $codewriter->doFunction($function_base_name, 'PHPTAL $_thistpl, PHPTAL $tpl'); + $this->function_name = $codewriter->getFunctionPrefix().$function_base_name; + + $codewriter->doSetVar('$ctx', '$tpl->getContext()'); + $codewriter->doInitTranslator(); + } else { + $codewriter->pushCode('ob_start()'); + $this->function_name = null; + } + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + if ($this->function_name !== null) { + $codewriter->doEnd(); + $codewriter->pushCode('$ctx->fillSlotCallback('.$codewriter->str($this->expression).', '.$codewriter->str($this->function_name).', $_thistpl, clone $tpl)'); + } else { + $codewriter->pushCode('$ctx->fillSlot('.$codewriter->str($this->expression).', ob_get_clean())'); + } + } + + // rough guess + const CALLBACK_THRESHOLD = 10000; + + /** + * inspects contents of the element to decide whether callback makes sense + */ + private function shouldUseCallback() + { + // since callback is slightly slower than buffering, + // use callback only for content that is large to offset speed loss by memory savings + return $this->estimateNumberOfBytesOutput($this->phpelement, false) > self::CALLBACK_THRESHOLD; + } + + /** + * @param bool $is_nested_in_repeat true if any parent element has tal:repeat + * + * @return rough guess + */ + private function estimateNumberOfBytesOutput(PHPTAL_Dom_Element $element, $is_nested_in_repeat) + { + // macros don't output anything on their own + if ($element->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) { + return 0; + } + + $estimated_bytes = 2*(3+strlen($element->getQualifiedName())); + + foreach ($element->getAttributeNodes() as $attr) { + $estimated_bytes += 4+strlen($attr->getQualifiedName()); + if ($attr->getReplacedState() === PHPTAL_Dom_Attr::NOT_REPLACED) { + $estimated_bytes += strlen($attr->getValueEscaped()); // this is shoddy for replaced attributes + } + } + + $has_repeat_attr = $element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'repeat'); + + if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'content') || + $element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'replace')) { + // assume that output in loops is shorter (e.g. table rows) than outside (main content) + $estimated_bytes += ($has_repeat_attr || $is_nested_in_repeat) ? 500 : 2000; + } else { + foreach ($element->childNodes as $node) { + if ($node instanceof PHPTAL_Dom_Element) { + $estimated_bytes += $this->estimateNumberOfBytesOutput($node, $has_repeat_attr || $is_nested_in_repeat); + } else { + $estimated_bytes += strlen($node->getValueEscaped()); + } + } + } + + if ($element->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'use-macro')) { + $estimated_bytes += ($has_repeat_attr || $is_nested_in_repeat) ? 500 : 2000; + } + + if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'condition')) { + $estimated_bytes /= 2; // naively assuming 50% chance, that works well with if/else pattern + } + + if ($element->hasAttributeNS('http://xml.zope.org/namespaces/tal', 'repeat')) { + // assume people don't write big nested loops + $estimated_bytes *= $is_nested_in_repeat ? 5 : 10; + } + + return $estimated_bytes; + } +} diff --git a/lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php b/lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php new file mode 100644 index 0000000..83e8144 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/METAL/UseMacro.php @@ -0,0 +1,135 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * METAL Specification 1.0 + * + * argument ::= expression + * + * Example: + * + * <hr /> + * <p metal:use-macro="here/master_page/macros/copyright"> + * <hr /> + * + * PHPTAL: (here not supported) + * + * <?php echo phptal_macro( $tpl, 'master_page.html/macros/copyright'); ? > + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.metal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_METAL_UseMacro extends PHPTAL_Php_Attribute +{ + static $ALLOWED_ATTRIBUTES = array( + 'fill-slot'=>'http://xml.zope.org/namespaces/metal', + 'define-macro'=>'http://xml.zope.org/namespaces/metal', + 'define'=>'http://xml.zope.org/namespaces/tal', + ); + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $this->pushSlots($codewriter); + + foreach ($this->phpelement->childNodes as $child) { + $this->generateFillSlots($codewriter, $child); + } + + $macroname = strtr($this->expression, '-', '_'); + + // throw error if attempting to define and use macro at same time + // [should perhaps be a TemplateException? but I don't know how to set that up...] + if ($defineAttr = $this->phpelement->getAttributeNodeNS( + 'http://xml.zope.org/namespaces/metal', 'define-macro')) { + if ($defineAttr->getValue() == $macroname) + throw new PHPTAL_TemplateException("Cannot simultaneously define and use macro '$macroname'", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + + // local macro (no filename specified) and non dynamic macro name + // can be called directly if it's a known function (just generated or seen in previous compilation) + if (preg_match('/^[a-z0-9_]+$/i', $macroname) && $codewriter->functionExists($macroname)) { + $code = $codewriter->getFunctionPrefix() . $macroname . '($_thistpl, $tpl)'; + $codewriter->pushCode($code); + } + // external macro or ${macroname}, use PHPTAL at runtime to resolve it + else { + $code = $codewriter->interpolateTalesVarsInString($this->expression); + $codewriter->pushCode('$tpl->_executeMacroOfTemplate('.$code.', $_thistpl)'); + } + + $this->popSlots($codewriter); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } + + /** + * reset template slots on each macro call ? + * + * NOTE: defining a macro and using another macro on the same tag + * means inheriting from the used macro, thus slots are shared, it + * is a little tricky to understand but very natural to use. + * + * For example, we may have a main design.html containing our main + * website presentation with some slots (menu, content, etc...) then + * we may define a member.html macro which use the design.html macro + * for the general layout, fill the menu slot and let caller templates + * fill the parent content slot without interfering. + */ + private function pushSlots(PHPTAL_Php_CodeWriter $codewriter) + { + if (!$this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) { + $codewriter->pushCode('$ctx->pushSlots()'); + } + } + + /** + * generate code that pops macro slots + * (restore slots if not inherited macro) + */ + private function popSlots(PHPTAL_Php_CodeWriter $codewriter) + { + if (!$this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/metal', 'define-macro')) { + $codewriter->pushCode('$ctx->popSlots()'); + } + } + + /** + * recursively generates code for slots + */ + private function generateFillSlots(PHPTAL_Php_CodeWriter $codewriter, PHPTAL_Dom_Node $phpelement) + { + if (false == ($phpelement instanceof PHPTAL_Dom_Element)) { + return; + } + + // if the tag contains one of the allowed attribute, we generate it + foreach (self::$ALLOWED_ATTRIBUTES as $qname => $uri) { + if ($phpelement->hasAttributeNS($uri, $qname)) { + $phpelement->generateCode($codewriter); + return; + } + } + + foreach ($phpelement->childNodes as $child) { + $this->generateFillSlots($codewriter, $child); + } + } +} diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php new file mode 100644 index 0000000..3504a22 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Cache.php @@ -0,0 +1,97 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * phptal:cache (note that's not tal:cache) caches element's HTML for a given time. Time is a number with 'd', 'h', 'm' or 's' suffix. + * There's optional parameter that defines how cache should be shared. By default cache is not sensitive to template's context at all + * - it's shared between all pages that use that template. + * You can add per url to have separate copy of given element for every URL. + * + * You can add per expression to have different cache copy for every different value of an expression (which MUST evaluate to a string). + * Expression cannot refer to variables defined using tal:define on the same element. + * + * NB: + * * phptal:cache blocks can be nested, but outmost block will cache other blocks regardless of their freshness. + * * you cannot use metal:fill-slot inside elements with phptal:cache + * + * Examples: + * <div phptal:cache="3h">...</div> <!-- <div> to be evaluated at most once per 3 hours. --> + * <ul phptal:cache="1d per object/id">...</ul> <!-- <ul> be cached for one day, separately for each object. --> + * + * @package PHPTAL + * @subpackage Php.attribute.phptal +*/ +class PHPTAL_Php_Attribute_PHPTAL_Cache extends PHPTAL_Php_Attribute +{ + private $cache_filename_var; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // number or variable name followed by time unit + // optional per expression + if (!preg_match('/^\s*([0-9]+\s*|[a-zA-Z][\/a-zA-Z0-9_]*\s+)([dhms])\s*(?:\;?\s*per\s+([^;]+)|)\s*$/', $this->expression, $matches)) { + throw new PHPTAL_ParserException("Cache attribute syntax error: ".$this->expression, + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + + $cache_len = $matches[1]; + if (!is_numeric($cache_len)) { + $cache_len = $codewriter->evaluateExpression($cache_len); + + if (is_array($cache_len)) throw new PHPTAL_ParserException("Chained expressions in cache length are not supported", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + switch ($matches[2]) { + case 'd': $cache_len .= '*24'; /* no break */ + case 'h': $cache_len .= '*60'; /* no break */ + case 'm': $cache_len .= '*60'; /* no break */ + } + + $cache_tag = '"'.addslashes( $this->phpelement->getQualifiedName() . ':' . $this->phpelement->getSourceLine()).'"'; + + $cache_per_expression = isset($matches[3])?trim($matches[3]):null; + if ($cache_per_expression == 'url') { + $cache_tag .= '.$_SERVER["REQUEST_URI"]'; + } elseif ($cache_per_expression == 'nothing') { + /* do nothing */ + } elseif ($cache_per_expression) { + $code = $codewriter->evaluateExpression($cache_per_expression); + + if (is_array($code)) throw new PHPTAL_ParserException("Chained expressions in per-cache directive are not supported", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + + $cache_tag = '('.$code.')."@".' . $cache_tag; + } + + $this->cache_filename_var = $codewriter->createTempVariable(); + $codewriter->doSetVar($this->cache_filename_var, $codewriter->str($codewriter->getCacheFilesBaseName()).'.md5('.$cache_tag.')' ); + + $cond = '!file_exists('.$this->cache_filename_var.') || time() - '.$cache_len.' >= filemtime('.$this->cache_filename_var.')'; + + $codewriter->doIf($cond); + $codewriter->doEval('ob_start()'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEval('file_put_contents('.$this->cache_filename_var.', ob_get_flush())'); + $codewriter->doElse(); + $codewriter->doEval('readfile('.$this->cache_filename_var.')'); + $codewriter->doEnd('if'); + + $codewriter->recycleTempVariable($this->cache_filename_var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php new file mode 100644 index 0000000..d0e9c9b --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Debug.php @@ -0,0 +1,34 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * @package PHPTAL + * @subpackage Php.attribute.phptal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_PHPTAL_Debug extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $this->_oldMode = $codewriter->setDebug(true); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->setDebug($this->_oldMode); + } + + private $_oldMode; +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php new file mode 100644 index 0000000..dbee2a9 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Id.php @@ -0,0 +1,53 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * @package PHPTAL + * @subpackage Php.attribute.phptal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_PHPTAL_ID extends PHPTAL_Php_Attribute +{ + private $var; + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // retrieve trigger + $this->var = $codewriter->createTempVariable(); + + $codewriter->doSetVar( + $this->var, + '$tpl->getTrigger('.$codewriter->str($this->expression).')' + ); + + // if trigger found and trigger tells to proceed, we execute + // the node content + $codewriter->doIf($this->var.' && + '.$this->var.'->start('.$codewriter->str($this->expression).', $tpl) === PHPTAL_Trigger::PROCEED'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + // end of if PROCEED + $codewriter->doEnd('if'); + + // if trigger found, notify the end of the node + $codewriter->doIf($this->var); + $codewriter->pushCode( + $this->var.'->end('.$codewriter->str($this->expression).', $tpl)' + ); + $codewriter->doEnd('if'); + $codewriter->recycleTempVariable($this->var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php new file mode 100644 index 0000000..f1fb4d7 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/PHPTAL/Tales.php @@ -0,0 +1,45 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * @package PHPTAL + * @subpackage Php.attribute.phptal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_PHPTAL_TALES extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $mode = trim($this->expression); + $mode = strtolower($mode); + + if ($mode == '' || $mode == 'default') + $mode = 'tales'; + + if ($mode != 'php' && $mode != 'tales') { + throw new PHPTAL_TemplateException("Unsupported TALES mode '$mode'", + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + + $this->_oldMode = $codewriter->setTalesMode($mode); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->setTalesMode($this->_oldMode); + } + + private $_oldMode; +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php new file mode 100644 index 0000000..158d079 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Attributes.php @@ -0,0 +1,213 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * TAL Specifications 1.4 + * + * argument ::= attribute_statement [';' attribute_statement]* + * attribute_statement ::= attribute_name expression + * attribute_name ::= [namespace ':'] Name + * namespace ::= Name + * + * examples: + * + * <a href="/sample/link.html" + * tal:attributes="href here/sub/absolute_url"> + * <textarea rows="80" cols="20" + * tal:attributes="rows request/rows;cols request/cols"> + * + * IN PHPTAL: attributes will not work on structured replace. + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Attributes +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + /** before creates several variables that need to be freed in after */ + private $vars_to_recycle = array(); + + /** + * value for default keyword + */ + private $_default_escaped; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // split attributes using ; delimiter + $attrs = $codewriter->splitExpression($this->expression); + foreach ($attrs as $exp) { + list($qname, $expression) = $this->parseSetExpression($exp); + if ($expression) { + $this->prepareAttribute($codewriter, $qname, $expression); + } + } + } + + private function prepareAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $expression) + { + $tales_code = $this->extractEchoType($expression); + $code = $codewriter->evaluateExpression($tales_code); + + // XHTML boolean attribute does not appear when empty or false + if (PHPTAL_Dom_Defs::getInstance()->isBooleanAttribute($qname)) { + + // I don't want to mix code for boolean with chained executor + // so compile it again to simple expression + if (is_array($code)) { + $code = PHPTAL_Php_TalesInternal::compileToPHPExpression($tales_code); + } + return $this->prepareBooleanAttribute($codewriter, $qname, $code); + } + + // if $code is an array then the attribute value is decided by a + // tales chained expression + if (is_array($code)) { + return $this->prepareChainedAttribute($codewriter, $qname, $code); + } + + // i18n needs to read replaced value of the attribute, which is not possible if attribute is completely replaced with conditional code + if ($this->phpelement->hasAttributeNS('http://xml.zope.org/namespaces/i18n', 'attributes')) { + $this->prepareAttributeUnconditional($codewriter, $qname, $code); + } else { + $this->prepareAttributeConditional($codewriter, $qname, $code); + } + } + + /** + * attribute will be output regardless of its evaluated value. NULL behaves just like "". + */ + private function prepareAttributeUnconditional(PHPTAL_Php_CodeWriter $codewriter, $qname, $code) + { + // regular attribute which value is the evaluation of $code + $attkey = $this->getVarName($qname, $codewriter); + if ($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE) { + $value = $codewriter->stringifyCode($code); + } else { + $value = $codewriter->escapeCode($code); + } + $codewriter->doSetVar($attkey, $value); + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteValueWithVariable($attkey); + } + + /** + * If evaluated value of attribute is NULL, it will not be output at all. + */ + private function prepareAttributeConditional(PHPTAL_Php_CodeWriter $codewriter, $qname, $code) + { + // regular attribute which value is the evaluation of $code + $attkey = $this->getVarName($qname, $codewriter); + + $codewriter->doIf("null !== ($attkey = ($code))"); + + if ($this->_echoType !== PHPTAL_Php_Attribute::ECHO_STRUCTURE) + $codewriter->doSetVar($attkey, $codewriter->str(" $qname=\"").".".$codewriter->escapeCode($attkey).".'\"'"); + else + $codewriter->doSetVar($attkey, $codewriter->str(" $qname=\"").".".$codewriter->stringifyCode($attkey).".'\"'"); + + $codewriter->doElse(); + $codewriter->doSetVar($attkey, "''"); + $codewriter->doEnd('if'); + + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey); + } + + private function prepareChainedAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $chain) + { + $this->_default_escaped = false; + $this->_attribute = $qname; + if ($default_attr = $this->phpelement->getAttributeNode($qname)) { + $this->_default_escaped = $default_attr->getValueEscaped(); + } + $this->_attkey = $this->getVarName($qname, $codewriter); + $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $chain, $this); + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($this->_attkey); + } + + private function prepareBooleanAttribute(PHPTAL_Php_CodeWriter $codewriter, $qname, $code) + { + $attkey = $this->getVarName($qname, $codewriter); + + if ($codewriter->getOutputMode() === PHPTAL::HTML5) { + $value = "' $qname'"; + } else { + $value = "' $qname=\"$qname\"'"; + } + $codewriter->doIf($code); + $codewriter->doSetVar($attkey, $value); + $codewriter->doElse(); + $codewriter->doSetVar($attkey, '\'\''); + $codewriter->doEnd('if'); + $this->phpelement->getOrCreateAttributeNode($qname)->overwriteFullWithVariable($attkey); + } + + private function getVarName($qname, PHPTAL_Php_CodeWriter $codewriter) + { + $var = $codewriter->createTempVariable(); + $this->vars_to_recycle[] = $var; + return $var; + } + + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + foreach ($this->vars_to_recycle as $var) $codewriter->recycleTempVariable($var); + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $codewriter = $executor->getCodeWriter(); + $executor->doElse(); + $codewriter->doSetVar( + $this->_attkey, + "''" + ); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $codewriter = $executor->getCodeWriter(); + $executor->doElse(); + $attr_str = ($this->_default_escaped !== false) + ? ' '.$this->_attribute.'='.$codewriter->quoteAttributeValue($this->_default_escaped) // default value + : ''; // do not print attribute + $codewriter->doSetVar($this->_attkey, $codewriter->str($attr_str)); + $executor->breakChain(); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + $codewriter = $executor->getCodeWriter(); + + if (!$islast) { + $condition = "!phptal_isempty($this->_attkey = ($exp))"; + } else { + $condition = "null !== ($this->_attkey = ($exp))"; + } + $executor->doIf($condition); + + if ($this->_echoType == PHPTAL_Php_Attribute::ECHO_STRUCTURE) + $value = $codewriter->stringifyCode($this->_attkey); + else + $value = $codewriter->escapeCode($this->_attkey); + + $codewriter->doSetVar($this->_attkey, $codewriter->str(" {$this->_attribute}=\"").".$value.'\"'"); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php new file mode 100644 index 0000000..4e5896e --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Comment.php @@ -0,0 +1,30 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * @package PHPTAL + * @subpackage Php.attribute.tal + */ +class PHPTAL_Php_Attribute_TAL_Comment extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doComment($this->expression); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php new file mode 100644 index 0000000..d86b94b --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Condition.php @@ -0,0 +1,93 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= expression + * + * Example: + * + * <p tal:condition="here/copyright" + * tal:content="here/copyright">(c) 2000</p> + * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Condition +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + private $expressions = array(); + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $code = $codewriter->evaluateExpression($this->expression); + + // If it's a chained expression build a new code path + if (is_array($code)) { + $this->expressions = array(); + $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $code, $this); + return; + } + + // Force a falsy condition if the nothing keyword is active + if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + $code = 'false'; + } + + $codewriter->doIf('phptal_true(' . $code . ')'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('if'); + } + + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + // check if the expression is empty + if ($exp !== 'false') { + $this->expressions[] = '!phptal_isempty(' . $exp . ')'; + } + + if ($islast) { + // for the last one in the chain build a ORed condition + $executor->getCodeWriter()->doIf( implode(' || ', $this->expressions ) ); + // The executor will always end an if so we output a dummy if + $executor->doIf('false'); + } + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + // end the chain + $this->talesChainPart($executor, 'false', true); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + throw new PHPTAL_ParserException('\'default\' keyword not allowed on conditional expressions', + $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + } + +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php new file mode 100644 index 0000000..aef5865 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Content.php @@ -0,0 +1,95 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** TAL Specifications 1.4 + * + * argument ::= (['text'] | 'structure') expression + * + * Example: + * + * <p tal:content="user/name">Fred Farkas</p> + * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Content +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $expression = $this->extractEchoType($this->expression); + + $code = $codewriter->evaluateExpression($expression); + + if (is_array($code)) { + return $this->generateChainedContent($codewriter, $code); + } + + if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + return; + } + + if ($code == PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD) { + return $this->generateDefault($codewriter); + } + + $this->doEchoAttribute($codewriter, $code); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } + + private function generateDefault(PHPTAL_Php_CodeWriter $codewriter) + { + $this->phpelement->generateContent($codewriter, true); + } + + protected function generateChainedContent(PHPTAL_Php_CodeWriter $codewriter, $code) + { + $executor = new PHPTAL_Php_TalesChainExecutor($codewriter, $code, $this); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + if (!$islast) { + $var = $executor->getCodeWriter()->createTempVariable(); + $executor->doIf('!phptal_isempty('.$var.' = '.$exp.')'); + $this->doEchoAttribute($executor->getCodeWriter(), $var); + $executor->getCodeWriter()->recycleTempVariable($var); + } else { + $executor->doElse(); + $this->doEchoAttribute($executor->getCodeWriter(), $exp); + } + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->doElse(); + $this->generateDefault($executor->getCodeWriter()); + $executor->breakChain(); + } +} diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php new file mode 100644 index 0000000..f5c074b --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Define.php @@ -0,0 +1,193 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL spec 1.4 for tal:define content + * + * argument ::= define_scope [';' define_scope]* + * define_scope ::= (['local'] | 'global') define_var + * define_var ::= variable_name expression + * variable_name ::= Name + * + * Note: If you want to include a semi-colon (;) in an expression, it must be escaped by doubling it (;;).* + * + * examples: + * + * tal:define="mytitle template/title; tlen python:len(mytitle)" + * tal:define="global company_name string:Digital Creations, Inc." + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Define +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + private $tmp_content_var; + private $_buffered = false; + private $_defineScope = null; + private $_defineVar = null; + private $_pushedContext = false; + /** + * Prevents generation of invalid PHP code when given invalid TALES + */ + private $_chainPartGenerated=false; + + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $expressions = $codewriter->splitExpression($this->expression); + $definesAnyNonGlobalVars = false; + + foreach ($expressions as $exp) { + list($defineScope, $defineVar, $expression) = $this->parseExpression($exp); + if (!$defineVar) { + continue; + } + + $this->_defineScope = $defineScope; + + // <span tal:define="global foo" /> should be invisible, but <img tal:define="bar baz" /> not + if ($defineScope != 'global') $definesAnyNonGlobalVars = true; + + if ($this->_defineScope != 'global' && !$this->_pushedContext) { + $codewriter->pushContext(); + $this->_pushedContext = true; + } + + $this->_defineVar = $defineVar; + if ($expression === null) { + // no expression give, use content of tag as value for newly defined var. + $this->bufferizeContent($codewriter); + continue; + } + + $code = $codewriter->evaluateExpression($expression); + if (is_array($code)) { + $this->chainedDefine($codewriter, $code); + } elseif ( $code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + $this->doDefineVarWith($codewriter, 'null'); + } else { + $this->doDefineVarWith($codewriter, $code); + } + } + + // if the content of the tag was buffered or the tag has nothing to tell, we hide it. + if ($this->_buffered || (!$definesAnyNonGlobalVars && !$this->phpelement->hasRealContent() && !$this->phpelement->hasRealAttributes())) { + $this->phpelement->hidden = true; + } + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + if ($this->tmp_content_var) $codewriter->recycleTempVariable($this->tmp_content_var); + if ($this->_pushedContext) { + $codewriter->popContext(); + } + } + + private function chainedDefine(PHPTAL_Php_CodeWriter $codewriter, $parts) + { + $executor = new PHPTAL_Php_TalesChainExecutor( + $codewriter, $parts, $this + ); + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + if (!$this->_chainPartGenerated) throw new PHPTAL_TemplateException("Invalid expression in tal:define", $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + + $executor->doElse(); + $this->doDefineVarWith($executor->getCodeWriter(), 'null'); + $executor->breakChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + if (!$this->_chainPartGenerated) throw new PHPTAL_TemplateException("Invalid expression in tal:define", $this->phpelement->getSourceFile(), $this->phpelement->getSourceLine()); + + $executor->doElse(); + $this->bufferizeContent($executor->getCodeWriter()); + $executor->breakChain(); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + $this->_chainPartGenerated=true; + + if ($this->_defineScope == 'global') { + $var = '$tpl->getGlobalContext()->'.$this->_defineVar; + } else { + $var = '$ctx->'.$this->_defineVar; + } + + $cw = $executor->getCodeWriter(); + + if (!$islast) { + // must use temp variable, because expression could refer to itself + $tmp = $cw->createTempVariable(); + $executor->doIf('('.$tmp.' = '.$exp.') !== null'); + $cw->doSetVar($var, $tmp); + $cw->recycleTempVariable($tmp); + } else { + $executor->doIf('('.$var.' = '.$exp.') !== null'); + } + } + + /** + * Parse the define expression, already splitted in sub parts by ';'. + */ + public function parseExpression($exp) + { + $defineScope = false; // (local | global) + $defineVar = false; // var to define + + // extract defineScope from expression + $exp = trim($exp); + if (preg_match('/^(local|global)\s+(.*?)$/ism', $exp, $m)) { + list(, $defineScope, $exp) = $m; + $exp = trim($exp); + } + + // extract varname and expression from remaining of expression + list($defineVar, $exp) = $this->parseSetExpression($exp); + if ($exp !== null) $exp = trim($exp); + return array($defineScope, $defineVar, $exp); + } + + private function bufferizeContent(PHPTAL_Php_CodeWriter $codewriter) + { + if (!$this->_buffered) { + $this->tmp_content_var = $codewriter->createTempVariable(); + $codewriter->pushCode( 'ob_start()' ); + $this->phpelement->generateContent($codewriter); + $codewriter->doSetVar($this->tmp_content_var, 'ob_get_clean()'); + $this->_buffered = true; + } + $this->doDefineVarWith($codewriter, $this->tmp_content_var); + } + + private function doDefineVarWith(PHPTAL_Php_CodeWriter $codewriter, $code) + { + if ($this->_defineScope == 'global') { + $codewriter->doSetVar('$tpl->getGlobalContext()->'.$this->_defineVar, $code); + } else { + $codewriter->doSetVar('$ctx->'.$this->_defineVar, $code); + } + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php new file mode 100644 index 0000000..d7530b3 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/OmitTag.php @@ -0,0 +1,70 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= [expression] + * + * Example: + * + * <div tal:omit-tag="" comment="This tag will be removed"> + * <i>...but this text will remain.</i> + * </div> + * + * <b tal:omit-tag="not:bold">I may not be bold.</b> + * + * To leave the contents of a tag in place while omitting the surrounding + * start and end tag, use the omit-tag statement. + * + * If its expression evaluates to a false value, then normal processing + * of the element continues. + * + * If the expression evaluates to a true value, or there is no + * expression, the statement tag is replaced with its contents. It is up to + * the interface between TAL and the expression engine to determine the + * value of true and false. For these purposes, the value nothing is false, + * and cancellation of the action has the same effect as returning a + * false value. + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_OmitTag extends PHPTAL_Php_Attribute +{ + private $varname; + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + if (trim($this->expression) == '') { + $this->phpelement->headFootDisabled = true; + } else { + + $this->varname = $codewriter->createTempVariable(); + + // print tag header/foot only if condition is false + $cond = $codewriter->evaluateExpression($this->expression); + $this->phpelement->headPrintCondition = '('.$this->varname.' = !phptal_unravel_closure('.$cond.'))'; + $this->phpelement->footPrintCondition = $this->varname; + } + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + if ($this->varname) $codewriter->recycleTempVariable($this->varname); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php new file mode 100644 index 0000000..382d387 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/OnError.php @@ -0,0 +1,73 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= (['text'] | 'structure') expression + * + * Example: + * + * <p tal:on-error="string: Error! This paragraph is buggy!"> + * My name is <span tal:replace="here/SlimShady" />.<br /> + * (My login name is + * <b tal:on-error="string: Username is not defined!" + * tal:content="user">Unknown</b>) + * </p> + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_OnError extends PHPTAL_Php_Attribute +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doTry(); + $codewriter->pushCode('ob_start()'); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $var = $codewriter->createTempVariable(); + + $codewriter->pushCode('ob_end_flush()'); + $codewriter->doCatch('Exception '.$var); + $codewriter->pushCode('$tpl->addError('.$var.')'); + $codewriter->pushCode('ob_end_clean()'); + + $expression = $this->extractEchoType($this->expression); + + $code = $codewriter->evaluateExpression($expression); + switch ($code) { + case PHPTAL_Php_TalesInternal::NOTHING_KEYWORD: + break; + + case PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD: + $codewriter->pushHTML('<pre class="phptalError">'); + $codewriter->doEcho($var); + $codewriter->pushHTML('</pre>'); + break; + + default: + $this->doEchoAttribute($codewriter, $code); + break; + } + $codewriter->doEnd('catch'); + + $codewriter->recycleTempVariable($var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php new file mode 100644 index 0000000..d0e4c2d --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Repeat.php @@ -0,0 +1,99 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * TAL Specifications 1.4 + * + * argument ::= variable_name expression + * variable_name ::= Name + * + * Example: + * + * <p tal:repeat="txt python:'one', 'two', 'three'"> + * <span tal:replace="txt" /> + * </p> + * <table> + * <tr tal:repeat="item here/cart"> + * <td tal:content="repeat/item/index">1</td> + * <td tal:content="item/description">Widget</td> + * <td tal:content="item/price">$1.50</td> + * </tr> + * </table> + * + * The following information is available from an Iterator: + * + * * index - repetition number, starting from zero. + * * number - repetition number, starting from one. + * * even - true for even-indexed repetitions (0, 2, 4, ...). + * * odd - true for odd-indexed repetitions (1, 3, 5, ...). + * * start - true for the starting repetition (index 0). + * * end - true for the ending, or final, repetition. + * * length - length of the sequence, which will be the total number of repetitions. + * + * * letter - count reps with lower-case letters: "a" - "z", "aa" - "az", "ba" - "bz", ..., "za" - "zz", "aaa" - "aaz", and so forth. + * * Letter - upper-case version of letter. + * * roman - count reps with lower-case roman numerals: "i", "ii", "iii", "iv", "v", "vi" ... + * * Roman - upper-case version of roman numerals. + * * first - true for the first item in a group - see note below + * * lasst - true for the last item in a group - see note below + * + * Note: first and last are intended for use with sorted sequences. They try to + * divide the sequence into group of items with the same value. If you provide + * a path, then the value obtained by following that path from a sequence item + * is used for grouping, otherwise the value of the item is used. You can + * provide the path by appending it to the path from the repeat variable, + * as in "repeat/item/first/color". + * + * PHPTAL: index, number, even, etc... will be stored in the + * $ctx->repeat->'item' object. Thus $ctx->repeat->item->odd + * + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Repeat extends PHPTAL_Php_Attribute +{ + private $var; + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + $this->var = $codewriter->createTempVariable(); + + // alias to repeats handler to avoid calling extra getters on each variable access + $codewriter->doSetVar($this->var, '$ctx->repeat'); + + list($varName, $expression) = $this->parseSetExpression($this->expression); + $code = $codewriter->evaluateExpression($expression); + + // instantiate controller using expression + $codewriter->doSetVar( $this->var.'->'.$varName, 'new PHPTAL_RepeatController('.$code.')'."\n" ); + + $codewriter->pushContext(); + + // Lets loop the iterator with a foreach construct + $codewriter->doForeach('$ctx->'.$varName, $this->var.'->'.$varName); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + $codewriter->doEnd('foreach'); + $codewriter->popContext(); + + $codewriter->recycleTempVariable($this->var); + } +} + diff --git a/lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php b/lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php new file mode 100644 index 0000000..b72cafa --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Attribute/TAL/Replace.php @@ -0,0 +1,117 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * TAL Specifications 1.4 + * + * argument ::= (['text'] | 'structure') expression + * + * Default behaviour : text + * + * <span tal:replace="template/title">Title</span> + * <span tal:replace="text template/title">Title</span> + * <span tal:replace="structure table" /> + * <span tal:replace="nothing">This element is a comment.</span> + * + * + * + * @package PHPTAL + * @subpackage Php.attribute.tal + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Attribute_TAL_Replace +extends PHPTAL_Php_Attribute +implements PHPTAL_Php_TalesChainReader +{ + public function before(PHPTAL_Php_CodeWriter $codewriter) + { + // tal:replace="" => do nothing and ignore node + if (trim($this->expression) == "") { + return; + } + + $expression = $this->extractEchoType($this->expression); + $code = $codewriter->evaluateExpression($expression); + + // chained expression + if (is_array($code)) { + return $this->replaceByChainedExpression($codewriter, $code); + } + + // nothing do nothing + if ($code == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + return; + } + + // default generate default tag content + if ($code == PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD) { + return $this->generateDefault($codewriter); + } + + // replace tag with result of expression + $this->doEchoAttribute($codewriter, $code); + } + + public function after(PHPTAL_Php_CodeWriter $codewriter) + { + } + + /** + * support expressions like "foo | bar" + */ + private function replaceByChainedExpression(PHPTAL_Php_CodeWriter $codewriter, $expArray) + { + $executor = new PHPTAL_Php_TalesChainExecutor( + $codewriter, $expArray, $this + ); + } + + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->continueChain(); + } + + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor) + { + $executor->doElse(); + $this->generateDefault($executor->getCodeWriter()); + $executor->breakChain(); + } + + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $exp, $islast) + { + if (!$islast) { + $var = $executor->getCodeWriter()->createTempVariable(); + $executor->doIf('!phptal_isempty('.$var.' = '.$exp.')'); + $this->doEchoAttribute($executor->getCodeWriter(), $var); + $executor->getCodeWriter()->recycleTempVariable($var); + } else { + $executor->doElse(); + $this->doEchoAttribute($executor->getCodeWriter(), $exp); + } + } + + /** + * don't replace - re-generate default content + */ + private function generateDefault(PHPTAL_Php_CodeWriter $codewriter) + { + $this->phpelement->generateSurroundHead($codewriter); + $this->phpelement->generateHead($codewriter); + $this->phpelement->generateContent($codewriter); + $this->phpelement->generateFoot($codewriter); + $this->phpelement->generateSurroundFoot($codewriter); + } +} + diff --git a/lib/phptal/PHPTAL/Php/CodeWriter.php b/lib/phptal/PHPTAL/Php/CodeWriter.php new file mode 100644 index 0000000..44ee063 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/CodeWriter.php @@ -0,0 +1,511 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ +/** + * Helps generate php representation of a template. + * + * @package PHPTAL + * @subpackage Php + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_CodeWriter +{ + /** + * max id of variable to give as temp + */ + private $temp_var_counter=0; + /** + * stack with free'd variables + */ + private $temp_recycling=array(); + + /** + * keeps track of seen functions for function_exists + */ + private $known_functions = array(); + + + public function __construct(PHPTAL_Php_State $state) + { + $this->_state = $state; + } + + public function createTempVariable() + { + if (count($this->temp_recycling)) return array_shift($this->temp_recycling); + return '$_tmp_'.(++$this->temp_var_counter); + } + + public function recycleTempVariable($var) + { + if (substr($var, 0, 6)!=='$_tmp_') throw new PHPTAL_Exception("Invalid variable recycled"); + $this->temp_recycling[] = $var; + } + + public function getCacheFilesBaseName() + { + return $this->_state->getCacheFilesBaseName(); + } + + public function getResult() + { + $this->flush(); + if (version_compare(PHP_VERSION, '5.3', '>=') && __NAMESPACE__) { + return '<?php use '.'PHPTALNAMESPACE as P; ?>'.trim($this->_result); + } else { + return trim($this->_result); + } + } + + /** + * set full '<!DOCTYPE...>' string to output later + * + * @param string $dt + * + * @return void + */ + public function setDocType($dt) + { + $this->_doctype = $dt; + } + + /** + * set full '<?xml ?>' string to output later + * + * @param string $dt + * + * @return void + */ + public function setXmlDeclaration($dt) + { + $this->_xmldeclaration = $dt; + } + + /** + * functions later generated and checked for existence will have this prefix added + * (poor man's namespace) + * + * @param string $prefix + * + * @return void + */ + public function setFunctionPrefix($prefix) + { + $this->_functionPrefix = $prefix; + } + + /** + * @return string + */ + public function getFunctionPrefix() + { + return $this->_functionPrefix; + } + + /** + * @see PHPTAL_Php_State::setTalesMode() + * + * @param string $mode + * + * @return string + */ + public function setTalesMode($mode) + { + return $this->_state->setTalesMode($mode); + } + + public function splitExpression($src) + { + preg_match_all('/(?:[^;]+|;;)+/sm', $src, $array); + $array = $array[0]; + foreach ($array as &$a) $a = str_replace(';;', ';', $a); + return $array; + } + + public function evaluateExpression($src) + { + return $this->_state->evaluateExpression($src); + } + + public function indent() + { + $this->_indentation ++; + } + + public function unindent() + { + $this->_indentation --; + } + + public function flush() + { + $this->flushCode(); + $this->flushHtml(); + } + + public function noThrow($bool) + { + if ($bool) { + $this->pushCode('$ctx->noThrow(true)'); + } else { + $this->pushCode('$ctx->noThrow(false)'); + } + } + + public function flushCode() + { + if (count($this->_codeBuffer) == 0) return; + + // special treatment for one code line + if (count($this->_codeBuffer) == 1) { + $codeLine = $this->_codeBuffer[0]; + // avoid adding ; after } and { + if (!preg_match('/\}\s*$|\{\s*$/', $codeLine)) + $this->_result .= '<?php '.$codeLine."; ?>\n"; // PHP consumes newline + else + $this->_result .= '<?php '.$codeLine." ?>\n"; // PHP consumes newline + $this->_codeBuffer = array(); + return; + } + + $this->_result .= '<?php '."\n"; + foreach ($this->_codeBuffer as $codeLine) { + // avoid adding ; after } and { + if (!preg_match('/[{};]\s*$/', $codeLine)) { + $codeLine .= ' ;'."\n"; + } + $this->_result .= $codeLine; + } + $this->_result .= "?>\n";// PHP consumes newline + $this->_codeBuffer = array(); + } + + public function flushHtml() + { + if (count($this->_htmlBuffer) == 0) return; + + $this->_result .= implode('', $this->_htmlBuffer); + $this->_htmlBuffer = array(); + } + + /** + * Generate code for setting DOCTYPE + * + * @param bool $called_from_macro for error checking: unbuffered output doesn't support that + */ + public function doDoctype($called_from_macro = false) + { + if ($this->_doctype) { + $code = '$ctx->setDocType('.$this->str($this->_doctype).','.($called_from_macro?'true':'false').')'; + $this->pushCode($code); + } + } + + /** + * Generate XML declaration + * + * @param bool $called_from_macro for error checking: unbuffered output doesn't support that + */ + public function doXmlDeclaration($called_from_macro = false) + { + if ($this->_xmldeclaration && $this->getOutputMode() !== PHPTAL::HTML5) { + $code = '$ctx->setXmlDeclaration('.$this->str($this->_xmldeclaration).','.($called_from_macro?'true':'false').')'; + $this->pushCode($code); + } + } + + public function functionExists($name) + { + return isset($this->known_functions[$this->_functionPrefix . $name]); + } + + public function doTemplateFile($functionName, PHPTAL_Dom_Element $treeGen) + { + $this->doComment("\n*** DO NOT EDIT THIS FILE ***\n\nGenerated by PHPTAL from ".$treeGen->getSourceFile()." (edit that file instead)"); + $this->doFunction($functionName, 'PHPTAL $tpl, PHPTAL_Context $ctx'); + $this->setFunctionPrefix($functionName . "_"); + $this->doSetVar('$_thistpl', '$tpl'); + $this->doInitTranslator(); + $treeGen->generateCode($this); + $this->doComment("end"); + $this->doEnd('function'); + } + + public function doFunction($name, $params) + { + $name = $this->_functionPrefix . $name; + $this->known_functions[$name] = true; + + $this->pushCodeWriterContext(); + $this->pushCode("function $name($params) {\n"); + $this->indent(); + $this->_segments[] = 'function'; + } + + public function doComment($comment) + { + $comment = str_replace('*/', '* /', $comment); + $this->pushCode("/* $comment */"); + } + + public function doInitTranslator() + { + if ($this->_state->isTranslationOn()) { + $this->doSetVar('$_translator', '$tpl->getTranslator()'); + } + } + + public function getTranslatorReference() + { + if (!$this->_state->isTranslationOn()) { + throw new PHPTAL_ConfigurationException("i18n used, but Translator has not been set"); + } + return '$_translator'; + } + + public function doEval($code) + { + $this->pushCode($code); + } + + public function doForeach($out, $source) + { + $this->_segments[] = 'foreach'; + $this->pushCode("foreach ($source as $out):"); + $this->indent(); + } + + public function doEnd($expects = null) + { + if (!count($this->_segments)) { + if (!$expects) $expects = 'anything'; + throw new PHPTAL_Exception("Bug: CodeWriter generated end of block without $expects open"); + } + + $segment = array_pop($this->_segments); + if ($expects !== null && $segment !== $expects) { + throw new PHPTAL_Exception("Bug: CodeWriter generated end of $expects, but needs to close $segment"); + } + + $this->unindent(); + if ($segment == 'function') { + $this->pushCode("\n}\n\n"); + $this->flush(); + $functionCode = $this->_result; + $this->popCodeWriterContext(); + $this->_result = $functionCode . $this->_result; + } elseif ($segment == 'try') + $this->pushCode('}'); + elseif ($segment == 'catch') + $this->pushCode('}'); + else + $this->pushCode("end$segment"); + } + + public function doTry() + { + $this->_segments[] = 'try'; + $this->pushCode('try {'); + $this->indent(); + } + + public function doSetVar($varname, $code) + { + $this->pushCode($varname.' = '.$code); + } + + public function doCatch($catch) + { + $this->doEnd('try'); + $this->_segments[] = 'catch'; + $this->pushCode('catch('.$catch.') {'); + $this->indent(); + } + + public function doIf($condition) + { + $this->_segments[] = 'if'; + $this->pushCode('if ('.$condition.'): '); + $this->indent(); + } + + public function doElseIf($condition) + { + if (end($this->_segments) !== 'if') { + throw new PHPTAL_Exception("Bug: CodeWriter generated elseif without if"); + } + $this->unindent(); + $this->pushCode('elseif ('.$condition.'): '); + $this->indent(); + } + + public function doElse() + { + if (end($this->_segments) !== 'if') { + throw new PHPTAL_Exception("Bug: CodeWriter generated else without if"); + } + $this->unindent(); + $this->pushCode('else: '); + $this->indent(); + } + + public function doEcho($code) + { + if ($code === "''") return; + $this->flush(); + $this->pushCode('echo '.$this->escapeCode($code)); + } + + public function doEchoRaw($code) + { + if ($code === "''") return; + $this->pushCode('echo '.$this->stringifyCode($code)); + } + + public function interpolateHTML($html) + { + return $this->_state->interpolateTalesVarsInHtml($html); + } + + public function interpolateCDATA($str) + { + return $this->_state->interpolateTalesVarsInCDATA($str); + } + + public function pushHTML($html) + { + if ($html === "") return; + $this->flushCode(); + $this->_htmlBuffer[] = $html; + } + + public function pushCode($codeLine) + { + $this->flushHtml(); + $codeLine = $this->indentSpaces() . $codeLine; + $this->_codeBuffer[] = $codeLine; + } + + /** + * php string with escaped text + */ + public function str($string) + { + return "'".strtr($string,array("'"=>'\\\'','\\'=>'\\\\'))."'"; + } + + public function escapeCode($code) + { + return $this->_state->htmlchars($code); + } + + public function stringifyCode($code) + { + return $this->_state->stringify($code); + } + + public function getEncoding() + { + return $this->_state->getEncoding(); + } + + public function interpolateTalesVarsInString($src) + { + return $this->_state->interpolateTalesVarsInString($src); + } + + public function setDebug($bool) + { + return $this->_state->setDebug($bool); + } + + public function isDebugOn() + { + return $this->_state->isDebugOn(); + } + + public function getOutputMode() + { + return $this->_state->getOutputMode(); + } + + public function quoteAttributeValue($value) + { + // FIXME: interpolation is done _after_ that function, so ${} must be forbidden for now + + if ($this->getEncoding() == 'UTF-8') // HTML 5: 8.1.2.3 Attributes ; http://code.google.com/p/html5lib/issues/detail?id=93 + { + // regex excludes unicode control characters, all kinds of whitespace and unsafe characters + // and trailing / to avoid confusion with self-closing syntax + $unsafe_attr_regex = '/^$|[&=\'"><\s`\pM\pC\pZ\p{Pc}\p{Sk}]|\/$|\${/u'; + } else { + $unsafe_attr_regex = '/^$|[&=\'"><\s`\0177-\377]|\/$|\${/'; + } + + if ($this->getOutputMode() == PHPTAL::HTML5 && !preg_match($unsafe_attr_regex, $value)) { + return $value; + } else { + return '"'.$value.'"'; + } + } + + public function pushContext() + { + $this->doSetVar('$ctx', '$tpl->pushContext()'); + } + + public function popContext() + { + $this->doSetVar('$ctx', '$tpl->popContext()'); + } + + // ~~~~~ Private members ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + + private function indentSpaces() + { + return str_repeat("\t", $this->_indentation); + } + + private function pushCodeWriterContext() + { + $this->_contexts[] = clone $this; + $this->_result = ""; + $this->_indentation = 0; + $this->_codeBuffer = array(); + $this->_htmlBuffer = array(); + $this->_segments = array(); + } + + private function popCodeWriterContext() + { + $oldContext = array_pop($this->_contexts); + $this->_result = $oldContext->_result; + $this->_indentation = $oldContext->_indentation; + $this->_codeBuffer = $oldContext->_codeBuffer; + $this->_htmlBuffer = $oldContext->_htmlBuffer; + $this->_segments = $oldContext->_segments; + } + + private $_state; + private $_result = ""; + private $_indentation = 0; + private $_codeBuffer = array(); + private $_htmlBuffer = array(); + private $_segments = array(); + private $_contexts = array(); + private $_functionPrefix = ""; + private $_doctype = ""; + private $_xmldeclaration = ""; +} + diff --git a/lib/phptal/PHPTAL/Php/State.php b/lib/phptal/PHPTAL/Php/State.php new file mode 100644 index 0000000..cc4f193 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/State.php @@ -0,0 +1,254 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * @package PHPTAL + * @subpackage Php + */ +class PHPTAL_Php_State +{ + private $debug = false; + private $tales_mode = 'tales'; + private $encoding; + private $output_mode; + private $phptal; + + function __construct(PHPTAL $phptal) + { + $this->phptal = $phptal; + $this->encoding = $phptal->getEncoding(); + $this->output_mode = $phptal->getOutputMode(); + } + + /** + * used by codewriter to get information for phptal:cache + */ + public function getCacheFilesBaseName() + { + return $this->phptal->getCodePath(); + } + + /** + * true if PHPTAL has translator set + */ + public function isTranslationOn() + { + return !!$this->phptal->getTranslator(); + } + + /** + * 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->tales_mode; + $this->tales_mode = $mode; + return $old; + } + + public function getTalesMode() + { + return $this->tales_mode; + } + + /** + * encoding used for both template input and output + */ + public function getEncoding() + { + return $this->encoding; + } + + /** + * Syntax rules to follow in generated code + * + * @return one of PHPTAL::XHTML, PHPTAL::XML, PHPTAL::HTML5 + */ + public function getOutputMode() + { + return $this->output_mode; + } + + /** + * Load prefilter + */ + public function getPreFilterByName($name) + { + return $this->phptal->getPreFilterByName($name); + } + + /** + * compile TALES expression according to current talesMode + * @return string with PHP code or array with expressions for TalesChainExecutor + */ + public function evaluateExpression($expression) + { + if ($this->getTalesMode() === 'php') { + return PHPTAL_Php_TalesInternal::php($expression); + } + return PHPTAL_Php_TalesInternal::compileToPHPExpressions($expression, false); + } + + /** + * compile TALES expression according to current talesMode + * @return string with PHP code + */ + private function compileTalesToPHPExpression($expression) + { + if ($this->getTalesMode() === 'php') { + return PHPTAL_Php_TalesInternal::php($expression); + } + return PHPTAL_Php_TalesInternal::compileToPHPExpression($expression, false); + } + + /** + * returns PHP code that generates given string, including dynamic replacements + * + * It's almost unused. + */ + public function interpolateTalesVarsInString($string) + { + return PHPTAL_Php_TalesInternal::parseString($string, false, ($this->getTalesMode() === 'tales') ? '' : 'php:' ); + } + + /** + * replaces ${} in string, expecting HTML-encoded input and HTML-escapes output + */ + public function interpolateTalesVarsInHTML($src) + { + return preg_replace_callback('/((?:\$\$)*)\$\{(structure |text )?(.*?)\}|((?:\$\$)+)\{/isS', + array($this,'_interpolateTalesVarsInHTMLCallback'), $src); + } + + /** + * callback for interpolating TALES with HTML-escaping + */ + private function _interpolateTalesVarsInHTMLCallback($matches) + { + return $this->_interpolateTalesVarsCallback($matches, 'html'); + } + + /** + * replaces ${} in string, expecting CDATA (basically unescaped) input, + * generates output protected against breaking out of CDATA in XML/HTML + * (depending on current output mode). + */ + public function interpolateTalesVarsInCDATA($src) + { + return preg_replace_callback('/((?:\$\$)*)\$\{(structure |text )?(.*?)\}|((?:\$\$)+)\{/isS', + array($this,'_interpolateTalesVarsInCDATACallback'), $src); + } + + /** + * callback for interpolating TALES with CDATA escaping + */ + private function _interpolateTalesVarsInCDATACallback($matches) + { + return $this->_interpolateTalesVarsCallback($matches, 'cdata'); + } + + private function _interpolateTalesVarsCallback($matches, $format) + { + // replaces $${ with literal ${ (or $$$${ with $${ etc) + if (!empty($matches[4])) { + return substr($matches[4], strlen($matches[4])/2).'{'; + } + + // same replacement, but before executed expression + $dollars = substr($matches[1], strlen($matches[1])/2); + + $code = $matches[3]; + if ($format == 'html') { + $code = html_entity_decode($code, ENT_QUOTES, $this->getEncoding()); + } + + $code = $this->compileTalesToPHPExpression($code); + + if (rtrim($matches[2]) == 'structure') { // regex captures a space there + return $dollars.'<?php echo '.$this->stringify($code)." ?>\n"; + } else { + if ($format == 'html') { + return $dollars.'<?php echo '.$this->htmlchars($code)." ?>\n"; + } + if ($format == 'cdata') { + // quite complex for an "unescaped" section, isn't it? + if ($this->getOutputMode() === PHPTAL::HTML5) { + return $dollars."<?php echo str_replace('</','<\\\\/', ".$this->stringify($code).") ?>\n"; + } elseif ($this->getOutputMode() === PHPTAL::XHTML) { + // both XML and HMTL, because people will inevitably send it as text/html :( + return $dollars."<?php echo strtr(".$this->stringify($code)." ,array(']]>'=>']]]]><![CDATA[>','</'=>'<\\/')) ?>\n"; + } else { + return $dollars."<?php echo str_replace(']]>',']]]]><![CDATA[>', ".$this->stringify($code).") ?>\n"; + } + } + assert(0); + } + } + + /** + * expects PHP code and returns PHP code that will generate escaped string + * Optimizes case when PHP string is given. + * + * @return php code + */ + public function htmlchars($php) + { + // PHP strings can be escaped at compile time + if (preg_match('/^\'((?:[^\'{]+|\\\\.)*)\'$/s', $php, $m)) { + return "'".htmlspecialchars(str_replace('\\\'', "'", $m[1]), ENT_QUOTES, $this->encoding)."'"; + } + return 'phptal_escape('.$php.', \''.$this->encoding.'\')'; + } + + /** + * allow proper printing of any object + * (without escaping - for use with structure keyword) + * + * @return php code + */ + public function stringify($php) + { + // PHP strings don't need to be changed + if (preg_match('/^\'(?>[^\'\\\\]+|\\\\.)*\'$|^\s*"(?>[^"\\\\]+|\\\\.)*"\s*$/s', $php)) { + return $php; + } + return 'phptal_tostring('.$php.')'; + } +} + diff --git a/lib/phptal/PHPTAL/Php/TalesChainExecutor.php b/lib/phptal/PHPTAL/Php/TalesChainExecutor.php new file mode 100644 index 0000000..da94724 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/TalesChainExecutor.php @@ -0,0 +1,96 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * @package PHPTAL + * @subpackage Php + */ +class PHPTAL_Php_TalesChainExecutor +{ + const CHAIN_BREAK = 1; + const CHAIN_CONT = 2; + + public function __construct(PHPTAL_Php_CodeWriter $codewriter, array $chain, PHPTAL_Php_TalesChainReader $reader) + { + $this->_chain = $chain; + $this->_chainStarted = false; + $this->codewriter = $codewriter; + $this->_reader = $reader; + $this->_executeChain(); + } + + public function getCodeWriter() + { + return $this->codewriter; + } + + public function doIf($condition) + { + if ($this->_chainStarted == false) { + $this->_chainStarted = true; + $this->codewriter->doIf($condition); + } else { + $this->codewriter->doElseIf($condition); + } + } + + public function doElse() + { + $this->codewriter->doElse(); + } + + public function breakChain() + { + $this->_state = self::CHAIN_BREAK; + } + + public function continueChain() + { + $this->_state = self::CHAIN_CONT; + } + + private function _executeChain() + { + $this->codewriter->noThrow(true); + + end($this->_chain); $lastkey = key($this->_chain); + + foreach ($this->_chain as $key => $exp) { + $this->_state = 0; + + if ($exp == PHPTAL_Php_TalesInternal::NOTHING_KEYWORD) { + $this->_reader->talesChainNothingKeyword($this); + } elseif ($exp == PHPTAL_Php_TalesInternal::DEFAULT_KEYWORD) { + $this->_reader->talesChainDefaultKeyword($this); + } else { + $this->_reader->talesChainPart($this, $exp, $lastkey === $key); + } + + if ($this->_state == self::CHAIN_BREAK) + break; + if ($this->_state == self::CHAIN_CONT) + continue; + } + + $this->codewriter->doEnd('if'); + $this->codewriter->noThrow(false); + } + + private $_state = 0; + private $_chain; + private $_chainStarted = false; + private $codewriter = null; +} diff --git a/lib/phptal/PHPTAL/Php/TalesChainReader.php b/lib/phptal/PHPTAL/Php/TalesChainReader.php new file mode 100644 index 0000000..4992bfe --- /dev/null +++ b/lib/phptal/PHPTAL/Php/TalesChainReader.php @@ -0,0 +1,25 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * @package PHPTAL + * @subpackage Php + */ +interface PHPTAL_Php_TalesChainReader +{ + public function talesChainNothingKeyword(PHPTAL_Php_TalesChainExecutor $executor); + public function talesChainDefaultKeyword(PHPTAL_Php_TalesChainExecutor $executor); + public function talesChainPart(PHPTAL_Php_TalesChainExecutor $executor, $expression, $islast); +} diff --git a/lib/phptal/PHPTAL/Php/TalesInternal.php b/lib/phptal/PHPTAL/Php/TalesInternal.php new file mode 100644 index 0000000..4e84a66 --- /dev/null +++ b/lib/phptal/PHPTAL/Php/TalesInternal.php @@ -0,0 +1,503 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Moritz Bechler <mbechler@eenterphace.org> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + + +/** + * TALES Specification 1.3 + * + * Expression ::= [type_prefix ':'] String + * type_prefix ::= Name + * + * Examples: + * + * a/b/c + * path:a/b/c + * nothing + * path:nothing + * python: 1 + 2 + * string:Hello, ${username} + * + * + * Builtin Names in Page Templates (for PHPTAL) + * + * * nothing - special singleton value used by TAL to represent a + * non-value (e.g. void, None, Nil, NULL). + * + * * default - special singleton value used by TAL to specify that + * existing text should not be replaced. + * + * * repeat - the repeat variables (see RepeatVariable). + * + * + */ + +/** + * @package PHPTAL + * @subpackage Php + */ +class PHPTAL_Php_TalesInternal implements PHPTAL_Tales +{ + const DEFAULT_KEYWORD = 'new PHPTAL_DefaultKeyword'; + const NOTHING_KEYWORD = 'new PHPTAL_NothingKeyword'; + + static public function true($src, $nothrow) + { + return 'phptal_true(' . self::compileToPHPExpression($src, true) . ')'; + } + + /** + * not: + * + * not: Expression + * + * evaluate the expression string (recursively) as a full expression, + * and returns the boolean negation of its value + * + * return boolean based on the following rules: + * + * 1. integer 0 is false + * 2. integer > 0 is true + * 3. an empty string or other sequence is false + * 4. a non-empty string or other sequence is true + * 5. a non-value (e.g. void, None, Nil, NULL, etc) is false + * 6. all other values are implementation-dependent. + * + * Examples: + * + * not: exists: foo/bar/baz + * not: php: object.hasChildren() + * not: string:${foo} + * not: foo/bar/booleancomparable + */ + static public function not($expression, $nothrow) + { + return '!phptal_true(' . self::compileToPHPExpression($expression, $nothrow) . ')'; + } + + + /** + * path: + * + * PathExpr ::= Path [ '|' Path ]* + * Path ::= variable [ '/' URL_Segment ]* + * variable ::= Name + * + * Examples: + * + * path: username + * path: user/name + * path: object/method/10/method/member + * path: object/${dynamicmembername}/method + * path: maybethis | path: maybethat | path: default + * + * PHPTAL: + * + * 'default' may lead to some 'difficult' attributes implementation + * + * For example, the tal:content will have to insert php code like: + * + * if (isset($ctx->maybethis)) { + * echo $ctx->maybethis; + * } + * elseif (isset($ctx->maybethat) { + * echo $ctx->maybethat; + * } + * else { + * // process default tag content + * } + * + * @returns string or array + */ + static public function path($expression, $nothrow=false) + { + $expression = trim($expression); + if ($expression == 'default') return self::DEFAULT_KEYWORD; + if ($expression == 'nothing') return self::NOTHING_KEYWORD; + if ($expression == '') return self::NOTHING_KEYWORD; + + // split OR expressions terminated by a string + if (preg_match('/^(.*?)\s*\|\s*?(string:.*)$/sm', $expression, $m)) { + list(, $expression, $string) = $m; + } + // split OR expressions terminated by a 'fast' string + elseif (preg_match('/^(.*?)\s*\|\s*\'((?:[^\'\\\\]|\\\\.)*)\'\s*$/sm', $expression, $m)) { + list(, $expression, $string) = $m; + $string = 'string:'.stripslashes($string); + } + + // split OR expressions + $exps = preg_split('/\s*\|\s*/sm', $expression); + + // if (many expressions) or (expressions or terminating string) found then + // generate the array of sub expressions and return it. + if (count($exps) > 1 || isset($string)) { + $result = array(); + foreach ($exps as $i=>$exp) { + if(isset($string) || $i < count($exps) - 1) { + $result[] = self::compileToPHPExpressions(trim($exp), true); + } + else { + // the last expression can thorw exception. + $result[] = self::compileToPHPExpressions(trim($exp), false); + } + } + if (isset($string)) { + $result[] = self::compileToPHPExpressions($string, true); + } + return $result; + } + + + // see if there are subexpressions, but skip interpolated parts, i.e. ${a/b}/c is 2 parts + if (preg_match('/^((?:[^$\/]+|\$\$|\${[^}]+}|\$))\/(.+)$/s', $expression, $m)) + { + if (!self::checkExpressionPart($m[1])) { + throw new PHPTAL_ParserException("Invalid TALES path: '$expression', expected '{$m[1]}' to be variable name"); + } + + $next = self::string($m[1]); + $expression = self::string($m[2]); + } else { + if (!self::checkExpressionPart($expression)) { + throw new PHPTAL_ParserException("Invalid TALES path: '$expression', expected variable name. Complex expressions need php: modifier."); + } + + $next = self::string($expression); + $expression = null; + } + + if ($nothrow) { + return '$ctx->path($ctx, ' . $next . ($expression === null ? '' : '."/".'.$expression) . ', true)'; + } + + if (preg_match('/^\'[a-z][a-z0-9_]*\'$/i', $next)) $next = substr($next, 1, -1); else $next = '{'.$next.'}'; + + // if no sub part for this expression, just optimize the generated code + // and access the $ctx->var + if ($expression === null) { + return '$ctx->'.$next; + } + + // otherwise we have to call PHPTAL_Context::path() to resolve the path at runtime + // extract the first part of the expression (it will be the PHPTAL_Context::path() + // $base and pass the remaining of the path to PHPTAL_Context::path() + return '$ctx->path($ctx->'.$next.', '.$expression.')'; + } + + /** + * check if part of exprssion (/foo/ or /foo${bar}/) is alphanumeric + */ + private static function checkExpressionPart($expression) + { + $expression = preg_replace('/\${[^}]+}/', 'a', $expression); // pretend interpolation is done + return preg_match('/^[a-z_][a-z0-9_]*$/i', $expression); + } + + /** + * string: + * + * string_expression ::= ( plain_string | [ varsub ] )* + * varsub ::= ( '$' Path ) | ( '${' Path '}' ) + * plain_string ::= ( '$$' | non_dollar )* + * non_dollar ::= any character except '$' + * + * Examples: + * + * string:my string + * string:hello, $username how are you + * string:hello, ${user/name} + * string:you have $$130 in your bank account + */ + static public function string($expression, $nothrow=false) + { + return self::parseString($expression, $nothrow, ''); + } + + /** + * @param string $tales_prefix prefix added to all TALES in the string + */ + static public function parseString($expression, $nothrow, $tales_prefix) + { + // This is a simple parser which evaluates ${foo} inside + // 'string:foo ${foo} bar' expressions, it returns the php code which will + // print the string with correct interpollations. + // Nothing special there :) + + $inPath = false; + $inAccoladePath = false; + $lastWasDollar = false; + $result = ''; + $len = strlen($expression); + for ($i=0; $i<$len; $i++) { + $c = $expression[$i]; + switch ($c) { + case '$': + if ($lastWasDollar) { + $lastWasDollar = false; + } elseif ($inAccoladePath) { + $subPath .= $c; + $c = ''; + } else { + $lastWasDollar = true; + $c = ''; + } + break; + + case '\\': + if ($inAccoladePath) { + $subPath .= $c; + $c = ''; + } + else { + $c = '\\\\'; + } + break; + + case '\'': + if ($inAccoladePath) { + $subPath .= $c; + $c = ''; + } + else { + $c = '\\\''; + } + break; + + case '{': + if ($inAccoladePath) { + $subPath .= $c; + $c = ''; + } elseif ($lastWasDollar) { + $lastWasDollar = false; + $inAccoladePath = true; + $subPath = ''; + $c = ''; + } + break; + + case '}': + if ($inAccoladePath) { + $inAccoladePath = false; + $subEval = self::compileToPHPExpression($tales_prefix.$subPath,false); + $result .= "'.(" . $subEval . ").'"; + $subPath = ''; + $lastWasDollar = false; + $c = ''; + } + break; + + default: + if ($lastWasDollar) { + $lastWasDollar = false; + $inPath = true; + $subPath = $c; + $c = ''; + } elseif ($inAccoladePath) { + $subPath .= $c; + $c = ''; + } elseif ($inPath) { + $t = strtolower($c); + if (($t >= 'a' && $t <= 'z') || ($t >= '0' && $t <= '9') || ($t == '_')) { + $subPath .= $c; + $c = ''; + } else { + $inPath = false; + $subEval = self::compileToPHPExpression($tales_prefix.$subPath,false); + $result .= "'.(" . $subEval . ").'"; + } + } + break; + } + $result .= $c; + } + if ($inPath) { + $subEval = self::compileToPHPExpression($tales_prefix.$subPath, false); + $result .= "'.(" . $subEval . ").'"; + } + + // optimize ''.foo.'' to foo + $result = preg_replace("/^(?:''\.)?(.*?)(?:\.'')?$/", '\1', '\''.$result.'\''); + + /* + The following expression (with + in first alternative): + "/^\(((?:[^\(\)]+|\([^\(\)]*\))*)\)$/" + + did work properly for (aaaaaaa)aa, but not for (aaaaaaaaaaaaaaaaaaaaa)aa + WTF!? + */ + + // optimize (foo()) to foo() + $result = preg_replace("/^\(((?:[^\(\)]|\([^\(\)]*\))*)\)$/", '\1', $result); + + return $result; + } + + /** + * php: modifier. + * + * Transform the expression into a regular PHP expression. + */ + static public function php($src) + { + return PHPTAL_Php_Transformer::transform($src, '$ctx->'); + } + + /** + * phptal-internal-php-block: modifier for emulation of <?php ?> in attributes. + * + * Please don't use it in the templates! + */ + static public function phptal_internal_php_block($src) + { + $src = rawurldecode($src); + + // Simple echo can be supported via regular method + if (preg_match('/^\s*echo\s+((?:[^;]+|"[^"\\\\]*"|\'[^\'\\\\]*\'|\/\*.*?\*\/)+);*\s*$/s',$src,$m)) + { + return $m[1]; + } + + // <?php block expects statements, but modifiers must return expressions. + // unfortunately this ugliness is the only way to support it currently. + // ? > keeps semicolon optional + return "eval(".self::string($src.'?>').")"; + } + + /** + * exists: modifier. + * + * Returns the code required to invoke Context::exists() on specified path. + */ + static public function exists($src, $nothrow) + { + $src = trim($src); + if (ctype_alnum($src)) return 'isset($ctx->'.$src.')'; + return '(null !== ' . self::compileToPHPExpression($src, true) . ')'; + } + + /** + * number: modifier. + * + * Returns the number as is. + */ + static public function number($src, $nothrow) + { + if (!is_numeric(trim($src))) throw new PHPTAL_ParserException("'$src' is not a number"); + return trim($src); + } + + /** + * json: modifier. Serializes anything as JSON. + */ + static public function json($src, $nothrow) + { + return 'json_encode('.phptal_tale($src,$nothrow).')'; + } + + /** + * urlencode: modifier. Escapes a string. + */ + static public function urlencode($src, $nothrow) + { + return 'rawurlencode('.phptal_tale($src,$nothrow).')'; + } + + /** + * translates TALES expression with alternatives into single PHP expression. + * Identical to compileToPHPExpressions() for singular expressions. + * + * @see PHPTAL_Php_TalesInternal::compileToPHPExpressions() + * @return string + */ + public static function compileToPHPExpression($expression, $nothrow=false) + { + $r = self::compileToPHPExpressions($expression, $nothrow); + if (!is_array($r)) return $r; + + // this weird ternary operator construct is to execute noThrow inside the expression + return '($ctx->noThrow(true)||1?'.self::convertExpressionsToExpression($r, $nothrow).':"")'; + } + + /* + * helper function for compileToPHPExpression + * @access private + */ + private static function convertExpressionsToExpression(array $array, $nothrow) + { + if (count($array)==1) return '($ctx->noThrow('.($nothrow?'true':'false').')||1?('. + ($array[0]==self::NOTHING_KEYWORD?'null':$array[0]). + '):"")'; + + $expr = array_shift($array); + + return "(!phptal_isempty(\$_tmp5=$expr) && (\$ctx->noThrow(false)||1)?\$_tmp5:".self::convertExpressionsToExpression($array, $nothrow).')'; + } + + /** + * returns PHP code that will evaluate given TALES expression. + * e.g. "string:foo${bar}" may be transformed to "'foo'.phptal_escape($ctx->bar)" + * + * Expressions with alternatives ("foo | bar") will cause it to return array + * Use PHPTAL_Php_TalesInternal::compileToPHPExpression() if you always want string. + * + * @param bool $nothrow if true, invalid expression will return NULL (at run time) rather than throwing exception + * + * @return string or array + */ + public static function compileToPHPExpressions($expression, $nothrow=false) + { + $expression = trim($expression); + + // Look for tales modifier (string:, exists:, Namespaced\Tale:, etc...) + if (preg_match('/^([a-z](?:[a-z0-9._\\\\-]*[a-z0-9])?):(.*)$/si', $expression, $m)) { + list(, $typePrefix, $expression) = $m; + } + // may be a 'string' + elseif (preg_match('/^\'((?:[^\']|\\\\.)*)\'$/s', $expression, $m)) { + $expression = stripslashes($m[1]); + $typePrefix = 'string'; + } + // failback to path: + else { + $typePrefix = 'path'; + } + + // is a registered TALES expression modifier + $callback = PHPTAL_TalesRegistry::getInstance()->getCallback($typePrefix); + if ($callback !== NULL) + { + $result = call_user_func($callback, $expression, $nothrow); + self::verifyPHPExpressions($typePrefix, $result); + return $result; + } + + $func = 'phptal_tales_'.str_replace('-', '_', $typePrefix); + throw new PHPTAL_UnknownModifierException("Unknown phptal modifier '$typePrefix'. Function '$func' does not exist", $typePrefix); + } + + private static function verifyPHPExpressions($typePrefix,$expressions) + { + if (!is_array($expressions)) { + $expressions = array($expressions); + } + + foreach($expressions as $expr) { + if (preg_match('/;\s*$/', $expr)) { + throw new PHPTAL_ParserException("Modifier $typePrefix generated PHP statement rather than expression (don't add semicolons)"); + } + } + } +} diff --git a/lib/phptal/PHPTAL/Php/Transformer.php b/lib/phptal/PHPTAL/Php/Transformer.php new file mode 100644 index 0000000..c07608d --- /dev/null +++ b/lib/phptal/PHPTAL/Php/Transformer.php @@ -0,0 +1,418 @@ +<?php +/** + * PHPTAL templating engine + * + * PHP Version 5 + * + * @category HTML + * @package PHPTAL + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + * @author Kornel Lesiński <kornel@aardvarkmedia.co.uk> + * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License + * @version SVN: $Id$ + * @link http://phptal.org/ + */ + +/** + * Tranform php: expressions into their php equivalent. + * + * This transformer produce php code for expressions like : + * + * - a.b["key"].c().someVar[10].foo() + * - (a or b) and (c or d) + * - not myBool + * - ... + * + * The $prefix variable may be changed to change the context lookup. + * + * example: + * + * $res = PHPTAL_Php_Transformer::transform('a.b.c[x]', '$ctx->'); + * $res == '$ctx->a->b->c[$ctx->x]'; + * + * @package PHPTAL + * @subpackage Php + * @author Laurent Bedubourg <lbedubourg@motion-twin.com> + */ +class PHPTAL_Php_Transformer +{ + const ST_WHITE = -1; // start of string or whitespace + const ST_NONE = 0; // pass through (operators, parens, etc.) + const ST_STR = 1; // 'foo' + const ST_ESTR = 2; // "foo ${x} bar" + const ST_VAR = 3; // abcd + const ST_NUM = 4; // 123.02 + const ST_EVAL = 5; // $somevar + const ST_MEMBER = 6; // abcd.x + const ST_STATIC = 7; // class::[$]static|const + const ST_DEFINE = 8; // @MY_DEFINE + + /** + * transform PHPTAL's php-like syntax into real PHP + */ + public static function transform($str, $prefix='$') + { + $len = strlen($str); + $state = self::ST_WHITE; + $result = ''; + $i = 0; + $inString = false; + $backslashed = false; + $instanceof = false; + $eval = false; + + + for ($i = 0; $i <= $len; $i++) { + if ($i == $len) $c = "\0"; + else $c = $str[$i]; + + switch ($state) { + + // after whitespace a variable-variable may start, ${var} → $ctx->{$ctx->var} + case self::ST_WHITE: + if ($c === '$' && $i+1 < $len && $str[$i+1] === '{') + { + $result .= $prefix; + $state = self::ST_NONE; + continue; + } + /* NO BREAK - ST_WHITE is almost the same as ST_NONE */ + + // no specific state defined, just eat char and see what to do with it. + case self::ST_NONE: + // begin of eval without { + if ($c === '$' && $i+1 < $len && self::isAlpha($str[$i+1])) { + $state = self::ST_EVAL; + $mark = $i+1; + $result .= $prefix.'{'; + } + elseif (self::isDigit($c)) + { + $state = self::ST_NUM; + $mark = $i; + } + // that an alphabetic char, then it should be the begining + // of a var or static + // && !self::isDigit($c) checked earlier + elseif (self::isVarNameChar($c)) { + $state = self::ST_VAR; + $mark = $i; + } + // begining of double quoted string + elseif ($c === '"') { + $state = self::ST_ESTR; + $mark = $i; + $inString = true; + } + // begining of single quoted string + elseif ($c === '\'') { + $state = self::ST_STR; + $mark = $i; + $inString = true; + } + // closing a method, an array access or an evaluation + elseif ($c === ')' || $c === ']' || $c === '}') { + $result .= $c; + // if next char is dot then an object member must + // follow + if ($i+1 < $len && $str[$i+1] === '.') { + $result .= '->'; + $state = self::ST_MEMBER; + $mark = $i+2; + $i+=2; + } + } + // @ is an access to some defined variable + elseif ($c === '@') { + $state = self::ST_DEFINE; + $mark = $i+1; + } + elseif (ctype_space($c)) { + $state = self::ST_WHITE; + $result .= $c; + } + // character we don't mind about + else { + $result .= $c; + } + break; + + // $xxx + case self::ST_EVAL: + if (!self::isVarNameChar($c)) { + $result .= $prefix . substr($str, $mark, $i-$mark); + $result .= '}'; + $state = self::ST_NONE; + } + break; + + // single quoted string + case self::ST_STR: + if ($c === '\\') { + $backslashed = true; + } elseif ($backslashed) { + $backslashed = false; + } + // end of string, back to none state + elseif ($c === '\'') { + $result .= substr($str, $mark, $i-$mark+1); + $inString = false; + $state = self::ST_NONE; + } + break; + + // double quoted string + case self::ST_ESTR: + if ($c === '\\') { + $backslashed = true; + } elseif ($backslashed) { + $backslashed = false; + } + // end of string, back to none state + elseif ($c === '"') { + $result .= substr($str, $mark, $i-$mark+1); + $inString = false; + $state = self::ST_NONE; + } + // instring interpolation, search } and transform the + // interpollation to insert it into the string + elseif ($c === '$' && $i+1 < $len && $str[$i+1] === '{') { + $result .= substr($str, $mark, $i-$mark) . '{'; + + $sub = 0; + for ($j = $i; $j<$len; $j++) { + if ($str[$j] === '{') { + $sub++; + } elseif ($str[$j] === '}' && (--$sub) == 0) { + $part = substr($str, $i+2, $j-$i-2); + $result .= self::transform($part, $prefix); + $i = $j; + $mark = $i; + } + } + } + break; + + // var state + case self::ST_VAR: + if (self::isVarNameChar($c)) { + } + // end of var, begin of member (method or var) + elseif ($c === '.') { + $result .= $prefix . substr($str, $mark, $i-$mark); + $result .= '->'; + $state = self::ST_MEMBER; + $mark = $i+1; + } + // static call, the var is a class name + elseif ($c === ':' && $i+1 < $len && $str[$i+1] === ':') { + $result .= substr($str, $mark, $i-$mark+1); + $mark = $i+1; + $i++; + $state = self::ST_STATIC; + break; + } + // function invocation, the var is a function name + elseif ($c === '(') { + $result .= substr($str, $mark, $i-$mark+1); + $state = self::ST_NONE; + } + // array index, the var is done + elseif ($c === '[') { + if ($str[$mark]==='_') { // superglobal? + $result .= '$' . substr($str, $mark, $i-$mark+1); + } else { + $result .= $prefix . substr($str, $mark, $i-$mark+1); + } + $state = self::ST_NONE; + } + // end of var with non-var-name character, handle keywords + // and populate the var name + else { + $var = substr($str, $mark, $i-$mark); + $low = strtolower($var); + // boolean and null + if ($low === 'true' || $low === 'false' || $low === 'null') { + $result .= $var; + } + // lt, gt, ge, eq, ... + elseif (array_key_exists($low, self::$TranslationTable)) { + $result .= self::$TranslationTable[$low]; + } + // instanceof keyword + elseif ($low === 'instanceof') { + $result .= $var; + $instanceof = true; + } + // previous was instanceof + elseif ($instanceof) { + // last was instanceof, this var is a class name + $result .= $var; + $instanceof = false; + } + // regular variable + else { + $result .= $prefix . $var; + } + $i--; + $state = self::ST_NONE; + } + break; + + // object member + case self::ST_MEMBER: + if (self::isVarNameChar($c)) { + } + // eval mode ${foo} + elseif ($c === '$' && ($i >= $len-2 || $str[$i+1] !== '{')) { + $result .= '{' . $prefix; + $mark++; + $eval = true; + } + // x.${foo} x->{foo} + elseif ($c === '$') { + $mark++; + } + // end of var member var, begin of new member + elseif ($c === '.') { + $result .= substr($str, $mark, $i-$mark); + if ($eval) { $result .='}'; $eval = false; } + $result .= '->'; + $mark = $i+1; + $state = self::ST_MEMBER; + } + // begin of static access + elseif ($c === ':') { + $result .= substr($str, $mark, $i-$mark+1); + if ($eval) { $result .='}'; $eval = false; } + $state = self::ST_STATIC; + break; + } + // the member is a method or an array + elseif ($c === '(' || $c === '[') { + $result .= substr($str, $mark, $i-$mark+1); + if ($eval) { $result .='}'; $eval = false; } + $state = self::ST_NONE; + } + // regular end of member, it is a var + else { + $var = substr($str, $mark, $i-$mark); + if ($var !== '' && !preg_match('/^[a-z][a-z0-9_\x7f-\xff]*$/i',$var)) { + throw new PHPTAL_ParserException("Invalid field name '$var' in expression php:$str"); + } + $result .= $var; + if ($eval) { $result .='}'; $eval = false; } + $state = self::ST_NONE; + $i--; + } + break; + + // wait for separator + case self::ST_DEFINE: + if (self::isVarNameChar($c)) { + } else { + $state = self::ST_NONE; + $result .= substr($str, $mark, $i-$mark); + $i--; + } + break; + + // static call, can be const, static var, static method + // Klass::$static + // Klass::const + // Kclass::staticMethod() + // + case self::ST_STATIC: + if (self::isVarNameChar($c)) { + } + // static var + elseif ($c === '$') { + } + // end of static var which is an object and begin of member + elseif ($c === '.') { + $result .= substr($str, $mark, $i-$mark); + $result .= '->'; + $mark = $i+1; + $state = self::ST_MEMBER; + } + // end of static var which is a class name + elseif ($c === ':') { + $result .= substr($str, $mark, $i-$mark+1); + $state = self::ST_STATIC; + break; + } + // static method or array + elseif ($c === '(' || $c === '[') { + $result .= substr($str, $mark, $i-$mark+1); + $state = self::ST_NONE; + } + // end of static var or const + else { + $result .= substr($str, $mark, $i-$mark); + $state = self::ST_NONE; + $i--; + } + break; + + // numeric value + case self::ST_NUM: + if (!self::isDigitCompound($c)) { + $var = substr($str, $mark, $i-$mark); + + if (self::isAlpha($c) || $c === '_') { + throw new PHPTAL_ParserException("Syntax error in number '$var$c' in expression php:$str"); + } + if (!is_numeric($var)) { + throw new PHPTAL_ParserException("Syntax error in number '$var' in expression php:$str"); + } + + $result .= $var; + $state = self::ST_NONE; + $i--; + } + break; + } + } + + $result = trim($result); + + // CodeWriter doesn't like expressions that look like blocks + if ($result[strlen($result)-1] === '}') return '('.$result.')'; + + return $result; + } + + private static function isAlpha($c) + { + $c = strtolower($c); + return $c >= 'a' && $c <= 'z'; + } + + private static function isDigit($c) + { + return ($c >= '0' && $c <= '9'); + } + + private static function isDigitCompound($c) + { + return ($c >= '0' && $c <= '9' || $c === '.'); + } + + private static function isVarNameChar($c) + { + return self::isAlpha($c) || ($c >= '0' && $c <= '9') || $c === '_' || $c === '\\'; + } + + private static $TranslationTable = array( + 'not' => '!', + 'ne' => '!=', + 'and' => '&&', + 'or' => '||', + 'lt' => '<', + 'gt' => '>', + 'ge' => '>=', + 'le' => '<=', + 'eq' => '==', + ); +} + |