Skip to content

Commit

Permalink
merged branch Seldaek/optim (PR #7931)
Browse files Browse the repository at this point in the history
This PR was merged into the master branch.

Discussion
----------

A few optimizations

Commits
-------

ea633f5 [HttpKernel] Avoid updating the context if the request did not change
997d549 [HttpFoundation] Avoid a few unnecessary str_replace() calls
f5e7f24 [HttpFoundation] Optimize ServerBag::getHeaders()
  • Loading branch information
fabpot committed May 6, 2013
2 parents e2a2b58 + ea633f5 commit c852586
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
12 changes: 4 additions & 8 deletions src/Symfony/Component/HttpFoundation/RequestMatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,20 +136,16 @@ public function matches(Request $request)
}

foreach ($this->attributes as $key => $pattern) {
if (!preg_match('#'.str_replace('#', '\\#', $pattern).'#', $request->attributes->get($key))) {
if (!preg_match('{'.$pattern.'}', $request->attributes->get($key))) {
return false;
}
}

if (null !== $this->path) {
$path = str_replace('#', '\\#', $this->path);

if (!preg_match('#'.$path.'#', rawurldecode($request->getPathInfo()))) {
return false;
}
if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getPathInfo()))) {
return false;
}

if (null !== $this->host && !preg_match('#'.str_replace('#', '\\#', $this->host).'#i', $request->getHost())) {
if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getHost())) {
return false;
}

Expand Down
5 changes: 3 additions & 2 deletions src/Symfony/Component/HttpFoundation/ServerBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ class ServerBag extends ParameterBag
public function getHeaders()
{
$headers = array();
$contentHeaders = array('CONTENT_LENGTH' => true, 'CONTENT_MD5' => true, 'CONTENT_TYPE' => true);
foreach ($this->parameters as $key => $value) {
if (0 === strpos($key, 'HTTP_')) {
if ('HTTP_' === substr($key, 0, 5)) {
$headers[substr($key, 5)] = $value;
}
// CONTENT_* are not prefixed with HTTP_
elseif (in_array($key, array('CONTENT_LENGTH', 'CONTENT_MD5', 'CONTENT_TYPE'))) {
elseif (isset($contentHeaders[$key])) {
$headers[$key] = $value;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ class RouterListener implements EventSubscriberInterface
private $matcher;
private $context;
private $logger;
private $request;

/**
* Constructor.
Expand Down Expand Up @@ -72,9 +73,10 @@ public function __construct($matcher, RequestContext $context = null, LoggerInte
*/
public function setRequest(Request $request = null)
{
if (null !== $request) {
if (null !== $request && $this->request !== $request) {
$this->context->fromRequest($request);
}
$this->request = $request;
}

public function onKernelRequest(GetResponseEvent $event)
Expand Down

0 comments on commit c852586

Please sign in to comment.