cvs: pear /Template Template.php

From: Date: Wed, 16 Jun 2004 15:31:49 +0000
Subject: cvs: pear /Template Template.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20736@lists.php.net to get a copy of this message
alan_k Wed Jun 16 11:31:49 2004 EDT Modified files: /pear/Template Template.php Log: some of the tidy ups and ideas from philippe

http://cvs.php.net/diff.php/pear/Template/Template.php?r1=1.10&r2=1.11&ty=u Index: pear/Template/Template.php diff -u pear/Template/Template.php:1.10 pear/Template/Template.php:1.11 --- pear/Template/Template.php:1.10 Wed Jun 16 11:08:39 2004 +++ pear/Template/Template.php Wed Jun 16 11:31:49 2004 @@ -1,31 +1,47 @@ <?php /** -* Error code: syntax error in template. -*/ -define('TEMPLATE_ERROR_SYNTAX', -1); + * Error code: Debug info + */ +define('TEMPLATE_ERROR_DEBUG', 0); /** -* Error code: bad arguments to methods. -*/ -define('TEMPLATE_ERROR_INVALIDARGS', -2); + * Error code: syntax error in template. + */ +define('TEMPLATE_ERROR_SYNTAX', -1); /** -* Error code: file access problem -*/ -define('TEMPLATE_ERROR_FILE', -3); + * Error code: bad arguments to methods. + */ +define('TEMPLATE_ERROR_INVALIDARGS', -2); /** -* Error code: RETURN ERRORS -*/ -define('TEMPLATE_ERROR_RETURN', 1); + * Error code: file access problem + */ +define('TEMPLATE_ERROR_FILE', -3); /** -* Error code: FATAL DEATH -*/ -define('TEMPLATE_ERROR_DIE', 8); + * Error code: Provider not available + */ +define('TEMPLATE_ERROR_PROVIDER', -4); + +/** + * Error code: Missing implementation + */ +define('TEMPLATE_ERROR_NOTIMPLEMENTED', -5); + +/** + * Error code: RETURN ERRORS + */ +define('TEMPLATE_ERROR_RETURN', 1); + +/** + * Error code: FATAL DEATH + */ +define('TEMPLATE_ERROR_DIE', 8); + /** * * This class implements a "universal" template provider for PEAR. @@ -33,7 +49,7 @@ * See example usage and in-progress documentation at {@link * http://wiki.ciaweb.net/yawiki/index.php?area=Template} * -* $Id: Template.php,v 1.10 2004/06/16 15:08:39 alan_k Exp $ +* $Id: Template.php,v 1.11 2004/06/16 15:31:49 alan_k Exp $ * * @package Template * @@ -49,45 +65,49 @@ /** - * - * The options for initializing the template class. - * - * The options available in the base implementation are: - * - * 'template_dir' => string|array, where are your templates (can be - * an array, or a list seperatated by the system PATH_SEPARATOR) - * - * 'compiler' => string, which compiler to use (e.g., 'Flexy', - * 'Regex', '' (Raw), Xipe, Smarty, WACT, Block (ITX/Sigma/phplib) - * - * 'debug' => boolean, whether or not to print debug messages - * - * 'output.strict' => boolean, whether or not sll elements in the - * template must be defined - makes php E_NOTICE warnings appear when - * outputing template. - * - * 'errors.behaviour' => (note the British spelling!) a - * TEMPLATE_ERROR_* constant. - * - * 'plugins' => load classes to be made available via the plugin - * method eg. = array('Savant') - loads the Savant methods. = - * array('MyClass_Plugins' => 'MyClass/Plugins.php') Class, and where - * to include it from.. - * - * @access public - * - * @var array - * - */ - - var $options = array( - 'template_dir' => '', - 'compiler' => '', + * The options for initializing the template class. + * + * The options available in the base implementation are: + * + * 'template.dir' : string|array, where are your templates (can be + * an array, or a list seperatated by the system + * PATH_SEPARATOR) + * + * 'compiler' : string, which compiler to use (e.g., 'Flexy', + * 'Regex', '' (Raw), Xipe, Smarty, WACT, + * 'Block' (ITX/Sigma/phplib) + * + * + * 'debug.level' : integer. Verbose level of debug (0 = no debug, + * 1 = some, ...) + * + * 'output.strict' : boolean, whether or not all elements in the + * template must be defined - makes php E_NOTICE warnings + * appear when outputing template. + * + * 'errors.behaviour' : (note the British spelling!) a TEMPLATE_ERROR_* + * constant. + * + * 'plugins' : load classes to be made available via the plugin + * method eg. = array('Savant') - loads the Savant + * methods. = array('MyClass_Plugins' => + * 'MyClass/Plugins.php') Class, and where to include + * it from.. + * + * @access public + * @var array + **/ + + + var $options = array( + 'template.dir' => '', + 'compiler' => '', 'debug' => false, + 'debug.level' => 1, 'output.strict' => false, 'errors.behaviour' => TEMPLATE_ERROR_DIE, - 'plugins' => array(), - ); + 'plugins' => array() + ); /** @@ -160,12 +180,13 @@ * */ - var $provider = array( - 'Assign' => null, // assign API - 'Plugin' => null, // plugins - 'Block' => null, // IT type templates - 'Multisource' => null // where is the file? - ); + var $provider = array( + 'Assign' => null, // assign API + 'Plugin' => null, // plugins + 'Block' => null, // IT type templates + 'Source' => null, // template source locater and loader? + 'Compiler' => null // To compile/parse templates + ); /** @@ -174,11 +195,12 @@ * * Initializes the Template engine, and sets up behaviour based on * the provided options. + * this doesnt handle pass by ref setting of things..! * * @access public * - * @param array $options An array of options to pass to the Template - * object; see the $options property. + * @param array $options An array of options to pass to the Template object; + * @see the $options property. * */ @@ -198,52 +220,64 @@ /** * * Open and optionally parse or compile a template. - * + * + * TODO : Possible alternatives : use / useTemplate + * * @access public * - * @param string $file The file path of the template source, relative - * to the 'template_dir' option set in the constructor. + * @param mixed $file The file path of the template source, relative + * to the 'template_dir' option set in the constructor. + * use array('string'=>$template_string) or $filepointer + * for other types of source. * * @return mixed Boolean true on success (or string, if * 'compiler.toString' is set), or PEAR_Error on failure. * */ - function open( $file ) + function open( $location ) { - if (!$file) { + if (!$location) { return $this->raiseError('Template::compile no file selected', TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE); } + // should we have a check before we do this? + // clear the files + + $x = new Template; + $this->files = $x->files; // TODO: accept other input: // eg. file handle is_resource(), or string is_string() : // probably want to add another provider for this... - // If we are using any of the fancy path stuff. - let the multisource provider work + // If we are using any of the fancy path stuff or file pointers etc.. - let the Source provider work // out where the template is.. - if (($this->options['locale'] !== false) || count($this->options['template_dir']) > 1) { - $this->assertProvider('Multisource'); - if (true !== ($r = $this->provider['Multisource']->find($file))) { + if (($this->options['locale'] !== false) || + (count($this->options['template_dir']) > 1) || + !is_string($location)) + { + $this->assertProvider('Source'); + if (true !== ($r = $this->provider['Multisource']->open($location))) { return $r; } } else { $this->files['current_template'] = - $this->options['template_dir'][0] . PATH_SEPERATOR . $file; - + $this->options['template_dir'][0] . PATH_SEPERATOR . $location; + $this->files['requested_template'] = $location if (!file_exists($this->files['current_template'])) { // cant find the template!!! return $this->raiseError( - "Could not find Template %s in any of the configured path %s" . + "Could not find Template %s in the configured path %s" . implode(", ",$this->options['template_dir']) , TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE, - array($file, $this->options['template_dir'][0]); + array($location, $this->options['template_dir'][0]); } } @@ -349,7 +383,7 @@ extract($this->data['variables']); - foreach(array_keys($this->data['byref']) as $_k) { + foreach (array_keys($this->data['byref']) as $_k) { $$_k = &$this->$this->data['byref'][$_k]; } } @@ -388,7 +422,7 @@ } ob_start(); - if (true !== $this->output()) { + if (true !== ($r = $this->output())) { ob_end_clean(); return $r; } @@ -410,34 +444,27 @@ * */ - function debug($string) + function debug($string,$level =1 ) { - if (!$this->options['debug']) { + if ($level > $this->options['debug.level'] ) { return; } - if (ini_get('html_errors')) { - echo "<PRE><B>TEMPLATE DEBUG:</B> $string</PRE>"; - } else { - echo "TEMPLATE DEBUG: $string\n"; - } + $this->assertProvider('Debug'); + $this->provider['Debug']->debug($string); } /** * - * Lazy loading of PEAR, and the error handler.. + * Global RaiseError - very lightweight! * - * This should load Template_Error really.. (where all the mapping of error stuff can go..) + * This may return a PEAR_Error/ErrorStack, or throw an exception! * * @access public - * * @param string $mesage The error message text. - * * @param int $type Error type. - * * @param int $severity An equivalent to PEAR_ERROR_[RETURN|DIE|etc] - * * @param array $args Args for printf view of message. * * @return object PEAR_Error object. @@ -447,7 +474,7 @@ function raiseError($message, $type = null, $severity = TEMPLATE_ERROR_RETURN, $args = array()) { - $this->debug("<B>TEMPLATE::raiseError</B>$message"); + $this->debug("<B>TEMPLATE::raiseError</B>$message ($type/$severity)".print_r($args,true)); $this->assertProvider('Error'); return $this->provider['Error']->raiseError($message, $type, $severity, $args); @@ -587,8 +614,14 @@ return true; } - require_once "Template/$name.php"; + $file = "Template/$name.php"; $class = 'Template_'.str_replace('/','_',$name); + if (isset($this->options['provider'][$name])) { + $file = $this->options['provider'][$name][0]; + $class = $this->options['provider'][$name][1]; + } + require_once "Template/$name.php"; + // this may not work correctly - constructors can be nasty // when passing by reference.. $this->provider[$name] = new $class; @@ -673,6 +706,23 @@ } return isset($this->data['elements_original'][$name]) ? $this->data['elements_original'][$name] : false; } - - -} \ No newline at end of file + + + + /** + * -- consider if this is necessary.. + * Set one or multiple (array) options + * + * @access public + * @param mixed Associative array of options or name of single option + * @param mixed Value of single option + * @return void + */ + function setOption($name, $value = null) + { + if (is_array($name)) { + $this->options = array_merge($this->options, $name); + } else { + $this->options[$name] = $value; + } + }
« previous php.pear.cvs (#20736) next »