From 7e81754ef68be8b05cf7f60812b13e924c886c89 Mon Sep 17 00:00:00 2001 From: maximebf Date: Thu, 19 Sep 2013 16:31:00 -0400 Subject: [PATCH] Added abstraction for HTTP features (fixed #28) --- src/DebugBar/DebugBar.php | 61 +++++++++++++++++---- src/DebugBar/HttpDriverInterface.php | 63 +++++++++++++++++++++ src/DebugBar/PhpHttpDriver.php | 67 +++++++++++++++++++++++ tests/DebugBar/Tests/DebugBarTest.php | 28 ++++++---- tests/DebugBar/Tests/DebugBarTestCase.php | 1 + tests/DebugBar/Tests/MockHttpDriver.php | 44 +++++++++++++++ 6 files changed, 242 insertions(+), 22 deletions(-) create mode 100644 src/DebugBar/HttpDriverInterface.php create mode 100644 src/DebugBar/PhpHttpDriver.php create mode 100644 tests/DebugBar/Tests/MockHttpDriver.php diff --git a/src/DebugBar/DebugBar.php b/src/DebugBar/DebugBar.php index f30a7f1..db08c05 100644 --- a/src/DebugBar/DebugBar.php +++ b/src/DebugBar/DebugBar.php @@ -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; } /** diff --git a/src/DebugBar/HttpDriverInterface.php b/src/DebugBar/HttpDriverInterface.php new file mode 100644 index 0000000..cdcf243 --- /dev/null +++ b/src/DebugBar/HttpDriverInterface.php @@ -0,0 +1,63 @@ + $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]); + } +} \ No newline at end of file diff --git a/tests/DebugBar/Tests/DebugBarTest.php b/tests/DebugBar/Tests/DebugBarTest.php index e3602df..c6bd2cc 100644 --- a/tests/DebugBar/Tests/DebugBarTest.php +++ b/tests/DebugBar/Tests/DebugBarTest.php @@ -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']); diff --git a/tests/DebugBar/Tests/DebugBarTestCase.php b/tests/DebugBar/Tests/DebugBarTestCase.php index 6acbd97..017448a 100644 --- a/tests/DebugBar/Tests/DebugBarTestCase.php +++ b/tests/DebugBar/Tests/DebugBarTestCase.php @@ -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) diff --git a/tests/DebugBar/Tests/MockHttpDriver.php b/tests/DebugBar/Tests/MockHttpDriver.php new file mode 100644 index 0000000..da1b96c --- /dev/null +++ b/tests/DebugBar/Tests/MockHttpDriver.php @@ -0,0 +1,44 @@ +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]); + } +} \ No newline at end of file