cvs: pear /Template/Template Assign.php Error.php Filter.php Plugin.php Source.php /Template/Template/Filter ColourizeCode.php TrimWhiteSpace.php
/Template/tests TemplateTest.php test.php /Template/tests/templates simple_template.php
| From: | Philippe Jausions | Date: | Mon, 05 Jul 2004 00:37:01 +0000 |
| Subject: | cvs: pear /Template/Template Assign.php Error.php Filter.php Plugin.php Source.php /Template/Template/Filter ColourizeCode.php TrimWhiteSpace.php /Template/tests TemplateTest.php test.php /Template/tests/templates simple_template.php |
||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-21512@lists.php.net to get a copy of this message | ||
jausions Sun Jul 4 20:37:01 2004 EDT
Added files:
/pear/Template/Template Error.php
/pear/Template/tests TemplateTest.php test.php
/pear/Template/tests/templates simple_template.php
Modified files:
/pear/Template/Template Assign.php Filter.php Plugin.php Source.php
/pear/Template/Template/Filter ColourizeCode.php TrimWhiteSpace.php
Log:
Added unit tests.
Very tentative use of PEAR_ErrorStack
Corrected various files to make the package pass them.
http://cvs.php.net/diff.php/pear/Template/Template/Assign.php?r1=1.6&r2=1.7&ty=u Index: pear/Template/Template/Assign.php diff -u pear/Template/Template/Assign.php:1.6 pear/Template/Template/Assign.php:1.7 --- pear/Template/Template/Assign.php:1.6 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Assign.php Sun Jul 4 20:37:00 2004 @@ -16,7 +16,7 @@ // | Authors: many... // +----------------------------------------------------------------------+ // -// $Id: Assign.php,v 1.6 2004/07/04 18:23:09 jausions Exp $ +// $Id: Assign.php,v 1.7 2004/07/05 00:37:00 jausions Exp $ // // Provider for Assign API // @@ -107,7 +107,7 @@ * should not be set. Otherwise, a copy of $args[1] is assigned to a * template variable named after $args[0]. * - * @return bool|PEAR_Error TRUE or a PEAR_Error object on error. + * @return bool TRUE or FALSE on error. * * @throws SAVANT_ERROR_ASSIGN Unknown reason for error, probably * because you passed $args[1] when $args[0] is an array or object. @@ -122,7 +122,7 @@ // in Form 2, $args[0] is an associative array. // in Form 3, $args[0] is an object. $args = func_get_args(); - $count = count($args); + $argc = count($args); // ------------------------------------------------------------- // @@ -132,10 +132,9 @@ // 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. - if (is_string($args[0]) && $count > 1) { - if (isset($this->data['byref'][$args[0]])) { - unset($this->data['byref'][$args[0]]); - } + if (is_string($args[0]) && $argc > 1) { + unset($this->data['byref'][$args[0]]); + // keep a copy in the token vars array $this->data['variables'][$args[0]] = $args[1]; @@ -144,9 +143,9 @@ } // form 2 (assoc array) - if (is_array($args[0]) && $count == 1) { + if (is_array($args[0]) && $argc == 1) { - foreach ($args[0] as $key=>$val) { + foreach ($args[0] as $key => $val) { $this->setData($key, $val); } @@ -155,7 +154,7 @@ } // form 3 (object props) - if (is_object($args[0]) && $count == 1) { + if (is_object($args[0]) && $argc == 1) { // get the object properties $data = get_object_vars($args[0]); @@ -177,7 +176,7 @@ "invalid type sent to assign, %s,", TEMPLATE_ASSIGN_ERROR_INVALIDARGS, TEMPLATE_ERROR_RETURN, - array(print_r($args,true)) + array(print_r($args, true)) ); } @@ -191,7 +190,7 @@ * * @param mixed &$ref the variable passed by-reference. * - * @return bool|PEAR_Error TRUE or a PEAR_Error object on error + * @return bool TRUE or FALSE on error * * @access public * @author Paul M. Jones <pmjones@ciaweb.net> @@ -200,10 +199,8 @@ function setDataByRef($name, &$ref) { // look for the proper case: name and variable - if (is_string($name) && isset($ref)) { - if (isset($this->data['variables'][$name])) { - unset($this->data['variables'][$name]); - } + if (is_string($name) && (isset($ref) || is_null($ref))) { + unset($this->data['variables'][$name]); // // assign the token as a reference $this->data['byref'][$name] =& $ref; @@ -217,7 +214,7 @@ "invalid type sent to assignRef, %s %s", TEMPLATE_ASSIGN_ERROR_INVALIDARGS, TEMPLATE_ERROR_RETURN, - array(print_r($name,true),print_r($ref,true)) + array(print_r($name, true), print_r($ref, true)) ); } } http://cvs.php.net/diff.php/pear/Template/Template/Filter.php?r1=1.6&r2=1.7&ty=u Index: pear/Template/Template/Filter.php diff -u pear/Template/Template/Filter.php:1.6 pear/Template/Template/Filter.php:1.7 --- pear/Template/Template/Filter.php:1.6 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Filter.php Sun Jul 4 20:37:01 2004 @@ -16,7 +16,7 @@ // | Authors: Philippe Jausions <Philippe.Jausions -@- 11abacus.com> | // +----------------------------------------------------------------------+ // -// $Id: Filter.php,v 1.6 2004/07/04 18:23:09 jausions Exp $ +// $Id: Filter.php,v 1.7 2004/07/05 00:37:01 jausions Exp $ // // Filter provider. // @@ -82,7 +82,7 @@ * * @param object Template The instantated template. * - * @return bool|PEAR_Error TRUE or PEAR_Error object on error + * @return bool TRUE or FALSE on error * @access public */ function initProvider(&$template) @@ -97,7 +97,7 @@ /** * Filters the output of the template * - * @return bool|PEAR_Error TRUE or PEAR_Error object on error + * @return bool TRUE or FALSE error * @access public */ function output() @@ -113,7 +113,7 @@ /** * Returns a filtered output of the template * - * @return string|PEAR_Error Result or PEAR_Error object on error + * @return string|bool Result or FALSE on error * @access public */ function toString() @@ -124,7 +124,9 @@ return $this->template->toString(); } // load the filters if necessary - $this->_loadFilters(); + if (true !== ($r = $this->_loadFilters()) { + return $r; + } // dont recurse! $this->template->options['filter.enabled'] = false; @@ -136,24 +138,25 @@ foreach (array_keys($this->filter) as $i) { // call the filter method on the object.. if (is_object($this->filter[$i])) { - $r = $this->filter[$i]->filter($r); - } - // call a function. - if (is_string($this->filter[$i])) { - $r = $this->filter[$i]($r); - } - // call a user class/method etc.. - if (is_array($this->filter[$i]) && is_callable($this->filter[$i])) { - $r = call_user_func($this->filter[$i],$r); + $exit = $this->filter[$i]->filter($r); + + } elseif (is_string($this->filter[$i])) { + // call a function. + $exit = $this->filter[$i]($r); + + } elseif (is_array($this->filter[$i]) + && is_callable($this->filter[$i])) { + // call a user class/method etc.. + $exit = call_user_func($this->filter[$i], $r); } // technically if this filter didnt get used.. it's an error condition!! - if (!is_string($r)) { + if (!is_string($r) || $exit === false) { break; } } - // prevent recursive calling! + // Done applying filters $this->template->options['filter.enabled'] = true; return $r; } @@ -162,14 +165,14 @@ * Loads the required filters into $this->filters.. * * @interface - * @return string|PEAR_Error + * @return bool TRUE or FALSE on error * @access public */ function _loadFilters() { // already loaded? if ($this->filters) { - return; + return true; } foreach (array_keys($this->template->options['filter.sources']) as $k) { if (!is_int($k)) { @@ -211,27 +214,31 @@ } } // error condition!!! - $this->template->raiseError( + return $this->template->raiseError( 'Filter::_loadFilter() unknown filter type %s', TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE, - array(print_r($v,true)) + array(print_r($v, true)) ); } // we should have all the stuff loaded into filters now.!! - + return true; } /** - * Performs the actual filtering. + * Performs the actual filtering + * + * To be implemented by individual filters. The filter modifies + * the parameter passed. * * @interface - * @return string|PEAR_Error + * @param string $string a reference to a string to filter + * @return bool TRUE or FALSE on error * @access public */ - function filter($string) + function filter(&$string) { - return $string; + return true; } } http://cvs.php.net/diff.php/pear/Template/Template/Plugin.php?r1=1.2&r2=1.3&ty=u Index: pear/Template/Template/Plugin.php diff -u pear/Template/Template/Plugin.php:1.2 pear/Template/Template/Plugin.php:1.3 --- pear/Template/Template/Plugin.php:1.2 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Plugin.php Sun Jul 4 20:37:01 2004 @@ -16,7 +16,7 @@ // | Authors: nobody <nobody@localhost> | // +----------------------------------------------------------------------+ // -// $Id: Plugin.php,v 1.2 2004/07/04 18:23:09 jausions Exp $ +// $Id: Plugin.php,v 1.3 2004/07/05 00:37:01 jausions Exp $ // // Plugin API provides support for < ? = $this->plugin(".....",.....); ? > // or {this.plugin(#xxxxx#,#xxxx#):h} @@ -32,42 +32,39 @@ class Template_Plugin { /** - * reference to main engine.. - * - * @var object Template - * @access public - */ + * reference to main engine.. + * + * @var Template + * @access public + */ var $template; var $pluginCache = array(); // store of instanced plugins.. - /** - * Options used by the Plugin Provider - * - * currently only plugin.sources (a list of the plugin objects to be loaded) - * when searching for plugin methods. - * - * - * - * @var array - * @access public - */ + /** + * Options used by the Plugin Provider + * + * currently only plugin.sources (a list of the plugin objects to be loaded) + * when searching for plugin methods. + * + * @var array + * @access public + */ var $options = array( 'plugin.sources' => array( 'Savant', // default to using savant plugin Provider // from Template/Plugin/Savant.php // 'MyPlugin' => '/source/of/myPlugin.php' )); + /** - * instances of plugin objects. - * - * @var array Template_Plugin's - * @access public - */ + * instances of plugin objects. + * + * @var array Template_Plugin's + * @access public + */ var $plugins = array(); - - function initProvider(&$template) { // if you need to access template variables (Eg. options/data/providers) @@ -80,18 +77,18 @@ return true; } + /** - * Call a Plugin method. - * - * Look up in all the plugins to see if the method exists, if it does, call it. - * - * - * @param array name of method, arguments. - * - * - * @return string hopefully - * @access public - */ + * Call a Plugin method. + * + * Look up in all the plugins to see if the method exists, if it does, call it. + * + * + * @param array name of method, arguments. + * + * @return string hopefully + * @access public + */ function call($args) { @@ -99,30 +96,27 @@ // attempt to load the plugin on-the-fly $class = $this->_loadPlugins($method); - if (is_a($class,'PEAR_Error')) { + if (is_a($class, 'PEAR_Error')) { //echo $class->toString(); return $class; } - // first argument is always the plugin name; shift the first // argument off the front of the array and reduce the number of // array elements. array_shift($args); - return call_user_func_array(array(&$this->plugins[$class],$method), $args); + return call_user_func_array(array(&$this->plugins[$class], $method), $args); } /** - * Load the plugins, and lookup which one provides the required method - * - * - * @param string Name - * - * @return string|PEAR_Error the class that provides it. - * @access private - */ - + * Load the plugins, and lookup which one provides the required method + * + * @param string Name + * + * @return string|bool the class that provides it. + * @access private + */ function _findPlugin($name) { // name can be: @@ -158,6 +152,6 @@ return Template::raiseError('could not find plugin with method '. $name); } +} - -} \ No newline at end of file +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Template/Template/Source.php?r1=1.3&r2=1.4&ty=u Index: pear/Template/Template/Source.php diff -u pear/Template/Template/Source.php:1.3 pear/Template/Template/Source.php:1.4 --- pear/Template/Template/Source.php:1.3 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Source.php Sun Jul 4 20:37:01 2004 @@ -16,7 +16,7 @@ // | Authors: alan@akkbhome.com | // +----------------------------------------------------------------------+ // -// $Id: Source.php,v 1.3 2004/07/04 18:23:09 jausions Exp $ +// $Id: Source.php,v 1.4 2004/07/05 00:37:01 jausions Exp $ // /** @@ -54,9 +54,9 @@ /** * Initializes this provider. * - * @param object Template The instantated template. + * @param $template Template the instantated template. * - * @return boolean (it may return a PEAR::Error) + * @return boolean TRUE or FALSE on error * @access public * @see Template_Provider */ @@ -82,33 +82,27 @@ * 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 + * @return boolean TRUE or FALSE on error + * @access public */ function open($file) { - switch (get_type($file)) { + switch (gettype($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); - - + // 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; @@ -173,9 +167,11 @@ $files['current_template_used_file'] = $newfile; } } + return true; } - function resolveMultiPath() { + function resolveMultiPath() + { // look in all the posible locations for the template directory.. if ($files['currentTemplate'] === false) { $dirs = array_unique($options['template_dir']); @@ -199,6 +195,7 @@ $files['current_template_from_dir'] = $tmplDir; } } + return true; } http://cvs.php.net/diff.php/pear/Template/Template/Filter/ColourizeCode.php?r1=1.1&r2=1.2&ty=u Index: pear/Template/Template/Filter/ColourizeCode.php diff -u pear/Template/Template/Filter/ColourizeCode.php:1.1 pear/Template/Template/Filter/ColourizeCode.php:1.2 --- pear/Template/Template/Filter/ColourizeCode.php:1.1 Fri Jun 25 08:51:30 2004 +++ pear/Template/Template/Filter/ColourizeCode.php Sun Jul 4 20:37:01 2004 @@ -1,100 +1,107 @@ <?php /** -* +* * Colorizes all text between <code>...</code> tags with PHP's * highlight_string function. Additionally, this will convert HTML * entities within <code> blocks, so you can demonstrate HTML tags * without them being honored in the browser. Also converts tabs to four * spaces. -* +* * To mark the beginning of php code in a <code> block, use the custom * tag <php>, and use </php> to mark the end. You can't use the normal * php code tags because Savant will try to execute that code when the * template runs. -* +* * @author Paul M. Jones <pmjones@ciaweb.net> -* -* @version $Id: ColourizeCode.php,v 1.1 2004/06/25 12:51:30 alan_k Exp $ -* +* +* @version $Id: ColourizeCode.php,v 1.2 2004/07/05 00:37:01 jausions Exp $ +* */ - + +/** + * @package Template + * @subpackage Template_Filter[ColorizeCode] + */ class Template_Filter_ColorizeCode extends Template_Filter { /** - * do the filtering - * - * - * @param string input string - * - * @return string|PEAR_Error - * @access public - * @see Template_Filter::filter() - */ - + * Colorizes PHP code + * + * @param string $text input string + * + * @return string|bool the filtered result or FALSE on error + * @access public + * @see Template_Filter::filter() + * @see highlight_string() + */ function filter(&$text) { // break the source into blocks by the beginning <code> tag. // this will remove the text "<code>" from the block, so // we'll need to add it in again later. $blocks = explode('<code>', $text); - + // loop through each block and convert text within // <code></code> tags. foreach ($blocks as $key => $val) { - + // now find then the ending </code> within the block $pos = strpos($val, '</code>'); - + if ($pos === false) { - + // there was no </code> tag -- do nothing - + } else { - + // change all <php> and </php> tags $val = str_replace('<php>', '<?php', $val); $val = str_replace('</php>', '?>', $val); // <?php - + // $tmp[0] will be the part before </code>, and // thus the part we want to modify. - // + // // $tmp[1] will be the part after the // <code></code> block, which we will leave // alone. - // + // // this will remove the text "</code>" from the // text, so we'll need to add it in again when modifying // the text. $tmp = explode('</code>', $val); - + // set entities by highlighting the string. we do the // output buffering ob() thing because the native // highlight_string() dumps the output to the screen // instead of returning to a variable (before PHP // 4.2.2). $tmp[0] = trim($tmp[0]); - + ob_start(); highlight_string($tmp[0]); $tmp[0] = ob_get_contents(); ob_end_clean(); - + // remove break tags from the highlighted text $tmp[0] = str_replace("<br />", "\n", $tmp[0]); - + // convert tabs to 4-spaces and then // re-surround with <code> tags $tmp[0] = str_replace("\t", ' ', $tmp[0]); - + // save the modified text in the block $blocks[$key] = $tmp[0] . $tmp[1]; } - + } - + // reassemble the blocks $text = implode('', $blocks); + + return true; } -} \ No newline at end of file +} + +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Template/Template/Filter/TrimWhiteSpace.php?r1=1.1&r2=1.2&ty=u Index: pear/Template/Template/Filter/TrimWhiteSpace.php diff -u pear/Template/Template/Filter/TrimWhiteSpace.php:1.1 pear/Template/Template/Filter/TrimWhiteSpace.php:1.2 --- pear/Template/Template/Filter/TrimWhiteSpace.php:1.1 Fri Jun 25 08:51:30 2004 +++ pear/Template/Template/Filter/TrimWhiteSpace.php Sun Jul 4 20:37:01 2004 @@ -1,84 +1,85 @@ <?php /** -* -* Trim leading white space and blank lines from template source after it -* gets interpreted, cleaning up code and saving bandwidth. Does not -* affect <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br> -* -* @author Monte Ohrt <monte@ispi.net> -* -* @author Contributions from Lars Noschinski <lars@usenet.noschinski.de> -* -* @author Converted to a Savant filter by Paul M. Jones -* <pmjones@ciaweb.net> -* -* @version $Id: TrimWhiteSpace.php,v 1.1 2004/06/25 12:51:30 alan_k Exp $ -* -*/ + * + * Trim leading white space and blank lines from template source after it + * gets interpreted, cleaning up code and saving bandwidth. Does not + * affect <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br> + * + * @author Monte Ohrt <monte@ispi.net> + * + * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de> + * + * @author Converted to a Savant filter by Paul M. Jones + * <pmjones@ciaweb.net> + * + * @version $Id: TrimWhiteSpace.php,v 1.2 2004/07/05 00:37:01 jausions Exp $ + * + */ - +/** + * @package Template + * @subpackage Template_Filter[TrimWhiteSpace] + */ class Template_Filter_TrimWhiteSpace extends Template_Filter { /** - * Filter the string - * - * - * @param string input string - * - * @return string output string - * @access public - */ - - function filter(&$source) + * Trim white spaces from the string passed + * + * The filter modifies the value passed. + * + * @param string $text input string + * @return bool TRUE or FALSE on error + * @access public + */ + function filter(&$text) { // Pull out the script blocks - preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match); + preg_match_all("!<script[^>]+>.*?</script>!is", $text, $match); $_script_blocks = $match[0]; - $source = preg_replace("!<script[^>]+>.*?</script>!is", - '@@@TEMPLATE:TRIM:SCRIPT@@@', $source); - + $text = preg_replace("!<script[^>]+>.*?</script>!is", + '@@@TEMPLATE:TRIM:SCRIPT@@@', $text); + // Pull out the pre blocks - preg_match_all("!<pre>.*?</pre>!is", $source, $match); + preg_match_all("!<pre>.*?</pre>!is", $text, $match); $_pre_blocks = $match[0]; - $source = preg_replace("!<pre>.*?</pre>!is", - '@@@TEMPLATE:TRIM:PRE@@@', $source); - + $text = preg_replace("!<pre>.*?</pre>!is", + '@@@TEMPLATE:TRIM:PRE@@@', $text); + // Pull out the textarea blocks - preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match); + preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $text, $match); $_textarea_blocks = $match[0]; - $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is", - '@@@TEMPLATE:TRIM:TEXTAREA@@@', $source); - + $text = preg_replace("!<textarea[^>]+>.*?</textarea>!is", + '@@@TEMPLATE:TRIM:TEXTAREA@@@', $text); + // remove all leading spaces, tabs and carriage returns NOT // preceeded by a php close tag. - $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source)); - + $text = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $text)); + // replace script blocks - $this->_replace( "@@@TEMPLATE:TRIM:SCRIPT@@@",$_script_blocks, $source); - + $this->_replace( "@@@TEMPLATE:TRIM:SCRIPT@@@",$_script_blocks, $text); + // replace pre blocks - $this->_replace( "@@@TEMPLATE:TRIM:PRE@@@",$_pre_blocks, $source); - + $this->_replace( "@@@TEMPLATE:TRIM:PRE@@@",$_pre_blocks, $text); + // replace textarea blocks - $this->_replace( "@@@TEMPLATE:TRIM:TEXTAREA@@@",$_textarea_blocks, $source); - - return $source; + $this->_replace( "@@@TEMPLATE:TRIM:TEXTAREA@@@",$_textarea_blocks, $text); + + return true; } + /** - * put back the special elements that cant have space trimmed from them.. - * - * @param string matching string. - * @param array array of source matches - * @param string the full string to modify.. - * - * - * @return none - * @access private - */ - + * Puts back the special elements that cant have space trimmed from them.. + * + * @param string matching string. + * @param array array of source matches + * @param string the full string to modify.. + * + * @return void + * @access private + */ function _replace($search_str, $replace, &$subject) { $_len = strlen($search_str); @@ -91,6 +92,6 @@ } } } - } + ?> http://cvs.php.net/co.php/pear/Template/Template/Error.php?r=1.1&p=1 Index: pear/Template/Template/Error.php +++ pear/Template/Template/Error.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 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/2_02.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: many... // +----------------------------------------------------------------------+ // // $Id: Error.php,v 1.1 2004/07/05 00:37:01 jausions Exp $ // // Provider for Assign API // /** * @package Template * @subpackage Template_Error */ class Template_Error { /** * Reference to the instantated template. * * @var Template * @access public */ var $template = null; /** * @var PEAR_ErrorStack * @access protected */ var $error_stack = null; /** * Initializes the Provider. * * @param Template the instantiated template engine * * @return bool TRUE on success, FALSE otherwise * @access public * @see Template_Provider */ 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; require_once 'PEAR/ErrorStack.php'; $this->error_stack =& PEAR_ErrorStack::singleton('Template'); return true; } /** * This may return a PEAR_Error/ErrorStack, or throw an exception! * * @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. * * @access public */ function raiseError($message, $type = null, $severity = TEMPLATE_ERROR_RETURN, $args = array()) { print_r($message); print_r($args); switch ($severity) { case TEMPLATE_ERROR_RETURN: $this->error_stack->push($type, 'error', $args, $message); break; case TEMPLATE_ERROR_DIE: $this->error_stack->push($type, 'error', $args, $message); exit; break; } return false; } } ?> http://cvs.php.net/co.php/pear/Template/tests/TemplateTest.php?r=1.1&p=1 Index: pear/Template/tests/TemplateTest.php +++ pear/Template/tests/TemplateTest.php <?php // Unit tests for Template package // class TemplateTest extends PHPUnit_TestCase { /** * To hold options for Template * @var array */ var $options; /** * Constructor * * @param string $name */ function __construct($name) { $this->PHPUnit_TestCase($name); } function setUp() { $this->options = array( 'template.dir' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'templates'); } /** * Test set data by name/value */ function testSetDataByNameValuePair() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $t = new Template($this->options); $t->open('simple_template.php'); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * Test set data by array */ function testSetDataByArray() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $array = array( 'value' => $value, 'arrayvalue' => $arrayvalue); $t = new Template($this->options); $t->open('simple_template.php'); $t->setData($array); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * Test set data by reference */ function testSetDataByReference() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); $t = new Template($this->options); $t->open('simple_template.php'); $t->setDataByRef('value', $value); $t->setDataByRef('arrayvalue', $arrayvalue); $value = 'CHANGED VALUE'; $arrayvalue[] = 'ADDED VALUE (INDEX 1)'; ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * toString() and output() methods should return the same thing */ function testOutputAndTostringMethodsReturnSameResult() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); $t = new Template($this->options); $t->open('simple_template.php'); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); ob_start(); $t->output(); $expected = ob_get_clean(); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * We should be able to setData() before opening templates */ function testSettingDataBeforeOpeningTemplateDoesNotMatter() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $t = new Template($this->options); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); $t->open('simple_template.php'); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * Testing clearData() */ function testTemplateDataAreEmptyAfterCleardataMethodIsCalled() { ob_start(); @include 'templates/simple_template.php'; $expected = ob_get_clean(); $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); $t = new Template($this->options); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); $t->clearData(); $t->open('simple_template.php'); $result = $t->toString(); return $this->assertEquals($expected, $result); } } ?> http://cvs.php.net/co.php/pear/Template/tests/test.php?r=1.1&p=1 Index: pear/Template/tests/test.php +++ pear/Template/tests/test.php <?php // Unit tests for Image_Transform package // ini_set('include_path', dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . PATH_SEPARATOR . ini_get('include_path')); require_once 'PHPUnit.php'; require_once 'Template.php'; require_once 'PHP/Compat/Function/file_put_contents.php'; require_once 'PHP/Compat/Function/ob_get_clean.php'; require_once './TemplateTest.php'; $suite = new PHPUnit_TestSuite('TemplateTest'); $result =& PHPUnit::run($suite); echo $result->toString(); ?> http://cvs.php.net/co.php/pear/Template/tests/templates/simple_template.php?r=1.1&p=1 Index: pear/Template/tests/templates/simple_template.php +++ pear/Template/tests/templates/simple_template.php $value = <?php @var_dump($value); ?> $arrayvalue = <?php @var_dump($arrayvalue); ?>
http://cvs.php.net/diff.php/pear/Template/Template/Assign.php?r1=1.6&r2=1.7&ty=u Index: pear/Template/Template/Assign.php diff -u pear/Template/Template/Assign.php:1.6 pear/Template/Template/Assign.php:1.7 --- pear/Template/Template/Assign.php:1.6 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Assign.php Sun Jul 4 20:37:00 2004 @@ -16,7 +16,7 @@ // | Authors: many... // +----------------------------------------------------------------------+ // -// $Id: Assign.php,v 1.6 2004/07/04 18:23:09 jausions Exp $ +// $Id: Assign.php,v 1.7 2004/07/05 00:37:00 jausions Exp $ // // Provider for Assign API // @@ -107,7 +107,7 @@ * should not be set. Otherwise, a copy of $args[1] is assigned to a * template variable named after $args[0]. * - * @return bool|PEAR_Error TRUE or a PEAR_Error object on error. + * @return bool TRUE or FALSE on error. * * @throws SAVANT_ERROR_ASSIGN Unknown reason for error, probably * because you passed $args[1] when $args[0] is an array or object. @@ -122,7 +122,7 @@ // in Form 2, $args[0] is an associative array. // in Form 3, $args[0] is an object. $args = func_get_args(); - $count = count($args); + $argc = count($args); // ------------------------------------------------------------- // @@ -132,10 +132,9 @@ // 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. - if (is_string($args[0]) && $count > 1) { - if (isset($this->data['byref'][$args[0]])) { - unset($this->data['byref'][$args[0]]); - } + if (is_string($args[0]) && $argc > 1) { + unset($this->data['byref'][$args[0]]); + // keep a copy in the token vars array $this->data['variables'][$args[0]] = $args[1]; @@ -144,9 +143,9 @@ } // form 2 (assoc array) - if (is_array($args[0]) && $count == 1) { + if (is_array($args[0]) && $argc == 1) { - foreach ($args[0] as $key=>$val) { + foreach ($args[0] as $key => $val) { $this->setData($key, $val); } @@ -155,7 +154,7 @@ } // form 3 (object props) - if (is_object($args[0]) && $count == 1) { + if (is_object($args[0]) && $argc == 1) { // get the object properties $data = get_object_vars($args[0]); @@ -177,7 +176,7 @@ "invalid type sent to assign, %s,", TEMPLATE_ASSIGN_ERROR_INVALIDARGS, TEMPLATE_ERROR_RETURN, - array(print_r($args,true)) + array(print_r($args, true)) ); } @@ -191,7 +190,7 @@ * * @param mixed &$ref the variable passed by-reference. * - * @return bool|PEAR_Error TRUE or a PEAR_Error object on error + * @return bool TRUE or FALSE on error * * @access public * @author Paul M. Jones <pmjones@ciaweb.net> @@ -200,10 +199,8 @@ function setDataByRef($name, &$ref) { // look for the proper case: name and variable - if (is_string($name) && isset($ref)) { - if (isset($this->data['variables'][$name])) { - unset($this->data['variables'][$name]); - } + if (is_string($name) && (isset($ref) || is_null($ref))) { + unset($this->data['variables'][$name]); // // assign the token as a reference $this->data['byref'][$name] =& $ref; @@ -217,7 +214,7 @@ "invalid type sent to assignRef, %s %s", TEMPLATE_ASSIGN_ERROR_INVALIDARGS, TEMPLATE_ERROR_RETURN, - array(print_r($name,true),print_r($ref,true)) + array(print_r($name, true), print_r($ref, true)) ); } } http://cvs.php.net/diff.php/pear/Template/Template/Filter.php?r1=1.6&r2=1.7&ty=u Index: pear/Template/Template/Filter.php diff -u pear/Template/Template/Filter.php:1.6 pear/Template/Template/Filter.php:1.7 --- pear/Template/Template/Filter.php:1.6 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Filter.php Sun Jul 4 20:37:01 2004 @@ -16,7 +16,7 @@ // | Authors: Philippe Jausions <Philippe.Jausions -@- 11abacus.com> | // +----------------------------------------------------------------------+ // -// $Id: Filter.php,v 1.6 2004/07/04 18:23:09 jausions Exp $ +// $Id: Filter.php,v 1.7 2004/07/05 00:37:01 jausions Exp $ // // Filter provider. // @@ -82,7 +82,7 @@ * * @param object Template The instantated template. * - * @return bool|PEAR_Error TRUE or PEAR_Error object on error + * @return bool TRUE or FALSE on error * @access public */ function initProvider(&$template) @@ -97,7 +97,7 @@ /** * Filters the output of the template * - * @return bool|PEAR_Error TRUE or PEAR_Error object on error + * @return bool TRUE or FALSE error * @access public */ function output() @@ -113,7 +113,7 @@ /** * Returns a filtered output of the template * - * @return string|PEAR_Error Result or PEAR_Error object on error + * @return string|bool Result or FALSE on error * @access public */ function toString() @@ -124,7 +124,9 @@ return $this->template->toString(); } // load the filters if necessary - $this->_loadFilters(); + if (true !== ($r = $this->_loadFilters()) { + return $r; + } // dont recurse! $this->template->options['filter.enabled'] = false; @@ -136,24 +138,25 @@ foreach (array_keys($this->filter) as $i) { // call the filter method on the object.. if (is_object($this->filter[$i])) { - $r = $this->filter[$i]->filter($r); - } - // call a function. - if (is_string($this->filter[$i])) { - $r = $this->filter[$i]($r); - } - // call a user class/method etc.. - if (is_array($this->filter[$i]) && is_callable($this->filter[$i])) { - $r = call_user_func($this->filter[$i],$r); + $exit = $this->filter[$i]->filter($r); + + } elseif (is_string($this->filter[$i])) { + // call a function. + $exit = $this->filter[$i]($r); + + } elseif (is_array($this->filter[$i]) + && is_callable($this->filter[$i])) { + // call a user class/method etc.. + $exit = call_user_func($this->filter[$i], $r); } // technically if this filter didnt get used.. it's an error condition!! - if (!is_string($r)) { + if (!is_string($r) || $exit === false) { break; } } - // prevent recursive calling! + // Done applying filters $this->template->options['filter.enabled'] = true; return $r; } @@ -162,14 +165,14 @@ * Loads the required filters into $this->filters.. * * @interface - * @return string|PEAR_Error + * @return bool TRUE or FALSE on error * @access public */ function _loadFilters() { // already loaded? if ($this->filters) { - return; + return true; } foreach (array_keys($this->template->options['filter.sources']) as $k) { if (!is_int($k)) { @@ -211,27 +214,31 @@ } } // error condition!!! - $this->template->raiseError( + return $this->template->raiseError( 'Filter::_loadFilter() unknown filter type %s', TEMPLATE_ERROR_INVALIDARGS, TEMPLATE_ERROR_DIE, - array(print_r($v,true)) + array(print_r($v, true)) ); } // we should have all the stuff loaded into filters now.!! - + return true; } /** - * Performs the actual filtering. + * Performs the actual filtering + * + * To be implemented by individual filters. The filter modifies + * the parameter passed. * * @interface - * @return string|PEAR_Error + * @param string $string a reference to a string to filter + * @return bool TRUE or FALSE on error * @access public */ - function filter($string) + function filter(&$string) { - return $string; + return true; } } http://cvs.php.net/diff.php/pear/Template/Template/Plugin.php?r1=1.2&r2=1.3&ty=u Index: pear/Template/Template/Plugin.php diff -u pear/Template/Template/Plugin.php:1.2 pear/Template/Template/Plugin.php:1.3 --- pear/Template/Template/Plugin.php:1.2 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Plugin.php Sun Jul 4 20:37:01 2004 @@ -16,7 +16,7 @@ // | Authors: nobody <nobody@localhost> | // +----------------------------------------------------------------------+ // -// $Id: Plugin.php,v 1.2 2004/07/04 18:23:09 jausions Exp $ +// $Id: Plugin.php,v 1.3 2004/07/05 00:37:01 jausions Exp $ // // Plugin API provides support for < ? = $this->plugin(".....",.....); ? > // or {this.plugin(#xxxxx#,#xxxx#):h} @@ -32,42 +32,39 @@ class Template_Plugin { /** - * reference to main engine.. - * - * @var object Template - * @access public - */ + * reference to main engine.. + * + * @var Template + * @access public + */ var $template; var $pluginCache = array(); // store of instanced plugins.. - /** - * Options used by the Plugin Provider - * - * currently only plugin.sources (a list of the plugin objects to be loaded) - * when searching for plugin methods. - * - * - * - * @var array - * @access public - */ + /** + * Options used by the Plugin Provider + * + * currently only plugin.sources (a list of the plugin objects to be loaded) + * when searching for plugin methods. + * + * @var array + * @access public + */ var $options = array( 'plugin.sources' => array( 'Savant', // default to using savant plugin Provider // from Template/Plugin/Savant.php // 'MyPlugin' => '/source/of/myPlugin.php' )); + /** - * instances of plugin objects. - * - * @var array Template_Plugin's - * @access public - */ + * instances of plugin objects. + * + * @var array Template_Plugin's + * @access public + */ var $plugins = array(); - - function initProvider(&$template) { // if you need to access template variables (Eg. options/data/providers) @@ -80,18 +77,18 @@ return true; } + /** - * Call a Plugin method. - * - * Look up in all the plugins to see if the method exists, if it does, call it. - * - * - * @param array name of method, arguments. - * - * - * @return string hopefully - * @access public - */ + * Call a Plugin method. + * + * Look up in all the plugins to see if the method exists, if it does, call it. + * + * + * @param array name of method, arguments. + * + * @return string hopefully + * @access public + */ function call($args) { @@ -99,30 +96,27 @@ // attempt to load the plugin on-the-fly $class = $this->_loadPlugins($method); - if (is_a($class,'PEAR_Error')) { + if (is_a($class, 'PEAR_Error')) { //echo $class->toString(); return $class; } - // first argument is always the plugin name; shift the first // argument off the front of the array and reduce the number of // array elements. array_shift($args); - return call_user_func_array(array(&$this->plugins[$class],$method), $args); + return call_user_func_array(array(&$this->plugins[$class], $method), $args); } /** - * Load the plugins, and lookup which one provides the required method - * - * - * @param string Name - * - * @return string|PEAR_Error the class that provides it. - * @access private - */ - + * Load the plugins, and lookup which one provides the required method + * + * @param string Name + * + * @return string|bool the class that provides it. + * @access private + */ function _findPlugin($name) { // name can be: @@ -158,6 +152,6 @@ return Template::raiseError('could not find plugin with method '. $name); } +} - -} \ No newline at end of file +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Template/Template/Source.php?r1=1.3&r2=1.4&ty=u Index: pear/Template/Template/Source.php diff -u pear/Template/Template/Source.php:1.3 pear/Template/Template/Source.php:1.4 --- pear/Template/Template/Source.php:1.3 Sun Jul 4 14:23:09 2004 +++ pear/Template/Template/Source.php Sun Jul 4 20:37:01 2004 @@ -16,7 +16,7 @@ // | Authors: alan@akkbhome.com | // +----------------------------------------------------------------------+ // -// $Id: Source.php,v 1.3 2004/07/04 18:23:09 jausions Exp $ +// $Id: Source.php,v 1.4 2004/07/05 00:37:01 jausions Exp $ // /** @@ -54,9 +54,9 @@ /** * Initializes this provider. * - * @param object Template The instantated template. + * @param $template Template the instantated template. * - * @return boolean (it may return a PEAR::Error) + * @return boolean TRUE or FALSE on error * @access public * @see Template_Provider */ @@ -82,33 +82,27 @@ * 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 + * @return boolean TRUE or FALSE on error + * @access public */ function open($file) { - switch (get_type($file)) { + switch (gettype($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); - - + // 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; @@ -173,9 +167,11 @@ $files['current_template_used_file'] = $newfile; } } + return true; } - function resolveMultiPath() { + function resolveMultiPath() + { // look in all the posible locations for the template directory.. if ($files['currentTemplate'] === false) { $dirs = array_unique($options['template_dir']); @@ -199,6 +195,7 @@ $files['current_template_from_dir'] = $tmplDir; } } + return true; } http://cvs.php.net/diff.php/pear/Template/Template/Filter/ColourizeCode.php?r1=1.1&r2=1.2&ty=u Index: pear/Template/Template/Filter/ColourizeCode.php diff -u pear/Template/Template/Filter/ColourizeCode.php:1.1 pear/Template/Template/Filter/ColourizeCode.php:1.2 --- pear/Template/Template/Filter/ColourizeCode.php:1.1 Fri Jun 25 08:51:30 2004 +++ pear/Template/Template/Filter/ColourizeCode.php Sun Jul 4 20:37:01 2004 @@ -1,100 +1,107 @@ <?php /** -* +* * Colorizes all text between <code>...</code> tags with PHP's * highlight_string function. Additionally, this will convert HTML * entities within <code> blocks, so you can demonstrate HTML tags * without them being honored in the browser. Also converts tabs to four * spaces. -* +* * To mark the beginning of php code in a <code> block, use the custom * tag <php>, and use </php> to mark the end. You can't use the normal * php code tags because Savant will try to execute that code when the * template runs. -* +* * @author Paul M. Jones <pmjones@ciaweb.net> -* -* @version $Id: ColourizeCode.php,v 1.1 2004/06/25 12:51:30 alan_k Exp $ -* +* +* @version $Id: ColourizeCode.php,v 1.2 2004/07/05 00:37:01 jausions Exp $ +* */ - + +/** + * @package Template + * @subpackage Template_Filter[ColorizeCode] + */ class Template_Filter_ColorizeCode extends Template_Filter { /** - * do the filtering - * - * - * @param string input string - * - * @return string|PEAR_Error - * @access public - * @see Template_Filter::filter() - */ - + * Colorizes PHP code + * + * @param string $text input string + * + * @return string|bool the filtered result or FALSE on error + * @access public + * @see Template_Filter::filter() + * @see highlight_string() + */ function filter(&$text) { // break the source into blocks by the beginning <code> tag. // this will remove the text "<code>" from the block, so // we'll need to add it in again later. $blocks = explode('<code>', $text); - + // loop through each block and convert text within // <code></code> tags. foreach ($blocks as $key => $val) { - + // now find then the ending </code> within the block $pos = strpos($val, '</code>'); - + if ($pos === false) { - + // there was no </code> tag -- do nothing - + } else { - + // change all <php> and </php> tags $val = str_replace('<php>', '<?php', $val); $val = str_replace('</php>', '?>', $val); // <?php - + // $tmp[0] will be the part before </code>, and // thus the part we want to modify. - // + // // $tmp[1] will be the part after the // <code></code> block, which we will leave // alone. - // + // // this will remove the text "</code>" from the // text, so we'll need to add it in again when modifying // the text. $tmp = explode('</code>', $val); - + // set entities by highlighting the string. we do the // output buffering ob() thing because the native // highlight_string() dumps the output to the screen // instead of returning to a variable (before PHP // 4.2.2). $tmp[0] = trim($tmp[0]); - + ob_start(); highlight_string($tmp[0]); $tmp[0] = ob_get_contents(); ob_end_clean(); - + // remove break tags from the highlighted text $tmp[0] = str_replace("<br />", "\n", $tmp[0]); - + // convert tabs to 4-spaces and then // re-surround with <code> tags $tmp[0] = str_replace("\t", ' ', $tmp[0]); - + // save the modified text in the block $blocks[$key] = $tmp[0] . $tmp[1]; } - + } - + // reassemble the blocks $text = implode('', $blocks); + + return true; } -} \ No newline at end of file +} + +?> \ No newline at end of file http://cvs.php.net/diff.php/pear/Template/Template/Filter/TrimWhiteSpace.php?r1=1.1&r2=1.2&ty=u Index: pear/Template/Template/Filter/TrimWhiteSpace.php diff -u pear/Template/Template/Filter/TrimWhiteSpace.php:1.1 pear/Template/Template/Filter/TrimWhiteSpace.php:1.2 --- pear/Template/Template/Filter/TrimWhiteSpace.php:1.1 Fri Jun 25 08:51:30 2004 +++ pear/Template/Template/Filter/TrimWhiteSpace.php Sun Jul 4 20:37:01 2004 @@ -1,84 +1,85 @@ <?php /** -* -* Trim leading white space and blank lines from template source after it -* gets interpreted, cleaning up code and saving bandwidth. Does not -* affect <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br> -* -* @author Monte Ohrt <monte@ispi.net> -* -* @author Contributions from Lars Noschinski <lars@usenet.noschinski.de> -* -* @author Converted to a Savant filter by Paul M. Jones -* <pmjones@ciaweb.net> -* -* @version $Id: TrimWhiteSpace.php,v 1.1 2004/06/25 12:51:30 alan_k Exp $ -* -*/ + * + * Trim leading white space and blank lines from template source after it + * gets interpreted, cleaning up code and saving bandwidth. Does not + * affect <<PRE>></PRE> and <SCRIPT></SCRIPT> blocks.<br> + * + * @author Monte Ohrt <monte@ispi.net> + * + * @author Contributions from Lars Noschinski <lars@usenet.noschinski.de> + * + * @author Converted to a Savant filter by Paul M. Jones + * <pmjones@ciaweb.net> + * + * @version $Id: TrimWhiteSpace.php,v 1.2 2004/07/05 00:37:01 jausions Exp $ + * + */ - +/** + * @package Template + * @subpackage Template_Filter[TrimWhiteSpace] + */ class Template_Filter_TrimWhiteSpace extends Template_Filter { /** - * Filter the string - * - * - * @param string input string - * - * @return string output string - * @access public - */ - - function filter(&$source) + * Trim white spaces from the string passed + * + * The filter modifies the value passed. + * + * @param string $text input string + * @return bool TRUE or FALSE on error + * @access public + */ + function filter(&$text) { // Pull out the script blocks - preg_match_all("!<script[^>]+>.*?</script>!is", $source, $match); + preg_match_all("!<script[^>]+>.*?</script>!is", $text, $match); $_script_blocks = $match[0]; - $source = preg_replace("!<script[^>]+>.*?</script>!is", - '@@@TEMPLATE:TRIM:SCRIPT@@@', $source); - + $text = preg_replace("!<script[^>]+>.*?</script>!is", + '@@@TEMPLATE:TRIM:SCRIPT@@@', $text); + // Pull out the pre blocks - preg_match_all("!<pre>.*?</pre>!is", $source, $match); + preg_match_all("!<pre>.*?</pre>!is", $text, $match); $_pre_blocks = $match[0]; - $source = preg_replace("!<pre>.*?</pre>!is", - '@@@TEMPLATE:TRIM:PRE@@@', $source); - + $text = preg_replace("!<pre>.*?</pre>!is", + '@@@TEMPLATE:TRIM:PRE@@@', $text); + // Pull out the textarea blocks - preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $source, $match); + preg_match_all("!<textarea[^>]+>.*?</textarea>!is", $text, $match); $_textarea_blocks = $match[0]; - $source = preg_replace("!<textarea[^>]+>.*?</textarea>!is", - '@@@TEMPLATE:TRIM:TEXTAREA@@@', $source); - + $text = preg_replace("!<textarea[^>]+>.*?</textarea>!is", + '@@@TEMPLATE:TRIM:TEXTAREA@@@', $text); + // remove all leading spaces, tabs and carriage returns NOT // preceeded by a php close tag. - $source = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $source)); - + $text = trim(preg_replace('/((?<!\?>)\n)[\s]+/m', '\1', $text)); + // replace script blocks - $this->_replace( "@@@TEMPLATE:TRIM:SCRIPT@@@",$_script_blocks, $source); - + $this->_replace( "@@@TEMPLATE:TRIM:SCRIPT@@@",$_script_blocks, $text); + // replace pre blocks - $this->_replace( "@@@TEMPLATE:TRIM:PRE@@@",$_pre_blocks, $source); - + $this->_replace( "@@@TEMPLATE:TRIM:PRE@@@",$_pre_blocks, $text); + // replace textarea blocks - $this->_replace( "@@@TEMPLATE:TRIM:TEXTAREA@@@",$_textarea_blocks, $source); - - return $source; + $this->_replace( "@@@TEMPLATE:TRIM:TEXTAREA@@@",$_textarea_blocks, $text); + + return true; } + /** - * put back the special elements that cant have space trimmed from them.. - * - * @param string matching string. - * @param array array of source matches - * @param string the full string to modify.. - * - * - * @return none - * @access private - */ - + * Puts back the special elements that cant have space trimmed from them.. + * + * @param string matching string. + * @param array array of source matches + * @param string the full string to modify.. + * + * @return void + * @access private + */ function _replace($search_str, $replace, &$subject) { $_len = strlen($search_str); @@ -91,6 +92,6 @@ } } } - } + ?> http://cvs.php.net/co.php/pear/Template/Template/Error.php?r=1.1&p=1 Index: pear/Template/Template/Error.php +++ pear/Template/Template/Error.php <?php /* vim: set expandtab tabstop=4 shiftwidth=4: */ // +----------------------------------------------------------------------+ // | PHP Version 4 | // +----------------------------------------------------------------------+ // | Copyright (c) 1997-2002 The PHP Group | // +----------------------------------------------------------------------+ // | This source file is subject to version 2.02 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/2_02.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: many... // +----------------------------------------------------------------------+ // // $Id: Error.php,v 1.1 2004/07/05 00:37:01 jausions Exp $ // // Provider for Assign API // /** * @package Template * @subpackage Template_Error */ class Template_Error { /** * Reference to the instantated template. * * @var Template * @access public */ var $template = null; /** * @var PEAR_ErrorStack * @access protected */ var $error_stack = null; /** * Initializes the Provider. * * @param Template the instantiated template engine * * @return bool TRUE on success, FALSE otherwise * @access public * @see Template_Provider */ 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; require_once 'PEAR/ErrorStack.php'; $this->error_stack =& PEAR_ErrorStack::singleton('Template'); return true; } /** * This may return a PEAR_Error/ErrorStack, or throw an exception! * * @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. * * @access public */ function raiseError($message, $type = null, $severity = TEMPLATE_ERROR_RETURN, $args = array()) { print_r($message); print_r($args); switch ($severity) { case TEMPLATE_ERROR_RETURN: $this->error_stack->push($type, 'error', $args, $message); break; case TEMPLATE_ERROR_DIE: $this->error_stack->push($type, 'error', $args, $message); exit; break; } return false; } } ?> http://cvs.php.net/co.php/pear/Template/tests/TemplateTest.php?r=1.1&p=1 Index: pear/Template/tests/TemplateTest.php +++ pear/Template/tests/TemplateTest.php <?php // Unit tests for Template package // class TemplateTest extends PHPUnit_TestCase { /** * To hold options for Template * @var array */ var $options; /** * Constructor * * @param string $name */ function __construct($name) { $this->PHPUnit_TestCase($name); } function setUp() { $this->options = array( 'template.dir' => dirname(__FILE__) . DIRECTORY_SEPARATOR . 'templates'); } /** * Test set data by name/value */ function testSetDataByNameValuePair() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $t = new Template($this->options); $t->open('simple_template.php'); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * Test set data by array */ function testSetDataByArray() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $array = array( 'value' => $value, 'arrayvalue' => $arrayvalue); $t = new Template($this->options); $t->open('simple_template.php'); $t->setData($array); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * Test set data by reference */ function testSetDataByReference() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); $t = new Template($this->options); $t->open('simple_template.php'); $t->setDataByRef('value', $value); $t->setDataByRef('arrayvalue', $arrayvalue); $value = 'CHANGED VALUE'; $arrayvalue[] = 'ADDED VALUE (INDEX 1)'; ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * toString() and output() methods should return the same thing */ function testOutputAndTostringMethodsReturnSameResult() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); $t = new Template($this->options); $t->open('simple_template.php'); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); ob_start(); $t->output(); $expected = ob_get_clean(); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * We should be able to setData() before opening templates */ function testSettingDataBeforeOpeningTemplateDoesNotMatter() { $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); ob_start(); include 'templates/simple_template.php'; $expected = ob_get_clean(); $t = new Template($this->options); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); $t->open('simple_template.php'); $result = $t->toString(); return $this->assertEquals($expected, $result); } /** * Testing clearData() */ function testTemplateDataAreEmptyAfterCleardataMethodIsCalled() { ob_start(); @include 'templates/simple_template.php'; $expected = ob_get_clean(); $value = 'TESTING'; $arrayvalue = array('INDEX 0\'s VALUE', 'STRING INDEX' => 'VALUE'); $t = new Template($this->options); $t->setData('value', $value); $t->setData('arrayvalue', $arrayvalue); $t->clearData(); $t->open('simple_template.php'); $result = $t->toString(); return $this->assertEquals($expected, $result); } } ?> http://cvs.php.net/co.php/pear/Template/tests/test.php?r=1.1&p=1 Index: pear/Template/tests/test.php +++ pear/Template/tests/test.php <?php // Unit tests for Image_Transform package // ini_set('include_path', dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . PATH_SEPARATOR . ini_get('include_path')); require_once 'PHPUnit.php'; require_once 'Template.php'; require_once 'PHP/Compat/Function/file_put_contents.php'; require_once 'PHP/Compat/Function/ob_get_clean.php'; require_once './TemplateTest.php'; $suite = new PHPUnit_TestSuite('TemplateTest'); $result =& PHPUnit::run($suite); echo $result->toString(); ?> http://cvs.php.net/co.php/pear/Template/tests/templates/simple_template.php?r=1.1&p=1 Index: pear/Template/tests/templates/simple_template.php +++ pear/Template/tests/templates/simple_template.php $value = <?php @var_dump($value); ?> $arrayvalue = <?php @var_dump($arrayvalue); ?>