1
0
mirror of https://github.com/maximebf/php-debugbar.git synced 2025-01-16 21:08:34 +01:00

Added abstraction for HTTP features (fixed #28)

This commit is contained in:
maximebf 2013-09-19 16:31:00 -04:00
parent ba75ec7e66
commit 7e81754ef6
6 changed files with 242 additions and 22 deletions

View File

@ -40,6 +40,8 @@ class DebugBar implements ArrayAccess
protected $storage;
protected $httpDriver;
protected $stackSessionNamespace = 'PHPDEBUGBAR_STACK_DATA';
protected $stackAlwaysUseSessionStorage = false;
@ -163,6 +165,32 @@ class DebugBar implements ArrayAccess
return $this->storage !== null;
}
/**
* Sets the HTTP driver
*
* @param HttpDriverInterface $driver
*/
public function setHttpDriver(HttpDriverInterface $driver)
{
$this->httpDriver = $driver;
return $this;
}
/**
* Returns the HTTP driver
*
* If no http driver where defined, a PhpHttpDriver is automatically created
*
* @return HttpDriverInterface
*/
public function getHttpDriver()
{
if ($this->httpDriver === null) {
$this->httpDriver = new PhpHttpDriver();
}
return $this->httpDriver;
}
/**
* Collects the data from the collectors
*
@ -226,10 +254,13 @@ class DebugBar implements ArrayAccess
}
$chunks[] = $data;
$headers = array();
for ($i = 0, $c = count($chunks); $i < $c; $i++) {
$name = $headerName . ($i > 0 ? "-$i" : '');
header("$name: {$chunks[$i]}");
$headers[$name] = $chunks[$i];
}
$this->getHttpDriver()->setHeaders($headers);
return $this;
}
@ -239,7 +270,7 @@ class DebugBar implements ArrayAccess
*/
public function stackData()
{
$this->initStackSession();
$http = $this->initStackSession();
$data = null;
if (!$this->isDataPersisted() || $this->stackAlwaysUseSessionStorage) {
@ -248,7 +279,9 @@ class DebugBar implements ArrayAccess
$this->collect();
}
$_SESSION[$this->stackSessionNamespace][$this->getCurrentRequestId()] = $data;
$stack = $http->getSessionValue($this->stackSessionNamespace);
$stack[$this->getCurrentRequestId()] = $data;
$http->setSessionValue($this->stackSessionNamespace, $stack);
return $this;
}
@ -259,8 +292,8 @@ class DebugBar implements ArrayAccess
*/
public function hasStackedData()
{
$this->initStackSession();
return count($_SESSION[$this->stackSessionNamespace]) > 0;
$http = $this->initStackSession();
return count($http->getSessionValue($this->stackSessionNamespace)) > 0;
}
/**
@ -271,10 +304,10 @@ class DebugBar implements ArrayAccess
*/
public function getStackedData($delete = true)
{
$this->initStackSession();
$stackedData = $_SESSION[$this->stackSessionNamespace];
$http = $this->initStackSession();
$stackedData = $http->getSessionValue($this->stackSessionNamespace);
if ($delete) {
unset($_SESSION[$this->stackSessionNamespace]);
$http->deleteSessionValue($this->stackSessionNamespace);
}
$datasets = array();
@ -335,17 +368,21 @@ class DebugBar implements ArrayAccess
/**
* Initializes the session for stacked data
*
* @return HttpDriverInterface
*/
protected function initStackSession()
{
if (!isset($_SESSION)) {
$http = $this->getHttpDriver();
if (!$http->isSessionStarted()) {
throw new DebugBarException("Session must be started before using stack data in the debug bar");
}
$ns = $this->stackSessionNamespace;
if (!isset($_SESSION[$ns])) {
$_SESSION[$ns] = array();
if (!$http->hasSessionValue($this->stackSessionNamespace)) {
$http->setSessionValue($this->stackSessionNamespace, array());
}
return $http;
}
/**

View File

@ -0,0 +1,63 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar;
/**
* Provides an abstraction of PHP native features for easier integration
* in third party frameworks
*/
interface HttpDriverInterface
{
/**
* Sets HTTP headers
*
* @param string $headers
*/
function setHeaders(array $headers);
/**
* Checks if the session is started
*
* @return boolean
*/
function isSessionStarted();
/**
* Sets a value in the session
*
* @param string $name
* @param string $value
*/
function setSessionValue($name, $value);
/**
* Checks if a value is in the session
*
* @param string $name
* @return boolean
*/
function hasSessionValue($name);
/**
* Returns a value from the session
*
* @param string $name
* @return mixed
*/
function getSessionValue($name);
/**
* Deletes a value from the session
*
* @param string $name
*/
function deleteSessionValue($name);
}

View File

@ -0,0 +1,67 @@
<?php
/*
* This file is part of the DebugBar package.
*
* (c) 2013 Maxime Bouroumeau-Fuseau
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace DebugBar;
/**
* HTTP driver for native php
*/
class PhpHttpDriver implements HttpDriverInterface
{
/**
* {@inheritDoc}
*/
function setHeaders(array $headers)
{
foreach ($headers as $name => $value) {
header("$name: $value");
}
}
/**
* {@inheritDoc}
*/
function isSessionStarted()
{
return isset($_SESSION);
}
/**
* {@inheritDoc}
*/
function setSessionValue($name, $value)
{
$_SESSION[$name] = $value;
}
/**
* {@inheritDoc}
*/
function hasSessionValue($name)
{
return array_key_exists($name, $_SESSION);
}
/**
* {@inheritDoc}
*/
function getSessionValue($name)
{
return $_SESSION[$name];
}
/**
* {@inheritDoc}
*/
function deleteSessionValue($name)
{
unset($_SESSION[$name]);
}
}

View File

@ -54,20 +54,29 @@ class DebugBarTest extends DebugBarTestCase
$this->assertEquals($s->data[$this->debugbar->getCurrentRequestId()], $data);
}
public function testSendDataInHeaders()
{
$http = $this->debugbar->getHttpDriver();
$this->debugbar->addCollector($c = new MockCollector(array('foo')));
$this->debugbar->sendDataInHeaders();
$this->assertArrayHasKey('phpdebugbar', $http->headers);
}
public function testStackedData()
{
$_SESSION = array();
$http = $this->debugbar->getHttpDriver();
$this->debugbar->addCollector($c = new MockCollector(array('foo')));
$this->debugbar->stackData();
$this->assertArrayHasKey($ns = $this->debugbar->getStackDataSessionNamespace(), $_SESSION);
$this->assertArrayHasKey($id = $this->debugbar->getCurrentRequestId(), $_SESSION[$ns]);
$this->assertArrayHasKey('mock', $_SESSION[$ns][$id]);
$this->assertEquals($c->collect(), $_SESSION[$ns][$id]['mock']);
$this->assertArrayHasKey($ns = $this->debugbar->getStackDataSessionNamespace(), $http->session);
$this->assertArrayHasKey($id = $this->debugbar->getCurrentRequestId(), $http->session[$ns]);
$this->assertArrayHasKey('mock', $http->session[$ns][$id]);
$this->assertEquals($c->collect(), $http->session[$ns][$id]['mock']);
$this->assertTrue($this->debugbar->hasStackedData());
$data = $this->debugbar->getStackedData();
$this->assertArrayNotHasKey($ns, $_SESSION);
$this->assertArrayNotHasKey($ns, $http->session);
$this->assertArrayHasKey($id, $data);
$this->assertEquals(1, count($data));
$this->assertArrayHasKey('mock', $data[$id]);
@ -76,14 +85,13 @@ class DebugBarTest extends DebugBarTestCase
public function testStackedDataWithStorage()
{
$_SESSION = array();
$s = new MockStorage();
$this->debugbar->setStorage($s);
$http = $this->debugbar->getHttpDriver();
$this->debugbar->setStorage($s = new MockStorage());
$this->debugbar->addCollector($c = new MockCollector(array('foo')));
$this->debugbar->stackData();
$id = $this->debugbar->getCurrentRequestId();
$this->assertNull($_SESSION[$this->debugbar->getStackDataSessionNamespace()][$id]);
$this->assertNull($http->session[$this->debugbar->getStackDataSessionNamespace()][$id]);
$data = $this->debugbar->getStackedData();
$this->assertEquals($c->collect(), $data[$id]['mock']);

View File

@ -10,6 +10,7 @@ abstract class DebugBarTestCase extends \PHPUnit_Framework_TestCase
public function setUp()
{
$this->debugbar = new DebugBar();
$this->debugbar->setHttpDriver($http = new MockHttpDriver());
}
public function assertJsonIsArray($json)

View File

@ -0,0 +1,44 @@
<?php
namespace DebugBar\Tests;
use DebugBar\HttpDriverInterface;
class MockHttpDriver implements HttpDriverInterface
{
public $headers = array();
public $sessionStarted = true;
public $session = array();
function setHeaders(array $headers)
{
$this->headers = array_merge($this->headers, $headers);
}
function isSessionStarted()
{
return $this->sessionStarted;
}
function setSessionValue($name, $value)
{
$this->session[$name] = $value;
}
function hasSessionValue($name)
{
return array_key_exists($name, $this->session);
}
function getSessionValue($name)
{
return $this->session[$name];
}
function deleteSessionValue($name)
{
unset($this->session[$name]);
}
}