ChainRequestPolicy.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace Drupal\Core\PageCache;
  3. use Symfony\Component\HttpFoundation\Request;
  4. /**
  5. * Implements a compound request policy.
  6. *
  7. * When evaluating the compound policy, all of the contained rules are applied
  8. * to the request. The overall result is computed according to the following
  9. * rules:
  10. *
  11. * <ol>
  12. * <li>Returns static::DENY if any of the rules evaluated to static::DENY</li>
  13. * <li>Returns static::ALLOW if at least one of the rules evaluated to
  14. * static::ALLOW and none to static::DENY</li>
  15. * <li>Otherwise returns NULL</li>
  16. * </ol>
  17. */
  18. class ChainRequestPolicy implements ChainRequestPolicyInterface {
  19. /**
  20. * A list of policy rules to apply when this policy is evaluated.
  21. *
  22. * @var \Drupal\Core\PageCache\RequestPolicyInterface[]
  23. */
  24. protected $rules = [];
  25. /**
  26. * {@inheritdoc}
  27. */
  28. public function check(Request $request) {
  29. $final_result = NULL;
  30. foreach ($this->rules as $rule) {
  31. $result = $rule->check($request);
  32. if ($result === static::DENY) {
  33. return $result;
  34. }
  35. elseif ($result === static::ALLOW) {
  36. $final_result = $result;
  37. }
  38. elseif (isset($result)) {
  39. throw new \UnexpectedValueException('Return value of RequestPolicyInterface::check() must be one of RequestPolicyInterface::ALLOW, RequestPolicyInterface::DENY or NULL');
  40. }
  41. }
  42. return $final_result;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function addPolicy(RequestPolicyInterface $policy) {
  48. $this->rules[] = $policy;
  49. return $this;
  50. }
  51. }