Matching.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. namespace Http\Message\Authentication;
  3. use Http\Message\Authentication;
  4. use Http\Message\RequestMatcher\CallbackRequestMatcher;
  5. use Psr\Http\Message\RequestInterface;
  6. @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);
  7. /**
  8. * Authenticate a PSR-7 Request if the request is matching.
  9. *
  10. * @author Márk Sági-Kazár <mark.sagikazar@gmail.com>
  11. *
  12. * @deprecated since since version 1.2, and will be removed in 2.0. Use {@link RequestConditional} instead.
  13. */
  14. final class Matching implements Authentication
  15. {
  16. /**
  17. * @var Authentication
  18. */
  19. private $authentication;
  20. /**
  21. * @var CallbackRequestMatcher
  22. */
  23. private $matcher;
  24. /**
  25. * @param Authentication $authentication
  26. * @param callable|null $matcher
  27. */
  28. public function __construct(Authentication $authentication, callable $matcher = null)
  29. {
  30. if (is_null($matcher)) {
  31. $matcher = function () {
  32. return true;
  33. };
  34. }
  35. $this->authentication = $authentication;
  36. $this->matcher = new CallbackRequestMatcher($matcher);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function authenticate(RequestInterface $request)
  42. {
  43. if ($this->matcher->matches($request)) {
  44. return $this->authentication->authenticate($request);
  45. }
  46. return $request;
  47. }
  48. /**
  49. * Creates a matching authentication for an URL.
  50. *
  51. * @param Authentication $authentication
  52. * @param string $url
  53. *
  54. * @return self
  55. */
  56. public static function createUrlMatcher(Authentication $authentication, $url)
  57. {
  58. $matcher = function (RequestInterface $request) use ($url) {
  59. return preg_match($url, $request->getRequestTarget());
  60. };
  61. return new static($authentication, $matcher);
  62. }
  63. }