Skip to content

Commit e301446

Browse files
committed
adjust request matcher to the symfony model
1 parent a3b45d5 commit e301446

File tree

2 files changed

+129
-15
lines changed

2 files changed

+129
-15
lines changed

spec/RequestMatcher/RegexRequestMatcherSpec.php

+68-8
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99

1010
class RegexRequestMatcherSpec extends ObjectBehavior
1111
{
12-
function let($regex)
12+
function let()
1313
{
14-
$this->beConstructedWith($regex);
14+
$this->beConstructedWith();
1515
}
1616

1717
function it_is_initializable()
@@ -24,22 +24,82 @@ function it_is_a_request_matcher()
2424
$this->shouldImplement('Http\Message\RequestMatcher');
2525
}
2626

27-
function it_matches_a_request(RequestInterface $request, UriInterface $uri)
27+
function it_matches_a_path(RequestInterface $request, UriInterface $uri)
2828
{
29-
$this->beConstructedWith('/test/');
29+
$this->beConstructedWith('^/tes?');
3030

3131
$request->getUri()->willReturn($uri);
32-
$uri->__toString()->willReturn('/test');
32+
$uri->getPath()->willReturn('/test/foo');
3333

3434
$this->matches($request)->shouldReturn(true);
3535
}
3636

37-
function it_does_not_match_a_request(RequestInterface $request, UriInterface $uri)
37+
function it_does_not_match_a_path(RequestInterface $request, UriInterface $uri)
3838
{
39-
$this->beConstructedWith('/test/');
39+
$this->beConstructedWith('#^/tes?#');
4040

4141
$request->getUri()->willReturn($uri);
42-
$uri->__toString()->willReturn('/ttttt');
42+
$uri->getPath()->willReturn('/ttttt');
43+
44+
$this->matches($request)->shouldReturn(false);
45+
}
46+
47+
48+
function it_matches_a_host(RequestInterface $request, UriInterface $uri)
49+
{
50+
$this->beConstructedWith(null, 'php-htt?');
51+
52+
$request->getUri()->willReturn($uri);
53+
$uri->getHost()->willReturn('php-http.org');
54+
55+
$this->matches($request)->shouldReturn(true);
56+
}
57+
58+
function it_does_not_match_a_host(RequestInterface $request, UriInterface $uri)
59+
{
60+
$this->beConstructedWith(null, 'php-htt?');
61+
62+
$request->getUri()->willReturn($uri);
63+
$uri->getHost()->willReturn('httplug.io');
64+
65+
$this->matches($request)->shouldReturn(false);
66+
}
67+
68+
function it_matches_a_method(RequestInterface $request)
69+
{
70+
$this->beConstructedWith(null, null, 'get');
71+
72+
$request->getMethod()->willReturn('GET');
73+
74+
$this->matches($request)->shouldReturn(true);
75+
}
76+
77+
function it_does_not_match_a_method(RequestInterface $request)
78+
{
79+
$this->beConstructedWith(null, null, 'get');
80+
81+
$request->getMethod()->willReturn('post');
82+
83+
$this->matches($request)->shouldReturn(false);
84+
}
85+
86+
87+
function it_matches_a_scheme(RequestInterface $request, UriInterface $uri)
88+
{
89+
$this->beConstructedWith(null, null, null, 'http');
90+
91+
$request->getUri()->willReturn($uri);
92+
$uri->getScheme()->willReturn('http');
93+
94+
$this->matches($request)->shouldReturn(true);
95+
}
96+
97+
function it_does_not_match_a_scheme(RequestInterface $request, UriInterface $uri)
98+
{
99+
$this->beConstructedWith(null, null, null, 'http');
100+
101+
$request->getUri()->willReturn($uri);
102+
$uri->getScheme()->willReturn('https');
43103

44104
$this->matches($request)->shouldReturn(false);
45105
}

src/RequestMatcher/RegexRequestMatcher.php

+61-7
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,83 @@
66
use Psr\Http\Message\RequestInterface;
77

88
/**
9-
* Match a request with a regex on the uri.
9+
* A port of the Symfony RequestMatcher for PSR-7.
1010
*
11+
* @author Fabien Potencier <[email protected]>
1112
* @author Joel Wurtz <[email protected]>
1213
*/
1314
final class RegexRequestMatcher implements RequestMatcher
1415
{
1516
/**
16-
* Matching regex.
17-
*
1817
* @var string
1918
*/
20-
private $regex;
19+
private $path;
20+
21+
/**
22+
* @var string
23+
*/
24+
private $host;
25+
26+
/**
27+
* @var array
28+
*/
29+
private $methods = [];
30+
31+
/**
32+
* @var string
33+
*/
34+
private $ips = [];
35+
36+
/**
37+
* @var array
38+
*/
39+
private $attributes = [];
2140

22-
public function __construct($regex)
41+
/**
42+
* @var string[]
43+
*/
44+
private $schemes = [];
45+
46+
/**
47+
* The regular expressions used for path or host must be specified without delimiter.
48+
* You do not need to escape the forward slash / to match it.
49+
*
50+
* @param string|null $path Regular expression for the path
51+
* @param string|null $host Regular expression for the hostname
52+
* @param string|string[]|null $methods Method or list of methods to match
53+
* @param string|string[]|null $schemes Scheme or list of schemes to match (e.g. http or https)
54+
*/
55+
public function __construct($path = null, $host = null, $methods = [], $schemes = [])
2356
{
24-
$this->regex = $regex;
57+
$this->path = $path;
58+
$this->host = $host;
59+
$this->methods = array_map('strtoupper', (array) $methods);
60+
$this->schemes = array_map('strtolower', (array) $schemes);
2561
}
2662

2763
/**
2864
* {@inheritdoc}
65+
*
66+
* @api
2967
*/
3068
public function matches(RequestInterface $request)
3169
{
32-
return (bool) preg_match($this->regex, (string) $request->getUri());
70+
if ($this->schemes && !in_array($request->getUri()->getScheme(), $this->schemes)) {
71+
return false;
72+
}
73+
74+
if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
75+
return false;
76+
}
77+
78+
if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getUri()->getPath()))) {
79+
return false;
80+
}
81+
82+
if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getUri()->getHost())) {
83+
return false;
84+
}
85+
86+
return true;
3387
}
3488
}

0 commit comments

Comments
 (0)