Skip to content

Instantly share code, notes, and snippets.

@gnutix
Created March 27, 2014 12:59
Show Gist options
  • Save gnutix/9807001 to your computer and use it in GitHub Desktop.
Save gnutix/9807001 to your computer and use it in GitHub Desktop.
<?php
namespace SymfonyExtension\Component\HttpFoundation;
use Symfony\Component\HttpFoundation\Request as BaseRequest;
use Symfony\Component\HttpFoundation\RequestMatcher as BaseRequestMatcher;
/**
* This class improves Symfony2's RequestMatcher by adding a method to specify the scheme(s) to match.
*/
class RequestMatcher extends BaseRequestMatcher
{
/** @var string[] */
protected $schemes;
/**
* Adds a check for the HTTP scheme.
*
* @param string|string[]|null $scheme An HTTP scheme or an array of HTTP schemes
*/
public function matchScheme($scheme)
{
$this->schemes = array_map('strtolower', (array) $scheme);
}
/**
* {@inheritDoc}
*/
public function matches(BaseRequest $request)
{
if ($this->schemes && !in_array($request->getScheme(), $this->schemes)) {
return false;
}
return parent::matches($request);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment