// require_once PHPTAL_DIR.'PHPTAL/Tales.php'; /** * Global registry of TALES expression modifiers * */ class PHPTAL_TalesRegistry { static $instance; static public function initialize() { self::$instance = new PHPTAL_TalesRegistry(); } /** * Enter description here... * * @return PHPTAL_TalesRegistry */ static public function getInstance() { if(!(self::$instance instanceof PHPTAL_TalesRegistry)) { self::initialize(); } return self::$instance; } protected function __construct() { } /** * * Expects an either a function name or an array of class and method as * callback. * * @param unknown_type $prefix * @param unknown_type $callback */ public function registerPrefix($prefix, $callback) { if($this->isRegistered($prefix)) { throw new PHPTAL_Exception(sprintf('Expression modifier "%s" is already registered.',$prefix)); } // Check if valid callback if(is_array($callback)) { $class = new ReflectionClass($callback[0]); if(!$class->isSubclassOf('PHPTAL_Tales')) { throw new PHPTAL_Exception('The class you want to register does not implement "PHPTAL_Tales".'); } $method = new ReflectionMethod($callback[0], $callback[1]); if(!$method->isStatic()) { throw new PHPTAL_Exception('The method you want to register is not static.'); } // maybe we want to check the parameters the method takes } else { if(!function_exists($callback)) { throw new PHPTAL_Exception('The function you are trying to register does not exist.'); } } $this->_callbacks[$prefix] = $callback; } public function isRegistered($prefix) { return (array_key_exists($prefix, $this->_callbacks)); } public function getCallback($prefix) { if(!$this->isRegistered($prefix)) { throw new PHPTAL_Exception(sprintf('Expression modifier "%s" is not registered.', $prefix)); } return $this->_callbacks[$prefix]; } private $_callbacks = array(); } ?>