2
0
Эх сурвалжийг харах

Fix serialization to include private properties, fixes #1727, fixes phpro/grumphp#1020

Jordi Boggiano 3 жил өмнө
parent
commit
4b4fad9476

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

@@ -48,6 +48,15 @@ abstract class Handler implements HandlerInterface
     {
         $this->close();
 
-        return array_keys(get_object_vars($this));
+        $reflClass = new \ReflectionClass($this);
+
+        $keys = [];
+        foreach ($reflClass->getProperties() as $reflProp) {
+            if (!$reflProp->isStatic()) {
+                $keys[] = $reflProp->getName();
+            }
+        }
+
+        return $keys;
     }
 }

+ 11 - 0
tests/Monolog/Handler/NullHandlerTest.php

@@ -30,4 +30,15 @@ class NullHandlerTest extends TestCase
         $handler = new NullHandler(Logger::WARNING);
         $this->assertFalse($handler->handle($this->getRecord(Logger::DEBUG)));
     }
+
+    public function testSerializeRestorePrivate()
+    {
+        $handler = new NullHandler(Logger::WARNING);
+        self::assertFalse($handler->handle($this->getRecord(Logger::DEBUG)));
+        self::assertTrue($handler->handle($this->getRecord(Logger::WARNING)));
+
+        $handler = unserialize(serialize($handler));
+        self::assertFalse($handler->handle($this->getRecord(Logger::DEBUG)));
+        self::assertTrue($handler->handle($this->getRecord(Logger::WARNING)));
+    }
 }