1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-08-06 13:16:39 +02:00

Refactored header creation to use $key => $value pairs for easier support for 3rd-party Response objects

This commit is contained in:
Eric Clemmons
2011-04-23 17:52:32 -07:00
parent 6f2c1682a7
commit 275084d5d7
2 changed files with 31 additions and 35 deletions

View File

@@ -55,14 +55,14 @@ class FirePHPHandler extends AbstractHandler
/**
* Function, Method or Closure for sending the header
*/
private $writer = 'header';
private $writer;
/**
* @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 = 'header')
public function __construct($level = Logger::DEBUG, $bubble = false, $writer = null)
{
$this->level = $level;
$this->bubble = $bubble;
@@ -78,12 +78,9 @@ class FirePHPHandler extends AbstractHandler
*/
protected function createHeader(Array $meta, $message)
{
return sprintf(
'%s-%s: %s',
$this->prefix,
join('-', $meta),
$message
);
$header = sprintf('%s-%s', $this->prefix, join('-', $meta));
return array($header => $message);
}
/**
@@ -117,10 +114,10 @@ class FirePHPHandler extends AbstractHandler
protected function getInitHeaders()
{
// Initial payload consists of required headers for Wildfire
return array(
return array_merge(
$this->createHeader(array('Protocol', 1), self::PROTOCOL_URI),
$this->createHeader(array(1, 'Structure', 1), self::STRUCTURE_URI),
$this->createHeader(array(1, 'Plugin', 1), self::PLUGIN_URI),
$this->createHeader(array(1, 'Plugin', 1), self::PLUGIN_URI)
);
}
@@ -128,23 +125,20 @@ 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
*/
protected function sendHeader($header)
protected function sendHeader($header, $content)
{
if (headers_sent()) {
return false;
} else if ($writer = $this->getWriter()) {
call_user_func_array($writer, array($header, $content));
} else {
$writer = $this->getWriter();
if ($writer instanceof \Closure) {
$writer($header);
} else {
call_user_func($writer, $header);
}
return true;
header(sprintf('%s: %s', $header, $content));
}
return true;
}
/**
@@ -158,15 +152,16 @@ class FirePHPHandler extends AbstractHandler
{
// WildFire-specific headers must be sent prior to any messages
if (! $this->initialized) {
foreach ($this->getInitHeaders() as $header) {
$this->sendHeader($header);
foreach ($this->getInitHeaders() as $header => $content) {
$this->sendHeader($header, $content);
}
$this->initialized = true;
}
$header = $this->createRecordHeader($record);
$this->sendHeader($header);
foreach ($this->createRecordHeader($record) as $header => $content) {
$this->sendHeader($header, $content);
}
}
/**

View File

@@ -28,9 +28,9 @@ class FirePHPHandlerTest extends TestCase
/**
* @dataProvider handlerProvider
*/
public function testDefaultWriterIsClosure($handler)
public function testDefaultWriterIsNull($handler)
{
$this->assertEquals('header', $handler->getWriter());
$this->assertEquals(null, $handler->getWriter());
}
public function testConstructWithWriter()
@@ -62,16 +62,17 @@ class FirePHPHandlerTest extends TestCase
$handler->handle($this->getRecord(Logger::DEBUG));
}
public function writerForTestMethodWriter($header)
public function writerForTestMethodWriter($header, $content)
{
$valid = 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-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 "]|',
);
$this->assertTrue(in_array($header, $valid));
$this->assertTrue(array_key_exists($header, $valid));
$this->assertEquals($valid[$header], $content);
}
public function testClosureWriter()
@@ -79,14 +80,14 @@ class FirePHPHandlerTest extends TestCase
$headers = array();
$handler = new FirePHPHandler;
$handler->setWriter(function($header) use (&$headers) {
$headers[] = $header;
$handler->setWriter(function($header, $content) use (&$headers) {
$headers[$header] = $content;
});
$handler->handle($this->getRecord(Logger::DEBUG));
$this->assertEquals(
'X-Wf-1-1-1-5: 50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
'50|[{"Type":"LOG","File":"","Line":""},"test: test "]|',
end($headers)
);