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

Bring sampling handler in line with other wrapper handlers

Jordi Boggiano 11 лет назад
Родитель
Сommit
8160b302a2

+ 29 - 32
src/Monolog/Handler/SamplingHandler.php

@@ -30,9 +30,9 @@ use Monolog\Formatter\FormatterInterface;
 class SamplingHandler extends AbstractHandler
 {
     /**
-     * @var HandlerInterface $delegate
+     * @var callable|HandlerInterface $handler
      */
-    protected $delegate;
+    protected $handler;
 
     /**
      * @var int $factor
@@ -40,50 +40,47 @@ class SamplingHandler extends AbstractHandler
     protected $factor;
 
     /**
-     * @param HandlerInterface $handler Wrapped handler
-     * @param int $factor Sample factor
+     * @param callable|HandlerInterface $handler Handler or factory callable($record, $fingersCrossedHandler).
+     * @param int                       $factor  Sample factor
      */
-    public function __construct(HandlerInterface $handler, $factor)
+    public function __construct($handler, $factor)
     {
         parent::__construct();
-        $this->delegate = $handler;
+        $this->handler = $handler;
         $this->factor = $factor;
     }
 
     public function isHandling(array $record)
     {
-        return $this->delegate->isHandling($record);
+        return $this->handler->isHandling($record);
     }
 
     public function handle(array $record)
     {
-        if ($this->isHandling($record)
-            && mt_rand(1, $this->factor) === 1)
-        {
-            return $this->delegate->handle($record);
-        }
-        return false;
-    }
+        if ($this->isHandling($record) && mt_rand(1, $this->factor) === 1) {
+            // The same logic as in FingersCrossedHandler
+            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);
+                if (!$this->handler instanceof HandlerInterface) {
+                    throw new \RuntimeException("The factory callable should return a HandlerInterface");
+                }
+            }
 
-    public function pushProcessor($callback)
-    {
-        $this->delegate->pushProcessor($callback);
-        return $this;
-    }
+            if ($this->processors) {
+                foreach ($this->processors as $processor) {
+                    $record = call_user_func($processor, $record);
+                }
+            }
 
-    public function popProcessor()
-    {
-        return $this->delegate->popProcessor();
-    }
-
-    public function setFormatter(FormatterInterface $formatter)
-    {
-        $this->delegate->setFormatter($formatter);
-        return $this;
-    }
+            $this->handler->handle($record);
+        }
 
-    public function getFormatter()
-    {
-        return $this->delegate->getFormatter();
+        return false === $this->bubble;
     }
 }

+ 1 - 2
tests/Monolog/Handler/SamplingHandlerTest.php

@@ -23,8 +23,7 @@ class SamplingHandlerTest extends TestCase
     {
         $testHandler = new TestHandler();
         $handler = new SamplingHandler($testHandler, 2);
-        for ($i=0; $i<10000; $i++)
-        {
+        for ($i = 0; $i < 10000; $i++) {
             $handler->handle($this->getRecord());
         }
         $count = count($testHandler->getRecords());