ChainResponsePolicy.php 1.3 KB

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