Просмотр исходного кода

Refactored the AbstractHandler to avoid having some dummy write() methods

Christophe Coevoet 14 лет назад
Родитель
Сommit
0defabb726

+ 0 - 43
src/Monolog/Handler/AbstractHandler.php

@@ -51,24 +51,6 @@ abstract class AbstractHandler implements HandlerInterface
         return $record['level'] >= $this->level;
     }
 
-    /**
-     * {@inheritdoc}
-     */
-    public function handle(array $record)
-    {
-        if ($record['level'] < $this->level) {
-            return false;
-        }
-
-        $record = $this->processRecord($record);
-
-        $record['message'] = $this->getFormatter()->format($record);
-
-        $this->write($record);
-
-        return false === $this->bubble;
-    }
-
     /**
      * {@inheritdoc}
      */
@@ -171,14 +153,6 @@ abstract class AbstractHandler implements HandlerInterface
         $this->close();
     }
 
-    /**
-     * Writes the record down to the log of the implementing handler
-     *
-     * @param array $record
-     * @return void
-     */
-    abstract protected function write(array $record);
-
     /**
      * Gets the default formatter.
      *
@@ -188,21 +162,4 @@ abstract class AbstractHandler implements HandlerInterface
     {
         return new LineFormatter();
     }
-
-    /**
-     * Processes a record.
-     *
-     * @param array $record
-     * @return array
-     */
-    protected function processRecord(array $record)
-    {
-        if ($this->processors) {
-            foreach ($this->processors as $processor) {
-                $record = call_user_func($processor, $record);
-            }
-        }
-
-        return $record;
-    }
 }

+ 80 - 0
src/Monolog/Handler/AbstractProcessingHandler.php

@@ -0,0 +1,80 @@
+<?php
+
+/*
+ * This file is part of the Monolog package.
+ *
+ * (c) Jordi Boggiano <j.boggiano@seld.be>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Monolog\Handler;
+
+use Monolog\Logger;
+use Monolog\Formatter\FormatterInterface;
+use Monolog\Formatter\LineFormatter;
+
+/**
+ * Base Handler class providing the Handler structure
+ *
+ * Classes extending it should (in most cases) only implement write($record)
+ *
+ * @author Jordi Boggiano <j.boggiano@seld.be>
+ * @author Christophe Coevoet <stof@notk.org>
+ */
+abstract class AbstractProcessingHandler extends AbstractHandler
+{
+    /**
+     * {@inheritdoc}
+     */
+    public function handle(array $record)
+    {
+        if ($record['level'] < $this->level) {
+            return false;
+        }
+
+        $record = $this->processRecord($record);
+
+        $record['message'] = $this->getFormatter()->format($record);
+
+        $this->write($record);
+
+        return false === $this->bubble;
+    }
+
+    /**
+     * {@inheritdoc}
+     */
+    public function handleBatch(array $records)
+    {
+        foreach ($records as $record) {
+            $this->handle($record);
+        }
+    }
+
+    /**
+     * Writes the record down to the log of the implementing handler
+     *
+     * @param array $record
+     * @return void
+     */
+    abstract protected function write(array $record);
+
+    /**
+     * Processes a record.
+     *
+     * @param array $record
+     * @return array
+     */
+    protected function processRecord(array $record)
+    {
+        if ($this->processors) {
+            foreach ($this->processors as $processor) {
+                $record = call_user_func($processor, $record);
+            }
+        }
+
+        return $record;
+    }
+}

+ 0 - 9
src/Monolog/Handler/BufferHandler.php

@@ -38,7 +38,6 @@ class BufferHandler extends AbstractHandler
         parent::__construct($level, $bubble);
         $this->handler = $handler;
         $this->bufferSize = $bufferSize;
-        $this->bubble = $bubble;
     }
 
     /**
@@ -65,12 +64,4 @@ class BufferHandler extends AbstractHandler
     {
         $this->handler->handleBatch($this->buffer);
     }
-
-    /**
-     * Implemented to comply with the AbstractHandler requirements. Can not be called.
-     */
-    protected function write(array $record)
-    {
-        throw new \BadMethodCallException('This method should not be called directly on the BufferHandler.');
-    }
 }

+ 8 - 8
src/Monolog/Handler/FingersCrossedHandler.php

@@ -47,6 +47,14 @@ class FingersCrossedHandler extends AbstractHandler
         $this->stopBuffering = $stopBuffering;
     }
 
+    /**
+     * {@inheritdoc}
+     */
+    public function isHandling(array $record)
+    {
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -84,12 +92,4 @@ class FingersCrossedHandler extends AbstractHandler
     {
         $this->buffering = true;
     }
-
-    /**
-     * Implemented to comply with the AbstractHandler requirements. Can not be called.
-     */
-    protected function write(array $record)
-    {
-        throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.');
-    }
 }

+ 1 - 1
src/Monolog/Handler/FirePHPHandler.php

@@ -19,7 +19,7 @@ use Monolog\Formatter\WildfireFormatter;
  *
  * @author Eric Clemmons (@ericclemmons) <eric@uxdriven.com>
  */
-class FirePHPHandler extends AbstractHandler
+class FirePHPHandler extends AbstractProcessingHandler
 {
     /**
      * WildFire JSON header message format

+ 10 - 10
src/Monolog/Handler/GroupHandler.php

@@ -23,7 +23,7 @@ class GroupHandler extends AbstractHandler
     protected $handlers;
 
     /**
-     * @param Array $handlers Array of Handlers or factory callbacks($record, $fingersCrossedHandler).
+     * @param array $handlers Array of Handlers.
      * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
      */
     public function __construct(array $handlers, $bubble = false)
@@ -31,7 +31,15 @@ class GroupHandler extends AbstractHandler
         $this->handlers = $handlers;
         $this->bubble = $bubble;
     }
-    
+
+    /**
+     * {@inheritdoc}
+     */
+    public function isHandling(array $record)
+    {
+        return true;
+    }
+
     /**
      * {@inheritdoc}
      */
@@ -52,12 +60,4 @@ class GroupHandler extends AbstractHandler
             $handler->handleBatch($records);
         }
     }
- 
-    /**
-     * Implemented to comply with the AbstractHandler requirements. Can not be called.
-     */
-    protected function write(array $record)
-    {
-        throw new \BadMethodCallException('This method should not be called directly on the GroupHandler.');
-    }
 }

+ 1 - 1
src/Monolog/Handler/MailHandler.php

@@ -16,7 +16,7 @@ namespace Monolog\Handler;
  *
  * @author Gyula Sallai
  */
-abstract class MailHandler extends AbstractHandler
+abstract class MailHandler extends AbstractProcessingHandler
 {
     /**
      * {@inheritdoc}

+ 0 - 7
src/Monolog/Handler/NullHandler.php

@@ -34,11 +34,4 @@ class NullHandler extends AbstractHandler
 
         return false === $this->bubble;
     }
-
-    /**
-     * {@inheritdoc}
-     */
-    protected function write(array $record)
-    {
-    }
 }

+ 1 - 1
src/Monolog/Handler/StreamHandler.php

@@ -21,7 +21,7 @@ use Monolog\Logger;
  *
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-class StreamHandler extends AbstractHandler
+class StreamHandler extends AbstractProcessingHandler
 {
     protected $stream;
     protected $url;

+ 1 - 1
src/Monolog/Handler/SyslogHandler.php

@@ -27,7 +27,7 @@ use Monolog\Logger;
  *
  * @author Sven Paulus <sven@karlsruhe.org>
  */
-class SyslogHandler extends AbstractHandler
+class SyslogHandler extends AbstractProcessingHandler
 {
     /**
      * Translates Monolog log levels to syslog log priorities.

+ 1 - 1
src/Monolog/Handler/TestHandler.php

@@ -20,7 +20,7 @@ use Monolog\Logger;
  *
  * @author Jordi Boggiano <j.boggiano@seld.be>
  */
-class TestHandler extends AbstractHandler
+class TestHandler extends AbstractProcessingHandler
 {
     protected $records;
     protected $recordsByLevel;