Skip to content

Commit 1271fe7

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

File tree

5 files changed

+194
-1
lines changed

5 files changed

+194
-1
lines changed

CHANGELOG.md

+5
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22

33
## Unreleased
44

5+
### Added
6+
7+
- The RequestMatcher is built after the Symfony RequestMatcher and separates
8+
scheme, host and path expressions and provides an option to filter on the
9+
method. The RegexRequestMatcher is deprecated and will be removed in 2.0.
510

611
## 1.1.0
712

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
},
4949
"extra": {
5050
"branch-alias": {
51-
"dev-master": "1.1-dev"
51+
"dev-master": "1.2-dev"
5252
}
5353
}
5454
}
+106
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
namespace spec\Http\Message\RequestMatcher;
4+
5+
use Http\Message\RequestMatcher;
6+
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\UriInterface;
8+
use PhpSpec\ObjectBehavior;
9+
10+
class RequestMatcherSpec extends ObjectBehavior
11+
{
12+
function let()
13+
{
14+
$this->beConstructedWith();
15+
}
16+
17+
function it_is_initializable()
18+
{
19+
$this->shouldHaveType('Http\Message\RequestMatcher\RequestMatcher');
20+
}
21+
22+
function it_is_a_request_matcher()
23+
{
24+
$this->shouldImplement('Http\Message\RequestMatcher');
25+
}
26+
27+
function it_matches_a_path(RequestInterface $request, UriInterface $uri)
28+
{
29+
$this->beConstructedWith('^/tes?');
30+
31+
$request->getUri()->willReturn($uri);
32+
$uri->getPath()->willReturn('/test/foo');
33+
34+
$this->matches($request)->shouldReturn(true);
35+
}
36+
37+
function it_does_not_match_a_path(RequestInterface $request, UriInterface $uri)
38+
{
39+
$this->beConstructedWith('#^/tes?#');
40+
41+
$request->getUri()->willReturn($uri);
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');
103+
104+
$this->matches($request)->shouldReturn(false);
105+
}
106+
}

src/RequestMatcher/RegexRequestMatcher.php

+4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,13 @@
55
use Http\Message\RequestMatcher;
66
use Psr\Http\Message\RequestInterface;
77

8+
@trigger_error('The '.__NAMESPACE__.'\RegexREquestMatcher class is deprecated since version 1.2 and will be removed in 2.0. Use Http\Message\RequestMatcher\RequestMatcher instead.', E_USER_DEPRECATED);
9+
810
/**
911
* Match a request with a regex on the uri.
1012
*
13+
* @deprecated Use RequestMatcher instead. This class will be removed in version 2.0
14+
*
1115
* @author Joel Wurtz <[email protected]>
1216
*/
1317
final class RegexRequestMatcher implements RequestMatcher

src/RequestMatcher/RequestMatcher.php

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
3+
namespace Http\Message\RequestMatcher;
4+
5+
use Http\Message\RequestMatcher as RequestMatcherInterface;
6+
use Psr\Http\Message\RequestInterface;
7+
8+
/**
9+
* A port of the Symfony RequestMatcher for PSR-7.
10+
*
11+
* @author Fabien Potencier <[email protected]>
12+
* @author Joel Wurtz <[email protected]>
13+
*/
14+
final class RequestMatcher implements RequestMatcherInterface
15+
{
16+
/**
17+
* @var string
18+
*/
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 $schemes = [];
35+
36+
/**
37+
* The regular expressions used for path or host must be specified without delimiter.
38+
* You do not need to escape the forward slash / to match it.
39+
*
40+
* @param string|null $path Regular expression for the path
41+
* @param string|null $host Regular expression for the hostname
42+
* @param string|string[]|null $methods Method or list of methods to match
43+
* @param string|string[]|null $schemes Scheme or list of schemes to match (e.g. http or https)
44+
*/
45+
public function __construct($path = null, $host = null, $methods = [], $schemes = [])
46+
{
47+
$this->path = $path;
48+
$this->host = $host;
49+
$this->methods = array_map('strtoupper', (array) $methods);
50+
$this->schemes = array_map('strtolower', (array) $schemes);
51+
}
52+
53+
/**
54+
* {@inheritdoc}
55+
*
56+
* @api
57+
*/
58+
public function matches(RequestInterface $request)
59+
{
60+
if ($this->schemes && !in_array($request->getUri()->getScheme(), $this->schemes)) {
61+
return false;
62+
}
63+
64+
if ($this->methods && !in_array($request->getMethod(), $this->methods)) {
65+
return false;
66+
}
67+
68+
if (null !== $this->path && !preg_match('{'.$this->path.'}', rawurldecode($request->getUri()->getPath()))) {
69+
return false;
70+
}
71+
72+
if (null !== $this->host && !preg_match('{'.$this->host.'}i', $request->getUri()->getHost())) {
73+
return false;
74+
}
75+
76+
return true;
77+
}
78+
}

0 commit comments

Comments
 (0)