diff --git a/src/Http/Response.php b/src/Http/Response.php index 86dfc29..e163008 100644 --- a/src/Http/Response.php +++ b/src/Http/Response.php @@ -6,6 +6,7 @@ class Response extends \Symfony\Component\HttpFoundation\Response { protected $_callTime; + protected $_sendCubexHeaders = true; public function __construct($content = '', $status = 200, $headers = []) { @@ -13,6 +14,24 @@ public function __construct($content = '', $status = 200, $headers = []) $this->from($content); } + /** + * @return $this + */ + public function disableCubexHeaders() + { + $this->_sendCubexHeaders = false; + return $this; + } + + /** + * @return $this + */ + public function enableCubexHeaders() + { + $this->_sendCubexHeaders = true; + return $this; + } + /** * Set the microtime(true) value when the call started * @@ -76,7 +95,7 @@ public function from($source) public function fromJson($object) { $this->_originalSource = $object; - $response = \json_encode($object); + $response = \json_encode($object); // Prevent content sniffing attacks by encoding "<" and ">", so browsers // won't try to execute the document as HTML @@ -104,8 +123,8 @@ public function fromJson($object) public function fromJsonp($responseKey, $object) { $this->_originalSource = $object; - $responseObject = \json_encode($object); - $response = "{$responseKey}({$responseObject})"; + $responseObject = \json_encode($object); + $response = "{$responseKey}({$responseObject})"; // Prevent content sniffing attacks by encoding "<" and ">", so browsers // won't try to execute the document as HTML @@ -157,21 +176,24 @@ public function send() */ public function setCubexHeaders() { - //Add the exec time as a header if PHP_START has been defined by the project - if(defined('PHP_START')) + if($this->_sendCubexHeaders) { - $this->headers->set( - "X-Execution-Time", - number_format((microtime(true) - PHP_START) * 1000, 3) . ' ms' - ); - } + //Add the exec time as a header if PHP_START has been defined by the project + if(defined('PHP_START')) + { + $this->headers->set( + "X-Execution-Time", + number_format((microtime(true) - PHP_START) * 1000, 3) . ' ms' + ); + } - if($this->_callTime > 0) - { - $this->headers->set( - 'X-Call-Time', - number_format((microtime(true) - $this->_callTime) * 1000, 3) . ' ms' - ); + if($this->_callTime > 0) + { + $this->headers->set( + 'X-Call-Time', + number_format((microtime(true) - $this->_callTime) * 1000, 3) . ' ms' + ); + } } } diff --git a/tests/Cubex/Http/ResponseTest.php b/tests/Cubex/Http/ResponseTest.php index 756ee45..4e78626 100644 --- a/tests/Cubex/Http/ResponseTest.php +++ b/tests/Cubex/Http/ResponseTest.php @@ -67,6 +67,17 @@ public function testCubexHeaders() $response = new Response(); $response->setCubexHeaders(); $this->assertContains('X-Execution-Time', (string)$response); + + $response = new Response(); + $response->disableCubexHeaders(); + $response->setCubexHeaders(); + $this->assertNotContains('X-Execution-Time', (string)$response); + + $response = new Response(); + $response->disableCubexHeaders(); + $response->enableCubexHeaders(); + $response->setCubexHeaders(); + $this->assertContains('X-Execution-Time', (string)$response); } }