1
|
|
|
<?php |
|
|
|
|
2
|
|
|
|
3
|
|
|
namespace Http\Message\Authentication; |
4
|
|
|
|
5
|
|
|
use Http\Message\Authentication; |
6
|
|
|
use Http\Message\RequestMatcher\CallbackRequestMatcher; |
7
|
|
|
use Psr\Http\Message\RequestInterface; |
8
|
|
|
|
9
|
|
|
@trigger_error('The '.__NAMESPACE__.'\Matching class is deprecated since version 1.2 and will be removed in 2.0. Use Http\Message\Authentication\RequestConditional instead.', E_USER_DEPRECATED); |
|
|
|
|
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Authenticate a PSR-7 Request if the request is matching. |
13
|
|
|
* |
14
|
|
|
* @author Márk Sági-Kazár <[email protected]> |
15
|
|
|
* |
16
|
|
|
* @deprecated since since version 1.2, and will be removed in 2.0. Use {@link RequestConditional} instead. |
17
|
|
|
*/ |
18
|
|
|
final class Matching implements Authentication |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @var Authentication |
22
|
|
|
*/ |
23
|
|
|
private $authentication; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var CallbackRequestMatcher |
27
|
|
|
*/ |
28
|
|
|
private $matcher; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param Authentication $authentication |
32
|
5 |
|
* @param callable|null $matcher |
33
|
|
|
*/ |
34
|
5 |
|
public function __construct(Authentication $authentication, callable $matcher = null) |
35
|
|
|
{ |
36
|
|
|
if (is_null($matcher)) { |
37
|
|
|
$matcher = function () { |
38
|
|
|
return true; |
39
|
|
|
}; |
40
|
5 |
|
} |
41
|
5 |
|
|
42
|
5 |
|
$this->authentication = $authentication; |
43
|
|
|
$this->matcher = new CallbackRequestMatcher($matcher); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
2 |
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
2 |
|
public function authenticate(RequestInterface $request) |
50
|
1 |
|
{ |
51
|
|
|
if ($this->matcher->matches($request)) { |
52
|
|
|
return $this->authentication->authenticate($request); |
53
|
1 |
|
} |
54
|
|
|
|
55
|
|
|
return $request; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Creates a matching authentication for an URL. |
60
|
|
|
* |
61
|
|
|
* @param Authentication $authentication |
62
|
|
|
* @param string $url |
63
|
|
|
* |
64
|
|
|
* @return self |
65
|
|
|
*/ |
66
|
1 |
|
public static function createUrlMatcher(Authentication $authentication, $url) |
67
|
|
|
{ |
68
|
1 |
|
$matcher = function (RequestInterface $request) use ($url) { |
69
|
|
|
return preg_match($url, $request->getRequestTarget()); |
70
|
1 |
|
}; |
71
|
|
|
|
72
|
|
|
return new static($authentication, $matcher); |
|
|
|
|
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.
The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.
To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.