Author: grobmeier
Date: Sun Aug 30 08:40:57 2009
New Revision: 809271

URL: http://svn.apache.org/viewvc?rev=809271&view=rev
Log:
refactored filter chain, added test

Modified:
    incubator/log4php/trunk/src/main/php/LoggerAppender.php
    incubator/log4php/trunk/src/main/php/LoggerFilter.php
    incubator/log4php/trunk/src/test/php/LoggerAppenderTest.php

Modified: incubator/log4php/trunk/src/main/php/LoggerAppender.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/LoggerAppender.php?rev=809271&r1=809270&r2=809271&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/LoggerAppender.php (original)
+++ incubator/log4php/trunk/src/main/php/LoggerAppender.php Sun Aug 30 08:40:57 
2009
@@ -34,7 +34,7 @@
         * The first filter in the filter chain
         * @var LoggerFilter
         */
-       protected $headFilter = null;
+       protected $filter = null;
                        
        /**
         * LoggerLayout for this appender. It can be null if appender has its 
own layout
@@ -48,12 +48,6 @@
        protected $name;
                   
        /**
-        * The last filter in the filter chain
-        * @var LoggerFilter
-        */
-       protected $tailFilter = null; 
-                  
-       /**
         * @var LoggerLevel There is no level threshold filtering by default.
         */
        protected $threshold = null;
@@ -70,7 +64,6 @@
         */
        public function __construct($name = '') {
                $this->name = $name;
-               $this->clearFilters();
        }
 
        /**
@@ -79,12 +72,10 @@
         * @param LoggerFilter $newFilter add a new LoggerFilter
         */
        public function addFilter($newFilter) {
-               if($this->headFilter === null) {
-                       $this->headFilter = $newFilter;
-                       $this->tailFilter = $this->headFilter;
+               if($this->filter === null) {
+                       $this->filter = $newFilter;
                } else {
-                       $this->tailFilter->next = $newFilter;
-                       $this->tailFilter = $this->tailFilter->next;
+                       $this->filter->addNext($newFilter);
                }
        }
        
@@ -93,10 +84,8 @@
         * @abstract
         */
        public function clearFilters() {
-               unset($this->headFilter);
-               unset($this->tailFilter);
-               $this->headFilter = null;
-               $this->tailFilter = null;
+               unset($this->filter);
+               $this->filter = null;
        }
 
        /**
@@ -105,7 +94,7 @@
         * @return LoggerFilter
         */
        public function getFilter() {
-               return $this->headFilter;
+               return $this->filter;
        } 
        
        /** 
@@ -114,7 +103,7 @@
         * @return LoggerFilter
         */
        public function getFirstFilter() {
-               return $this->headFilter;
+               return $this->filter;
        }
        
        

Modified: incubator/log4php/trunk/src/main/php/LoggerFilter.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/main/php/LoggerFilter.php?rev=809271&r1=809270&r2=809271&view=diff
==============================================================================
--- incubator/log4php/trunk/src/main/php/LoggerFilter.php (original)
+++ incubator/log4php/trunk/src/main/php/LoggerFilter.php Sun Aug 30 08:40:57 
2009
@@ -99,6 +99,25 @@
                return self::NEUTRAL;
        }
 
+       /**
+        * Adds a new filter to the filter chain this filter is a part of.
+        * If this filter has already and follow up filter, the param filter
+        * is passed on until it is the last filter in chain.
+        * 
+        * @param $filter - the filter to add to this chain
+        */
+       public function addNext($filter) {
+               if($this->next !== null) {
+                   $this->next->addNext($filter);
+               } else {
+                   $this->next = $filter;
+               }
+       }
+       
+       /**
+        * Returns the next filter in this chain
+        * @return the next filter
+        */
        public function getNext() {
                return $this->next;
        }

Modified: incubator/log4php/trunk/src/test/php/LoggerAppenderTest.php
URL: 
http://svn.apache.org/viewvc/incubator/log4php/trunk/src/test/php/LoggerAppenderTest.php?rev=809271&r1=809270&r2=809271&view=diff
==============================================================================
--- incubator/log4php/trunk/src/test/php/LoggerAppenderTest.php (original)
+++ incubator/log4php/trunk/src/test/php/LoggerAppenderTest.php Sun Aug 30 
08:40:57 2009
@@ -120,4 +120,23 @@
                $a = $appender->getThreshold();
                self::assertEquals($e, $a);
     }
+    
+     public function testSetFilter() {
+               $appender = new LoggerAppenderEcho("LoggerAppenderTest");
+               
+               $layout = new LoggerLayoutSimple();
+               $appender->setLayout($layout);
+               
+               $filter  = new LoggerFilterDenyAll();
+               $appender->addFilter($filter);
+               
+               $filter2  = new LoggerFilterLevelMatch();
+               $appender->addFilter($filter2);
+               
+               $first = $appender->getFilter();
+               self::assertEquals($first, $filter);
+               
+               $next = $first->getNext();
+               self::assertEquals($next, $filter2);
+    }
 }


Reply via email to