Преглед на файлове

Move handler checks in constructor

Jordi Boggiano преди 11 години
родител
ревизия
9b350bb1fd
променени са 3 файла, в които са добавени 12 реда и са изтрити 15 реда
  1. 4 6
      src/Monolog/Handler/FilterHandler.php
  2. 4 3
      src/Monolog/Handler/FingersCrossedHandler.php
  3. 4 6
      src/Monolog/Handler/SamplingHandler.php

+ 4 - 6
src/Monolog/Handler/FilterHandler.php

@@ -55,6 +55,10 @@ class FilterHandler extends AbstractHandler
         $this->handler  = $handler;
         $this->handler  = $handler;
         $this->bubble   = $bubble;
         $this->bubble   = $bubble;
         $this->setAcceptedLevels($minLevelOrList, $maxLevel);
         $this->setAcceptedLevels($minLevelOrList, $maxLevel);
+
+        if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
+            throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
+        }
     }
     }
 
 
     /**
     /**
@@ -102,12 +106,6 @@ class FilterHandler extends AbstractHandler
 
 
         // The same logic as in FingersCrossedHandler
         // The same logic as in FingersCrossedHandler
         if (!$this->handler instanceof HandlerInterface) {
         if (!$this->handler instanceof HandlerInterface) {
-            if (!is_callable($this->handler)) {
-                throw new \RuntimeException(
-                    "The given handler (" . json_encode($this->handler)
-                    . ") is not a callable nor a Monolog\\Handler\\HandlerInterface object"
-                );
-            }
             $this->handler = call_user_func($this->handler, $record, $this);
             $this->handler = call_user_func($this->handler, $record, $this);
             if (!$this->handler instanceof HandlerInterface) {
             if (!$this->handler instanceof HandlerInterface) {
                 throw new \RuntimeException("The factory callable should return a HandlerInterface");
                 throw new \RuntimeException("The factory callable should return a HandlerInterface");

+ 4 - 3
src/Monolog/Handler/FingersCrossedHandler.php

@@ -62,6 +62,10 @@ class FingersCrossedHandler extends AbstractHandler
         $this->bubble = $bubble;
         $this->bubble = $bubble;
         $this->stopBuffering = $stopBuffering;
         $this->stopBuffering = $stopBuffering;
         $this->passthruLevel = $passthruLevel;
         $this->passthruLevel = $passthruLevel;
+
+        if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
+            throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
+        }
     }
     }
 
 
     /**
     /**
@@ -93,9 +97,6 @@ class FingersCrossedHandler extends AbstractHandler
                     $this->buffering = false;
                     $this->buffering = false;
                 }
                 }
                 if (!$this->handler instanceof HandlerInterface) {
                 if (!$this->handler instanceof HandlerInterface) {
-                    if (!is_callable($this->handler)) {
-                        throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
-                    }
                     $this->handler = call_user_func($this->handler, $record, $this);
                     $this->handler = call_user_func($this->handler, $record, $this);
                     if (!$this->handler instanceof HandlerInterface) {
                     if (!$this->handler instanceof HandlerInterface) {
                         throw new \RuntimeException("The factory callable should return a HandlerInterface");
                         throw new \RuntimeException("The factory callable should return a HandlerInterface");

+ 4 - 6
src/Monolog/Handler/SamplingHandler.php

@@ -48,6 +48,10 @@ class SamplingHandler extends AbstractHandler
         parent::__construct();
         parent::__construct();
         $this->handler = $handler;
         $this->handler = $handler;
         $this->factor = $factor;
         $this->factor = $factor;
+
+        if (!$this->handler instanceof HandlerInterface && !is_callable($this->handler)) {
+            throw new \RuntimeException("The given handler (".json_encode($this->handler).") is not a callable nor a Monolog\Handler\HandlerInterface object");
+        }
     }
     }
 
 
     public function isHandling(array $record)
     public function isHandling(array $record)
@@ -60,12 +64,6 @@ class SamplingHandler extends AbstractHandler
         if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
         if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
             // The same logic as in FingersCrossedHandler
             // The same logic as in FingersCrossedHandler
             if (!$this->handler instanceof HandlerInterface) {
             if (!$this->handler instanceof HandlerInterface) {
-                if (!is_callable($this->handler)) {
-                    throw new \RuntimeException(
-                        "The given handler (" . json_encode($this->handler)
-                        . ") is not a callable nor a Monolog\\Handler\\HandlerInterface object"
-                    );
-                }
                 $this->handler = call_user_func($this->handler, $record, $this);
                 $this->handler = call_user_func($this->handler, $record, $this);
                 if (!$this->handler instanceof HandlerInterface) {
                 if (!$this->handler instanceof HandlerInterface) {
                     throw new \RuntimeException("The factory callable should return a HandlerInterface");
                     throw new \RuntimeException("The factory callable should return a HandlerInterface");