CommandLineOrUnsafeMethod.php

Same filename in other branches
  1. 9 core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php
  2. 8.9.x core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php
  3. 11.x core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php

Namespace

Drupal\Core\PageCache\RequestPolicy

File

core/lib/Drupal/Core/PageCache/RequestPolicy/CommandLineOrUnsafeMethod.php

View source
<?php

namespace Drupal\Core\PageCache\RequestPolicy;

use Drupal\Core\PageCache\RequestPolicyInterface;
use Symfony\Component\HttpFoundation\Request;

/**
 * Reject when running from the command line or when HTTP method is not safe.
 *
 * The policy denies caching if the request was initiated from the command line
 * interface (drush) or the request method is neither GET nor HEAD (see RFC
 * 2616, section 9.1.1 - Safe Methods).
 */
class CommandLineOrUnsafeMethod implements RequestPolicyInterface {
    
    /**
     * {@inheritdoc}
     */
    public function check(Request $request) {
        if ($this->isCli() || !$request->isMethodCacheable()) {
            return static::DENY;
        }
    }
    
    /**
     * Indicates whether this is a CLI request.
     */
    protected function isCli() {
        return PHP_SAPI === 'cli';
    }

}

Classes

Title Deprecated Summary
CommandLineOrUnsafeMethod Reject when running from the command line or when HTTP method is not safe.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.