Jelajahi Sumber

Make $initialized static
Removed custom writer, overriding is easy enough
CS fixes
Added TestFirePHPHandler class to enable testing of headers

Jordi Boggiano 14 tahun lalu
induk
melakukan
2b1c68e0d0

+ 3 - 4
src/Monolog/Formatter/WildfireFormatter.php

@@ -20,7 +20,6 @@ use Monolog\Logger;
  */
 class WildfireFormatter extends LineFormatter implements FormatterInterface
 {
-
     /**
      * Similar to LineFormatter::SIMPLE_FORMAT, except without the "[%datetime%]"
      */
@@ -48,7 +47,7 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
     {
         // Format record according with LineFormatter
         $formatted = parent::format($record);
-        
+
         // Create JSON object describing the appearance of the message in the console
         $json = json_encode(array(
             array(
@@ -58,14 +57,14 @@ class WildfireFormatter extends LineFormatter implements FormatterInterface
             ),
             $formatted['message'],
         ));
-        
+
         // The message itself is a serialization of the above JSON object + it's length
         $formatted['message'] = sprintf(
             '%s|%s|',
             strlen($json),
             $json
         );
-        
+
         return $formatted;
     }
 

+ 26 - 58
src/Monolog/Handler/FirePHPHandler.php

@@ -21,7 +21,6 @@ use Monolog\Formatter\WildfireFormatter;
  */
 class FirePHPHandler extends AbstractHandler
 {
-
     /**
      * WildFire JSON header message format
      */
@@ -37,59 +36,53 @@ class FirePHPHandler extends AbstractHandler
      */
     const PLUGIN_URI = 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2';
 
-    /**
-     * Whether or not Wildfire vendor-specific headers have been generated & sent yet
-     */
-    private $initialized = false;
-
     /**
      * Header prefix for Wildfire to recognize & parse headers
      */
-    private $prefix = 'X-Wf';
+    const HEADER_PREFIX = 'X-Wf';
 
     /**
-     * Shared static message index between potentially multiple handlers
+     * Whether or not Wildfire vendor-specific headers have been generated & sent yet
      */
-    private static $messageIndex = 1;
+    protected static $initialized = false;
 
     /**
-     * Function, Method or Closure for sending the header
+     * Shared static message index between potentially multiple handlers
+     * @var int
      */
-    private $writer;
+    protected static $messageIndex = 1;
 
     /**
      * @param integer $level  The minimum logging level at which this handler will be triggered
      * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
-     * @param mixed   $writer Function, Method or Closure to use for sending headers
      */
-    public function __construct($level = Logger::DEBUG, $bubble = false, $writer = null)
+    public function __construct($level = Logger::DEBUG, $bubble = false)
     {
         $this->level = $level;
         $this->bubble = $bubble;
-        $this->writer = $writer;
     }
 
     /**
      * Base header creation function used by init headers & record headers
      *
-     * @var Array $meta Wildfire Plugin, Protocol & Structure Indexes
-     * @var String $message Log message
-     * @return String Complete header string ready for the client
+     * @param array $meta Wildfire Plugin, Protocol & Structure Indexes
+     * @param string $message Log message
+     * @return string Complete header string ready for the client
      */
-    protected function createHeader(Array $meta, $message)
+    protected function createHeader(array $meta, $message)
     {
-        $header = sprintf('%s-%s', $this->prefix, join('-', $meta));
-        
+        $header = sprintf('%s-%s', self::HEADER_PREFIX, join('-', $meta));
+
         return array($header => $message);
     }
 
     /**
      * Creates message header from record
-     * 
+     *
      * @see createHeader()
-     * @var Array $record
+     * @param array $record
      */
-    protected function createRecordHeader(Array $record)
+    protected function createRecordHeader(array $record)
     {
         // Wildfire is extensible to support multiple protocols & plugins in a single request,
         // but we're not taking advantage of that (yet), so we're using "1" for simplicity's sake.
@@ -124,21 +117,14 @@ class FirePHPHandler extends AbstractHandler
     /**
      * Send header string to the client
      *
-     * @var String $header
-     * @var String $content
-     * @return Boolean False if headers are already sent, true if header are sent successfully
+     * @param string $header
+     * @param string $content
      */
     protected function sendHeader($header, $content)
     {
-        if (headers_sent()) {
-            return false;
-        } else if ($writer = $this->getWriter()) {
-                call_user_func_array($writer, array($header, $content));
-        } else {
+        if (!headers_sent()) {
             header(sprintf('%s: %s', $header, $content));
         }
-        
-        return true;
     }
 
     /**
@@ -146,38 +132,20 @@ class FirePHPHandler extends AbstractHandler
      *
      * @see sendHeader()
      * @see sendInitHeaders()
-     * @var Array $record
+     * @param array $record
      */
-    protected function write(Array $record)
+    protected function write(array $record)
     {
         // WildFire-specific headers must be sent prior to any messages
-        if (! $this->initialized) {
+        if (!self::$initialized) {
             foreach ($this->getInitHeaders() as $header => $content) {
                 $this->sendHeader($header, $content);
             }
-            
-            $this->initialized = true;
-        }
-        
-        foreach ($this->createRecordHeader($record) as $header => $content) {
-            $this->sendHeader($header, $content);
-        }
-    }
 
-    /**
-     * @return mixed Writer used for sending headers
-     */
-    public function getWriter()
-    {
-        return $this->writer;
-    }
+            self::$initialized = true;
+        }
 
-    /**
-     * @var mixed Function, Method or Closure to use for sending headers
-     */
-    public function setWriter($writer)
-    {
-        $this->writer = $writer;
+        $header = $this->createRecordHeader($record);
+        $this->sendHeader(key($header), current($header));
     }
-
 }

+ 48 - 70
tests/Monolog/Handler/FirePHPHandlerTest.php

@@ -16,95 +16,73 @@ use Monolog\Logger;
 
 class FirePHPHandlerTest extends TestCase
 {
-
-    /**
-     * @dataProvider handlerProvider
-     */
-    public function testCloseReturnsHeadersSent($handler)
+    public function setUp()
     {
-        $this->assertEquals(headers_sent(), $handler->close());
+        TestFirePHPHandler::reset();
     }
 
-    /**
-     * @dataProvider handlerProvider
-     */
-    public function testDefaultWriterIsNull($handler)
+    public function testHeaders()
     {
-        $this->assertEquals(null, $handler->getWriter());
-    }
+        $handler = new TestFirePHPHandler;
+        $handler->handle($this->getRecord(Logger::DEBUG));
+        $handler->handle($this->getRecord(Logger::WARNING));
 
-    public function testConstructWithWriter()
-    {
-        $writer = array($this, 'testWriter');
-        
-        $handler = new FirePHPHandler(Logger::DEBUG, false, $writer);
-        
-        $this->assertEquals($writer, $handler->getWriter());
-    }
+        $expected = array(
+            'X-Wf-Protocol-1'    => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2',
+            'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1',
+            'X-Wf-1-Plugin-1'    => 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2',
+            'X-Wf-1-1-1-1'       => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
+            'X-Wf-1-1-1-2'       => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|',
+        );
 
-    /**
-     * @dataProvider handlerProvider
-     */
-    public function testWriterIsSettable($handler)
-    {
-        $writer = array($this, 'testWriter');
-        $handler->setWriter($writer);
-        
-        $this->assertNotEquals('header', $handler->getWriter());
-        $this->assertEquals($writer, $handler->getWriter());
+        $this->assertEquals($expected, $handler->getHeaders());
     }
 
-    public function testMethodWriter()
+    public function testConcurrentHandlers()
     {
-        $handler = new FirePHPHandler;
-        $handler->setWriter(array($this, 'writerForTestMethodWriter'));
-        
+        $handler = new TestFirePHPHandler;
         $handler->handle($this->getRecord(Logger::DEBUG));
-    }
+        $handler->handle($this->getRecord(Logger::WARNING));
 
-    public function writerForTestMethodWriter($header, $content)
-    {
-        $valid = array(
+        $handler2 = new TestFirePHPHandler;
+        $handler2->handle($this->getRecord(Logger::DEBUG));
+        $handler2->handle($this->getRecord(Logger::WARNING));
+
+        $expected = array(
             'X-Wf-Protocol-1'    => 'http://meta.wildfirehq.org/Protocol/JsonStream/0.2',
             'X-Wf-1-Structure-1' => 'http://meta.firephp.org/Wildfire/Structure/FirePHP/FirebugConsole/0.1',
             'X-Wf-1-Plugin-1'    => 'http://meta.firephp.org/Wildfire/Plugin/ZendFramework/FirePHP/1.6.2',
-            'X-Wf-1-1-1-5'       => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
+            'X-Wf-1-1-1-1'       => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
+            'X-Wf-1-1-1-2'       => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|',
         );
-        
-        $this->assertTrue(array_key_exists($header, $valid));
-        $this->assertEquals($valid[$header], $content);
+
+        $expected2 = array(
+            'X-Wf-1-1-1-3'       => '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
+            'X-Wf-1-1-1-4'       => '51|[{"Type":"WARN","File":"","Line":""},"test: test "]|',
+        );
+
+        $this->assertEquals($expected, $handler->getHeaders());
+        $this->assertEquals($expected2, $handler2->getHeaders());
     }
+}
+
+class TestFirePHPHandler extends FirePHPHandler
+{
+    protected $headers = array();
 
-    public function testClosureWriter()
+    public static function reset()
     {
-        $headers = array();
-        
-        $handler = new FirePHPHandler;
-        $handler->setWriter(function($header, $content) use (&$headers) {
-            $headers[$header] = $content;
-        });
-        
-        $handler->handle($this->getRecord(Logger::DEBUG));
-        
-        $this->assertEquals(
-            '50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
-            end($headers)
-        );
-        
-        $this->assertEquals(4, count($headers), "There should be 3 init headers & 1 message header");
+        self::$initialized = false;
+        self::$messageIndex = 1;
     }
 
-    public function handlerProvider()
+    protected function sendHeader($header, $content)
     {
-        $handler = new FirePHPHandler();
-        
-        $handler->handle($this->getRecord(Logger::DEBUG));
-        $handler->handle($this->getRecord(Logger::DEBUG));
-        $handler->handle($this->getRecord(Logger::INFO));
-        $handler->handle($this->getRecord(Logger::WARNING));
-        
-        return array(
-            array($handler),
-        );
+        $this->headers[$header] = $content;
     }
-}
+
+    public function getHeaders()
+    {
+        return $this->headers;
+    }
+}