Skip to content

Commit

Permalink
Increased Performance of getRequestedMethod, also storing the method …
Browse files Browse the repository at this point in the history
…on a variable
  • Loading branch information
Claudio Santoro committed May 31, 2017
1 parent 95d58ea commit 92bf575
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/Bramus/Router/Router.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,15 @@ class Router
private $serverBasePath;

/**
* @var array The Http Request Headers
* @var array The Http Requested Headers
*/
private $headers = array();

/**
* @var string The Http Requested Method
*/
private $method = '';

/**
* Store a before middleware route and a handling function to be executed when accessed using one of the specified methods
*
Expand Down Expand Up @@ -229,23 +234,28 @@ public function getRequestHeaders()
*/
public function getRequestMethod()
{
// Take the method as found in $_SERVER
$method = $_SERVER['REQUEST_METHOD'];

// If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification
// @url http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
ob_start();
$method = 'GET';
} // If it's a POST request, check for a method override header
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = $this->getRequestHeaders();
if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], array('PUT', 'DELETE', 'PATCH'))) {
$method = $headers['X-HTTP-Method-Override'];
// If the Requested Method wasn't stored yet, store it
if(empty($this->method)) {
// Take the method as found in $_SERVER
$method = $_SERVER['REQUEST_METHOD'];

// If it's a HEAD request override it to being GET and prevent any output, as per HTTP Specification
// @url http://www.w3.org/Protocols/rfc2616/rfc2616-sec9.html#sec9.4
if ($_SERVER['REQUEST_METHOD'] == 'HEAD') {
ob_start();
$method = 'GET';
} // If it's a POST request, check for a method override header
elseif ($_SERVER['REQUEST_METHOD'] == 'POST') {
$headers = $this->getRequestHeaders();
if (isset($headers['X-HTTP-Method-Override']) && in_array($headers['X-HTTP-Method-Override'], array('PUT', 'DELETE', 'PATCH'))) {
$method = $headers['X-HTTP-Method-Override'];
}
}

return $this->method = $method;
}

return $method;
return $this->method;
}

/**
Expand Down

0 comments on commit 92bf575

Please sign in to comment.