CommandLineOrUnsafeMethod.php 806 B

123456789101112131415161718192021222324252627282930313233
  1. <?php
  2. namespace Drupal\Core\PageCache\RequestPolicy;
  3. use Drupal\Core\PageCache\RequestPolicyInterface;
  4. use Symfony\Component\HttpFoundation\Request;
  5. /**
  6. * Reject when running from the command line or when HTTP method is not safe.
  7. *
  8. * The policy denies caching if the request was initiated from the command line
  9. * interface (drush) or the request method is neither GET nor HEAD (see RFC
  10. * 2616, section 9.1.1 - Safe Methods).
  11. */
  12. class CommandLineOrUnsafeMethod implements RequestPolicyInterface {
  13. /**
  14. * {@inheritdoc}
  15. */
  16. public function check(Request $request) {
  17. if ($this->isCli() || !$request->isMethodCacheable()) {
  18. return static::DENY;
  19. }
  20. }
  21. /**
  22. * Indicates whether this is a CLI request.
  23. */
  24. protected function isCli() {
  25. return PHP_SAPI === 'cli';
  26. }
  27. }