/** * This is a singleton * * @return PHPTAL_TalesRegistry */ public static function getInstance() { if (!self::$instance) { self::$instance = new PHPTAL_TalesRegistry(); } return self::$instance; }
function phptal_tales($expression, $nothrow = false) { $expression = trim($expression); // Look for tales modifier (string:, exists:, etc...) //if (preg_match('/^([-a-z]+):(.*?)$/', $expression, $m)) { if (preg_match('/^([a-z][-.a-z]*[a-z]):(.*?)$/i', $expression, $m)) { list(, $typePrefix, $expression) = $m; } else { if (preg_match('/^\'((?:[^\']|\\\\.)*)\'$/', $expression, $m)) { $expression = stripslashes($m[1]); $typePrefix = 'string'; } else { $typePrefix = 'path'; } } // is a registered TALES expression modifier if (PHPTAL_TalesRegistry::getInstance()->isRegistered($typePrefix)) { $callback = PHPTAL_TalesRegistry::getInstance()->getCallback($typePrefix); return call_user_func($callback, $expression, $nothrow); } // class method if (strpos($typePrefix, '.')) { $classCallback = explode('.', $typePrefix, 2); $callbackName = NULL; if (!is_callable($classCallback, FALSE, $callbackName)) { $err = 'Unknown phptal modifier %s function %s does not exists or is not statically callable.'; $err = sprintf($err, $typePrefix, $callbackName); throw new PHPTAL_Exception($err); } $ref = new ReflectionClass($classCallback[0]); if (!$ref->implementsInterface('PHPTAL_Tales')) { $err = 'Unable to use phptal modifier %s as the class %s does not implement the PHPTAL_Tales interface.'; $err = sprintf($err, $typePrefix, $callbackName); throw new PHPTAL_Exception($err); } return call_user_func($classCallback, $expression, $nothrow); } // check if it is implemented via code-generating function $func = 'phptal_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($func)) { return $func($expression, $nothrow); } // check if it is implemented via runtime function $runfunc = 'phptal_runtime_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($runfunc)) { return "{$runfunc}(" . phptal_tale($expression, $nothrow) . ")"; } throw new PHPTAL_Exception("Unknown phptal modifier '{$typePrefix}'. Function '{$func}' does not exist"); }
public static function registerInternalTales() { static $registered = false; if ($registered) { return; } $registry = PHPTAL_TalesRegistry::getInstance(); $registry->registerPrefix('not', array(__CLASS__, 'not')); $registry->registerPrefix('path', array(__CLASS__, 'path')); $registry->registerPrefix('string', array(__CLASS__, 'string')); $registry->registerPrefix('php', array(__CLASS__, 'php')); $registry->registerPrefix('exists', array(__CLASS__, 'exists')); $registry->registerPrefix('number', array(__CLASS__, 'number')); $registry->registerPrefix('true', array(__CLASS__, 'true')); $registered = true; }
/** * 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 compileToPHPStatements($expression, $nothrow = false) { $expression = trim($expression); // Look for tales modifier (string:, exists:, etc...) if (preg_match('/^([a-z][a-z0-9._-]*[a-z0-9]):(.*)$/si', $expression, $m)) { list(, $typePrefix, $expression) = $m; } elseif (preg_match('/^\'((?:[^\']|\\\\.)*)\'$/s', $expression, $m)) { $expression = stripslashes($m[1]); $typePrefix = 'string'; } else { $typePrefix = 'path'; } // is a registered TALES expression modifier if (PHPTAL_TalesRegistry::getInstance()->isRegistered($typePrefix)) { $callback = PHPTAL_TalesRegistry::getInstance()->getCallback($typePrefix); return call_user_func($callback, $expression, $nothrow); } // class method if (strpos($typePrefix, '.')) { $classCallback = explode('.', $typePrefix, 2); $callbackName = null; if (!is_callable($classCallback, FALSE, $callbackName)) { throw new PHPTAL_UnknownModifierException("Unknown phptal modifier {$typePrefix}. Function {$callbackName} does not exists or is not statically callable", $typePrefix); } $ref = new ReflectionClass($classCallback[0]); if (!$ref->implementsInterface('PHPTAL_Tales')) { throw new PHPTAL_UnknownModifierException("Unable to use phptal modifier {$typePrefix} as the class {$callbackName} does not implement the PHPTAL_Tales interface", $typePrefix); } return call_user_func($classCallback, $expression, $nothrow); } // check if it is implemented via code-generating function $func = 'phptal_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($func)) { return $func($expression, $nothrow); } // check if it is implemented via runtime function $runfunc = 'phptal_runtime_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($runfunc)) { return "{$runfunc}(" . self::compileToPHPExpression($expression, $nothrow) . ")"; } throw new PHPTAL_UnknownModifierException("Unknown phptal modifier '{$typePrefix}'. Function '{$func}' does not exist", $typePrefix); }
protected static function getPHPExpressionsForModifier($typePrefix, $expression, $nothrow) { // is a registered TALES expression modifier if (PHPTAL_TalesRegistry::getInstance()->isRegistered($typePrefix)) { $callback = PHPTAL_TalesRegistry::getInstance()->getCallback($typePrefix); return call_user_func($callback, $expression, $nothrow); } // class method if (strpos($typePrefix, '.')) { $classCallback = explode('.', $typePrefix, 2); $callbackName = null; if (!is_callable($classCallback, false, $callbackName)) { throw new PHPTAL_UnknownModifierException("Unknown phptal modifier {$typePrefix}. Function {$callbackName} does not exists or is not statically callable", $typePrefix); } $ref = new ReflectionClass($classCallback[0]); if (!$ref->implementsInterface('PHPTAL_Tales')) { throw new PHPTAL_UnknownModifierException("Unable to use phptal modifier {$typePrefix} as the class {$callbackName} does not implement the PHPTAL_Tales interface", $typePrefix); } return call_user_func($classCallback, $expression, $nothrow); } // check if it is implemented via code-generating function $func = 'phptal_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($func)) { return $func($expression, $nothrow); } // The following code is automatically modified in version for PHP 5.3 $func = 'PHPTALNAMESPACE\\phptal_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($func)) { return $func($expression, $nothrow); } // check if it is implemented via runtime function $runfunc = 'phptal_runtime_tales_' . str_replace('-', '_', $typePrefix); if (function_exists($runfunc)) { return "{$runfunc}(" . self::compileToPHPExpression($expression, $nothrow) . ")"; } throw new PHPTAL_UnknownModifierException("Unknown phptal modifier '{$typePrefix}'. Function '{$func}' does not exist", $typePrefix); }
/** * 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:, etc...) if (preg_match('/^([a-z](?:[a-z0-9._-]*[a-z0-9])?):(.*)$/si', $expression, $m)) { list(, $typePrefix, $expression) = $m; } elseif (preg_match('/^\'((?:[^\']|\\\\.)*)\'$/s', $expression, $m)) { $expression = stripslashes($m[1]); $typePrefix = 'string'; } 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); }
function testCanRegisterFallbackOverRegistered() { PHPTAL_TalesRegistry::getInstance()->registerPrefix('registry_test_6', 'registry_test_callback2'); PHPTAL_TalesRegistry::getInstance()->registerPrefix('registry_test_6', 'registry_test_callback', true); }
return "!phptal_isempty(" . phptal_tale($src, true) . ")"; } public static function _empty($src, $nothrow) { return "phptal_isempty(" . phptal_tale($src, true) . ")"; } public static function cssodd($src, $nothrow) { return '(' . phptal_tale($src, $nothrow) . ' ? "odd" : "even" )'; } public static function nozeroFUNC($val) { if ((int) $val === 0) { return null; } return $val; } public static function nozero($src, $nothrow) { $exp = Template::SplitSRC($src, $nothrow); array_unshift($exp, 'Template_Tales::nozeroFUNC(' . phptal_tale($src, $nothrow) . ')'); return $exp; } } PHPTAL_TalesRegistry::getInstance()->registerPrefix("limit", array('Template_Tales', 'limit')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("empty", array('Template_Tales', '_empty')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("notempty", array('Template_Tales', 'notempty')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("count", array('Template_Tales', 'count')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("cssodd", array('Template_Tales', 'cssodd')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("nozero", array('Template_Tales', 'nozero'));
} public static function cssodd($src, $nothrow) { return '(' . phptal_tale($src, $nothrow) . ' ? "odd" : "even" )'; } public static function nozeroFUNC($val) { if ((int) $val === 0) { return null; } return $val; } public static function nozero($src, $nothrow) { $exp = Template::SplitSRC($src, $nothrow); array_unshift($exp, 'Template_Tales::nozeroFUNC(' . phptal_tale($src, $nothrow) . ')'); return $exp; } public static function json($src, $nothrow) { return 'json_encode(' . phptal_tale($src, $nothrow) . ')'; } } PHPTAL_TalesRegistry::getInstance()->registerPrefix("limit", array('Template_Tales', 'limit')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("empty", array('Template_Tales', '_empty')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("notempty", array('Template_Tales', 'notempty')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("count", array('Template_Tales', 'count')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("cssodd", array('Template_Tales', 'cssodd')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("nozero", array('Template_Tales', 'nozero')); PHPTAL_TalesRegistry::getInstance()->registerPrefix("json", array('Template_Tales', 'json'));
public static function initialize() { self::$instance = new PHPTAL_TalesRegistry(); }