cvs: pear /Template Template.php /Template/Template Assign.php Filter.php Provider.php Source.php

From: Date: Thu, 17 Jun 2004 00:36:21 +0000
Subject: cvs: pear /Template Template.php /Template/Template Assign.php Filter.php Provider.php Source.php
Groups: php.pear.cvs 
Request: Send a blank email to pear-cvs+get-20783@lists.php.net to get a copy of this message
jausions Wed Jun 16 20:36:21 2004 EDT Added files: /pear/Template/Template Filter.php Modified files: /pear/Template Template.php /pear/Template/Template Assign.php Provider.php Source.php Log: - Some work done on Filters: - Added a filters.enabled option - Added the Filters providers - Added missing ending ?> PHP tag to files

http://cvs.php.net/diff.php/pear/Template/Template.php?r1=1.14&r2=1.15&ty=u Index: pear/Template/Template.php diff -u pear/Template/Template.php:1.14 pear/Template/Template.php:1.15 --- pear/Template/Template.php:1.14 Wed Jun 16 15:51:24 2004 +++ pear/Template/Template.php Wed Jun 16 20:36:21 2004 @@ -49,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.14 2004/06/16 19:51:24 jausions Exp $ +* $Id: Template.php,v 1.15 2004/06/17 00:36:21 jausions Exp $ * * @package Template * @@ -181,6 +181,7 @@ var $provider = array( 'Assign' => null, // assign API + 'Filters' => null, // Output filters 'Plugin' => null, // plugins 'Block' => null, // IT type templates 'Source' => null, // template source locater and loader? @@ -316,22 +317,20 @@ /** - * - * Outputs the template. - * - * @access public - * - * @return void - * - */ - + * Outputs the template. + * + * @access public + * + * @return bool|PEAR_Error TRUE or PEAR_Error object on error + */ function output() { $this->debug( "output {$this->files['compiled_template']}"); - if (isset($this->options['filters'])) { + if (!empty($this->options['filters']) + && @$this->options['filters.enabled']) { $this->assertProvider('Filter'); return $this->provider['Filter']->output(); } @@ -423,7 +422,8 @@ */ function toString() { - if (isset($this->options['filters'])) { + if (!empty($this->options['filters']) + && @$this->options['filters.enabled']) { $this->assertProvider('Filter'); return $this->provider['Filter']->toString(); } http://cvs.php.net/diff.php/pear/Template/Template/Assign.php?r1=1.4&r2=1.5&ty=u Index: pear/Template/Template/Assign.php diff -u pear/Template/Template/Assign.php:1.4 pear/Template/Template/Assign.php:1.5 --- pear/Template/Template/Assign.php:1.4 Wed Jun 16 12:00:25 2004 +++ pear/Template/Template/Assign.php Wed Jun 16 20:36:21 2004 @@ -16,24 +16,24 @@ // | Authors: many... // +----------------------------------------------------------------------+ // -// $Id: Assign.php,v 1.4 2004/06/16 16:00:25 alan_k Exp $ +// $Id: Assign.php,v 1.5 2004/06/17 00:36:21 jausions Exp $ // -// Provider for Assign API +// Provider for Assign API // define('TEMPLATE_ASSIGN_ERROR_INVALIDARGS', -100); class Template_Assign { - - + + /** * A reference to the instantated template. * - * @var object Template + * @var object Template * @access public */ var $template = null; - + /** * initialize the Provider. * @@ -43,58 +43,58 @@ * @access public * @see Template_Provider */ - - function initProvider(&$template) + + function initProvider(&$template) { // if you need to access template variables (Eg. options/data/providers) // you should store a reference to the template object. $this->template = &$template; - - // we alias the template data to this - so the class can be used + + // we alias the template data to this - so the class can be used // for other things without depending on Template. - + $this->data = &$template->data; - + return true; - + } - - - - + + + + /** * Normally a reference to the template->data array, however the class can be used - * standalone.. + * standalone.. * * @var array * @access public */ var $data = array(); - - + + /** - * + * * Assigns a token-name and value to $this->_token_vars for use in a * template. - * + * * There are three valid ways to assign values to a template. - * + * * Form 1: $args[0] is a string and $args[1] is mixed. This means * $args[0] is a token name and $args[1] is the token value (which - * allows objects, arrays, strings, numbers, or anything else). + * allows objects, arrays, strings, numbers, or anything else). * $args[1] can be null, which means the corresponding token value in * the template will also be null. - * + * * Form 2: $args[0] is an array and $args[1] is not set. Assign a * series of tokens where the key is the token name, and the value is * token value. - * + * * Form 3: $args[0] is an object and $args[1] is not set. Assigns * copies of all object variables (properties) to tokens; the token * name and value is a copy of each object property and value. - * + * * @access public - * + * * @param string|array|object $args[0] This param can be a string, an * array, or an object. If $args[0] is a string, it is the name of a * variable in the template. If $args[0] is an array, it must be an @@ -102,37 +102,37 @@ * name in the template and the value is the value for that variable * in the template. If $args[0] is an object, copies of its * properties will be assigned to the template. - * + * * @param mixed $args[1] If $args[0] is an array or object, $args[1] * should not be set. Otherwise, a copy of $args[1] is assigned to a * template variable named after $args[0]. - * + * * @return bool|PEAR_Error Boolean true if all assignments were * committed, or a PEAR_Error object if there was an error. - * + * * @throws SAVANT_ERROR_ASSIGN Unknown reason for error, probably * because you passed $args[1] when $args[0] is an array or object. - * - * @author Paul M. Jones <pmjones@ciaweb.net> + * + * @author Paul M. Jones <pmjones@ciaweb.net> * @see assignRef() - * + * * @see assignObject() - * + * */ - + function setData() - { + { // in Form 1, $args[0] is a string name and $args[1] is mixed. // in Form 2, $args[0] is an associative array. // in Form 3, $args[0] is an object. $args = func_get_args(); $count = count($args); - + // ------------------------------------------------------------- // // Now we assign variable copies. // - + // form 1 (string name and mixed value) // don't check isset() on $args[1] becuase a 'null' is not set, // and we might want to pass a null. @@ -142,73 +142,73 @@ } // keep a copy in the token vars array $this->data['variables'][$args[0]] = $args[1]; - + // done! return true; } - + // form 2 (assoc array) if (is_array($args[0]) && $count == 1) { - + foreach ($args[0] as $key=>$val) { $this->setData($key, $val); } - + // done! return true; } - + // form 3 (object props) if (is_object($args[0]) && $count == 1) { - + // get the object properties $data = get_object_vars($args[0]); foreach ($data as $key=>$val) { $this->setData($key, $val); } - + // done! return true; } - - + + // ------------------------------------------------------------- // // Final error catch. We should not have gotten to this point. // - + return $this->template->raiseError( - "invalid type sent to assign, %s,", + "invalid type sent to assign, %s,", TEMPLATE_ASSIGN_ERROR_INVALIDARGS, TEMPLATE_ERROR_RETURN, array(print_r($args,true)) ); } - + /** - * + * * Assign a token by reference. This allows you change variable * values within the template and have those changes reflected back * at the calling logic script. Works as with form 2 of assign(). - * + * * @access public - * + * * @param string $name The template token-name for the reference. - * + * * @param mixed &$ref The variable passed by-reference. - * + * * @return bool|PEAR_Error Boolean true on success, or a PEAR_Error * on failure. - * + * * @throws SAVANT_ERROR_ASSIGN_REF Unknown reason for error. - * + * * @see assign() - * @author Paul M. Jones <pmjones@ciaweb.net> + * @author Paul M. Jones <pmjones@ciaweb.net> * @see assignObject() - * + * */ - + function setDataByRef($name, &$ref) { // look for the proper case: name and variable @@ -219,11 +219,11 @@ // // assign the token as a reference $this->data['byref'][$name] =& $ref; - + // done! return true; } - + // final error catch return $this->template->raiseError( "invalid type sent to assignRef, %s %s", @@ -232,7 +232,9 @@ array(print_r($name,true),print_r($ref,true)) ); } - - -} \ No newline at end of file + + +} + +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Template/Template/Provider.php?r1=1.2&r2=1.3&ty=u Index: pear/Template/Template/Provider.php diff -u pear/Template/Template/Provider.php:1.2 pear/Template/Template/Provider.php:1.3 --- pear/Template/Template/Provider.php:1.2 Mon Jun 14 04:34:07 2004 +++ pear/Template/Template/Provider.php Wed Jun 16 20:36:21 2004 @@ -16,41 +16,41 @@ // | Authors: alan@akkbhome.com | // +----------------------------------------------------------------------+ // -// $Id: Provider.php,v 1.2 2004/06/14 08:34:07 alan_k Exp $ +// $Id: Provider.php,v 1.3 2004/06/17 00:36:21 jausions Exp $ // // Example of how a Provider can be written. // class Template_Provider { - + /** * Default options related to this provider. * Please document these...!!! - * + * * @var array * @access public */ var $options = array( 'provider.use_something' => true // an example of an option. ) - + /** * A reference to the instantated template. * - * @var object Template + * @var object Template * @access public */ var $template = null; - + /** * initialize the Provider. * - * when the provider is created by the template, this method is called, with a + * when the provider is created by the template, this method is called, with a * reference to the template. * * Why not use PHP Constructors? - * unfortunatly this would make using a Provider as a Factory base class almost + * unfortunatly this would make using a Provider as a Factory base class almost * impossible (if not a little messy. * * Why not use a static Construct method? @@ -63,21 +63,22 @@ * @return none (it may return a PEAR::Error) * @access public */ - - function initProvider(&$template) + + function initProvider(&$template) { // if you need to access template variables (Eg. options/data/providers) // you should store a reference to the template object. $this->template = &$template; - + // if you add default options, merge your options. $this->template->options = array_merge($this->options,$this->template->options); - // if you change any of the default options + // if you change any of the default options // (you may be overriding the user specifed ones.) $this->template->options['output.no_extract'] = true; return true; - + } } +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Template/Template/Source.php?r1=1.1&r2=1.2&ty=u Index: pear/Template/Template/Source.php diff -u pear/Template/Template/Source.php:1.1 pear/Template/Template/Source.php:1.2 --- pear/Template/Template/Source.php:1.1 Wed Jun 16 11:42:05 2004 +++ pear/Template/Template/Source.php Wed Jun 16 20:36:21 2004 @@ -16,19 +16,19 @@ // | Authors: alan@akkbhome.com | // +----------------------------------------------------------------------+ // -// $Id: Source.php,v 1.1 2004/06/16 15:42:05 alan_k Exp $ +// $Id: Source.php,v 1.2 2004/06/17 00:36:21 jausions Exp $ // -// Source Provider - deals with +// Source Provider - deals with // a) mulitple paths for the source of a template // b) different source types (eg. file handles, template source, object providers?) -// +// class Template_Source { - + /** * Default options related to this provider. - * + * * @var array * @access public */ @@ -38,15 +38,15 @@ // file to exist in multiple sources // eg for themes. ); - + /** * A reference to the instantated template. * - * @var object Template + * @var object Template * @access public */ var $template = null; - + /** * initialize the Provider. * @@ -56,65 +56,65 @@ * @access public * @see Template_Provider */ - - function initProvider(&$template) + + function initProvider(&$template) { // if you need to access template variables (Eg. options/data/providers) // you should store a reference to the template object. $this->template = &$template; - + // if you add default options, merge your options. - $this->template->options = array_merge($this->options,$this->template->options); - + $this->template->options = array_merge($this->options, $this->template->options); + return true; - + } - - - + + + /** * Find the location of the requested file. * * Looks in : * a) {options[template_dir][*]}/templatename.{locale}.{ext} * b) {options[template_dir][*]}/templatename - * - * + * + * * @param string name of file. * * @return boolean true || pear error on failure. * @access public */ - - + + function open($file) { - - + + switch (get_type($file)) { case 'string': - + $options = $this->template->options; $files = &$this->template->options; - - + + //Remove the slash if there is one in front, just to be safe. $file = ltrim($file,DIRECTORY_SEPARATOR); - - - + + + $files['current_template'] = false; - - + + $this->resolveLocale( $file ); - - - + + + // found it already! ( a localized version ) if ($files['current_template']) { return true; } $this->resolveMultiPath(); - + if ($files['currentTemplate'] === false) { return $this->template->raiseError( "Could not find Template %s in any of your paths (%s)" . @@ -128,36 +128,36 @@ case 'resource': // file pointer.. return false; } - - - + + + } - + function resolveLocale($file) { - + // PART A mulitlanguage support: ( part B is gettext support in the engine..) // - user created language version of template. // - compile('abcdef.html') will check for compile('abcdef.en.html') // (eg. when locale=en) - + $options = $this->template->options; $files = &$this->template->options; - + $parts = array(); if ($options['locale'] && preg_match('/(.*)(\.[a-z]+)$/i',$file,$parts)) { $newfile = $parts[1].'.'.$options['locale'] .$parts[2]; $dirs = array_unique($options['template_dir']); - + if ($options['multisource.order'] == 'reverse') { $dirs = array_reverse($dirs); } - + foreach ($options['template_dir'] as $tmplDir) { if (@!file_exists($tmplDir . DIRECTORY_SEPARATOR .$newfile)) { continue; } - + if ($files['current_template'] !== false && !$options['multisource.allow']) { $old = $files['current_template']; $files['current_template'] = false; @@ -166,7 +166,7 @@ "Than once, (in %s and %s)", TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE, array($newfile, $old, $tmplDir . DIRECTORY_SEPARATOR .$newfile )); - + } $files['current_template'] = $tmplDir . DIRECTORY_SEPARATOR .$newfile; $files['current_template_from_dir'] = $tmplDir; @@ -174,7 +174,7 @@ } } } - + function resolveMultiPath() { // look in all the posible locations for the template directory.. if ($files['currentTemplate'] === false) { @@ -186,7 +186,7 @@ if (!@file_exists($tmplDir . DIRECTORY_SEPARATOR . $file)) { continue; } - + if (!$options['multisource.allow'] && ($files['current_template'] !== false)) { return $this->template->raiseError( "You have more than one template Named %s in your paths, found in more" . @@ -194,13 +194,14 @@ TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE, array($file, $old, $tmplDir . DIRECTORY_SEPARATOR .$file )); } - + $files['current_template'] = $tmplDir . DIRECTORY_SEPARATOR . $file; $files['current_template_from_dir'] = $tmplDir; } } } - - + + } +?> \ No newline at end of file http://cvs.php.net/co.php/pear/Template/Template/Filter.php?r=1.1&p=1 Index: pear/Template/Template/Filter.php +++ pear/Template/Template/Filter.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2004 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 3.0 of the PHP license, | // | that is bundled with this package in the file LICENSE, and is | // | available at through the world-wide-web at | // | http://www.php.net/license/3_0.txt. | // | If you did not receive a copy of the PHP license and are unable to | // | obtain it through the world-wide-web, please send a note to | // | license@php.net so we can mail you a copy immediately. | // +----------------------------------------------------------------------+ // | Authors: Philippe Jausions <Philippe.Jausions -@- 11abacus.com> | // +----------------------------------------------------------------------+ // // $Id: Filter.php,v 1.1 2004/06/17 00:36:21 jausions Exp $ // // Filter provider. // class Template_Filter { /** * A reference to the instantated template. * * @var object Template * @access public **/ var $template = null; /** * A reference to the list of filters * * @access public * @var array **/ var $filters = null; /** * Initialize the Provider. * * when the provider is created by the template, this method is called, with a * reference to the template. * * Why not use PHP Constructors? * unfortunatly this would make using a Provider as a Factory base class almost * impossible (if not a little messy. * * Why not use a static Construct method? * a) this may limit the same pattern in any subclasses. * b) it gives you the posiblility to extend a base class, that does the same as this. * * * @param object Template The instantated template. * * @return bool|PEAR_Error TRUE or PEAR_Error object on error * @access public **/ function initProvider(&$template) { // if you need to access template variables (Eg. options/data/providers) // you should store a reference to the template object. $this->template = &$template; // if you add default options, merge your options. if (isset($template->options['filters'])) { $this->filters =& $template->options['filters']; } return true; } /** * Filter the output of the template * * @access public * @return bool|PEAR_Error TRUE or PEAR_Error object on error **/ function output() { if (!is_string($r = $this->toString())) { return $r; } echo $r; return true; } /** * Return a filtered output of the template * * @access public * @return bool|PEAR_Error TRUE or PEAR_Error object on error **/ function toString() { if (!@$this->template->options['filters.enabled']) { return $this->template->toString(); } $this->template->options['filters.enabled'] = false; if (is_string($r = $this->template->toString())) { foreach (array_keys($this->filter) as $i) { if (!is_string($r = call_user_func($this->filter[$i], $r))) { break; } } } $this->template->options['filters.enabled'] = true; return $r; } } ?>
« previous php.pear.cvs (#20783) next »