cvs: pear /Template/Template Filter.php
| From: | Alan Knowles | Date: | Fri, 25 Jun 2004 12:42:33 +0000 |
| Subject: | cvs: pear /Template/Template Filter.php | ||
| Groups: | php.pear.cvs | ||
| Request: | Send a blank email to pear-cvs+get-21110@lists.php.net to get a copy of this message | ||
alan_k Fri Jun 25 08:42:33 2004 EDT
Modified files:
/pear/Template/Template Filter.php
Log:
provide a multitude of ways to use filters = try and standardize on option name matching classname
(eg. filter. == Template_Filter)
http://cvs.php.net/diff.php/pear/Template/Template/Filter.php?r1=1.1&r2=1.2&ty=u
Index: pear/Template/Template/Filter.php
diff -u pear/Template/Template/Filter.php:1.1 pear/Template/Template/Filter.php:1.2
--- pear/Template/Template/Filter.php:1.1 Wed Jun 16 20:36:21 2004
+++ pear/Template/Template/Filter.php Fri Jun 25 08:42:33 2004
@@ -16,7 +16,7 @@
// | Authors: Philippe Jausions <Philippe.Jausions -@- 11abacus.com> |
// +----------------------------------------------------------------------+
//
-// $Id: Filter.php,v 1.1 2004/06/17 00:36:21 jausions Exp $
+// $Id: Filter.php,v 1.2 2004/06/25 12:42:33 alan_k Exp $
//
// Filter provider.
//
@@ -32,6 +32,26 @@
**/
var $template = null;
+
+
+ var $options = array(
+ 'filter.enabled' => false, // flag to prevent recursive filtering
+ 'filter.sources' => array(
+ // 'ColorizeCode', (built in)
+ // 'TrimWhiteSpace', (built in)
+ // 'MyClass' => '/path/to/filter', // load from..
+ // 'MyClass' => '', // just use the class
(assume it's loaded)
+
+ // array('someclass','somemethod'), // call_user_func
+ // array($this,'somemethod'), // call_user_func
+ // array('myfunction') // call_user_func // use this
function
+
+ // $object, // object that implements Filter.
+
+ ),
+ );
+
+
/**
* A reference to the list of filters
*
@@ -66,11 +86,6 @@
// 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;
}
@@ -96,21 +111,121 @@
* @return bool|PEAR_Error TRUE or PEAR_Error object on error
**/
function toString() {
- if (!@$this->template->options['filters.enabled']) {
+ // if we are already filtering (or filtering is not enabled...
+ // dont do anything
+ if (!@$this->template->options['filter.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;
- }
+ // load the filters if necessary
+ $this->_loadFilters();
+ // dont recurse!
+ $this->template->options['filter.enabled'] = false;
+
+ // get the template result as a string
+ if (!is_string($r = $this->template->toString())) {
+ return $r;
+ }
+ // now apply all the requested filters.
+ 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);
+ }
+ // technically if this filter didnt get used.. it's an error condition!!
+
+ if (!is_string($r)) {
+ break;
}
}
- $this->template->options['filters.enabled'] = true;
+
+ // prevent recursive calling!
+ $this->template->options['filter.enabled'] = true;
return $r;
}
-
+ /**
+ * loads the required filters into $this->filters..
+ *
+ * @interface
+ * @access public
+ * @return string|PEAR_Error
+ **/
+ function _loadFilters()
+ {
+ // already loaded?
+ if ($this->filters) {
+ return;
+ }
+ foreach(array_keys($this->template->options['filter.sources']) as $k) {
+ if (!is_int($k)) {
+ // then it's classname => filename
+ $filename = $this->template->options['filter.sources'][$k];
+ include_once $filename;
+ $this->filters[$classname] = new $k;
+ // ??? use initProvider????
+ $this->filters[$classname]->initProvider($this->template);
+ continue;
+ }
+ $v = &$this->template->options['filter.sources'][$k];
+ // standard provider..
+ if (is_string($v)) {
+ $this->template->assertProvider('Filter/'.$v);
+ $this->filters[$v] =
&$this->template->providers['Filter/'.$v];
+ continue;
+ }
+ // an object..
+ if (is_object($v)) {
+ $this->filters[get_class($v)] =
&$this->template->options['filter.sources'][$k];
+ continue;
+ }
+
+ // an array
+ if (is_array($v)) {
+ if (count($v) == 1) {
+ $this->filters['::'.$v[0]] = $v[0];
+ continue;
+ }
+ if (is_string($v[0])) {
+ $this->filters[$v[0].'::'.$v[1]] = $v;
+ continue;
+ }
+ if (is_object($v[0])) {
+ $this->filters[get_class_name($v[0]) .'::'.$v[1]] = $v;
+ continue;
+ }
+ }
+ // error condition!!!
+ $this->template->raiseError(
+ 'Filter::_loadFilter() unknown filter type %s',
+ TEMPLATE_ERROR_INVALIDARGS,
+ TEMPLATE_ERROR_DIE,
+ array(print_r($v,true))
+ );
+ }
+ // we should have all the stuff loaded into filters now.!!
+
+ }
+
+ /**
+ * perform the actual filtering.
+ *
+ * @interface
+ * @access public
+ * @return string|PEAR_Error
+ **/
+
+ function filter($string)
+ {
+ return $string;
+ }
+
}
?>