Drupal 8.0.0 beta 12. More info: https://www.drupal.org/node/2514176
This commit is contained in:
commit
9921556621
13277 changed files with 1459781 additions and 0 deletions
65
core/lib/Drupal/Core/PageCache/ChainRequestPolicy.php
Normal file
65
core/lib/Drupal/Core/PageCache/ChainRequestPolicy.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ChainRequestPolicy.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Implements a compound request policy.
|
||||
*
|
||||
* When evaluating the compound policy, all of the contained rules are applied
|
||||
* to the request. The overall result is computed according to the following
|
||||
* rules:
|
||||
*
|
||||
* <ol>
|
||||
* <li>Returns static::DENY if any of the rules evaluated to static::DENY</li>
|
||||
* <li>Returns static::ALLOW if at least one of the rules evaluated to
|
||||
* static::ALLOW and none to static::DENY</li>
|
||||
* <li>Otherwise returns NULL</li>
|
||||
* </ol>
|
||||
*/
|
||||
class ChainRequestPolicy implements ChainRequestPolicyInterface {
|
||||
|
||||
/**
|
||||
* A list of policy rules to apply when this policy is evaluated.
|
||||
*
|
||||
* @var \Drupal\Core\PageCache\RequestPolicyInterface[]
|
||||
*/
|
||||
protected $rules = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Request $request) {
|
||||
$final_result = NULL;
|
||||
|
||||
foreach ($this->rules as $rule) {
|
||||
$result = $rule->check($request);
|
||||
if ($result === static::DENY) {
|
||||
return $result;
|
||||
}
|
||||
elseif ($result === static::ALLOW) {
|
||||
$final_result = $result;
|
||||
}
|
||||
elseif (isset($result)) {
|
||||
throw new \UnexpectedValueException('Return value of RequestPolicyInterface::check() must be one of RequestPolicyInterface::ALLOW, RequestPolicyInterface::DENY or NULL');
|
||||
}
|
||||
}
|
||||
|
||||
return $final_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addPolicy(RequestPolicyInterface $policy) {
|
||||
$this->rules[] = $policy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,25 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ChainRequestPolicyInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
/**
|
||||
* Defines the interface for compound request policies.
|
||||
*/
|
||||
interface ChainRequestPolicyInterface extends RequestPolicyInterface {
|
||||
|
||||
/**
|
||||
* Add a policy to the list of policy rules.
|
||||
*
|
||||
* @param \Drupal\Core\PageCache\RequestPolicyInterface $policy
|
||||
* The request policy rule to add.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addPolicy(RequestPolicyInterface $policy);
|
||||
|
||||
}
|
56
core/lib/Drupal/Core/PageCache/ChainResponsePolicy.php
Normal file
56
core/lib/Drupal/Core/PageCache/ChainResponsePolicy.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ChainResponsePolicy.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Implements a compound response policy.
|
||||
*
|
||||
* When evaluating the compound policy, all of the contained rules are applied
|
||||
* to the response. The overall result is computed according to the following
|
||||
* rules:
|
||||
*
|
||||
* <ol>
|
||||
* <li>Returns static::DENY if any of the rules evaluated to static::DENY</li>
|
||||
* <li>Otherwise returns NULL</li>
|
||||
* </ol>
|
||||
*/
|
||||
class ChainResponsePolicy implements ChainResponsePolicyInterface {
|
||||
|
||||
/**
|
||||
* A list of policy rules to apply when this policy is checked.
|
||||
*
|
||||
* @var \Drupal\Core\PageCache\ResponsePolicyInterface[]
|
||||
*/
|
||||
protected $rules = [];
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Response $response, Request $request) {
|
||||
foreach ($this->rules as $rule) {
|
||||
$result = $rule->check($response, $request);
|
||||
if ($result === static::DENY) {
|
||||
return $result;
|
||||
}
|
||||
elseif (isset($result)) {
|
||||
throw new \UnexpectedValueException('Return value of ResponsePolicyInterface::check() must be one of ResponsePolicyInterface::DENY or NULL');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function addPolicy(ResponsePolicyInterface $policy) {
|
||||
$this->rules[] = $policy;
|
||||
return $this;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,24 @@
|
|||
<?php
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ChainResponsePolicyInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
/**
|
||||
* Defines the interface for compound request policies.
|
||||
*/
|
||||
interface ChainResponsePolicyInterface extends ResponsePolicyInterface {
|
||||
|
||||
/**
|
||||
* Add a policy to the list of policy rules.
|
||||
*
|
||||
* @param \Drupal\Core\PageCache\ResponsePolicyInterface $policy
|
||||
* The request policy rule to add.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function addPolicy(ResponsePolicyInterface $policy);
|
||||
|
||||
}
|
32
core/lib/Drupal/Core/PageCache/DefaultRequestPolicy.php
Normal file
32
core/lib/Drupal/Core/PageCache/DefaultRequestPolicy.php
Normal file
|
@ -0,0 +1,32 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\DefaultRequestPolicy.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
use Drupal\Core\Session\SessionConfigurationInterface;
|
||||
|
||||
/**
|
||||
* The default page cache request policy.
|
||||
*
|
||||
* Delivery of cached pages is denied if either the application is running from
|
||||
* the command line or the request was not initiated with a safe method (GET or
|
||||
* HEAD). Also caching is only allowed for requests without a session cookie.
|
||||
*/
|
||||
class DefaultRequestPolicy extends ChainRequestPolicy {
|
||||
|
||||
/**
|
||||
* Constructs the default page cache request policy.
|
||||
*
|
||||
* @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
|
||||
* The session configuration.
|
||||
*/
|
||||
public function __construct(SessionConfigurationInterface $session_configuration) {
|
||||
$this->addPolicy(new RequestPolicy\CommandLineOrUnsafeMethod());
|
||||
$this->addPolicy(new RequestPolicy\NoSessionOpen($session_configuration));
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\RequestPolicy\CommandLineOrUnsafeMethod.
|
||||
*/
|
||||
|
||||
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->isMethodSafe()) {
|
||||
return static::DENY;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Indicates whether this is a CLI request.
|
||||
*/
|
||||
protected function isCli() {
|
||||
return PHP_SAPI === 'cli';
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\RequestPolicy\NoSessionOpen.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache\RequestPolicy;
|
||||
|
||||
use Drupal\Core\PageCache\RequestPolicyInterface;
|
||||
use Drupal\Core\Session\SessionConfigurationInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* A policy allowing delivery of cached pages when there is no session open.
|
||||
*
|
||||
* Do not serve cached pages to authenticated users, or to anonymous users when
|
||||
* $_SESSION is non-empty. $_SESSION may contain status messages from a form
|
||||
* submission, the contents of a shopping cart, or other userspecific content
|
||||
* that should not be cached and displayed to other users.
|
||||
*/
|
||||
class NoSessionOpen implements RequestPolicyInterface {
|
||||
|
||||
/**
|
||||
* The session configuration.
|
||||
*
|
||||
* @var \Drupal\Core\Session\SessionConfigurationInterface
|
||||
*/
|
||||
protected $sessionConfiguration;
|
||||
|
||||
/**
|
||||
* Constructs a new page cache session policy.
|
||||
*
|
||||
* @param \Drupal\Core\Session\SessionConfigurationInterface $session_configuration
|
||||
* The session configuration.
|
||||
*/
|
||||
public function __construct(SessionConfigurationInterface $session_configuration) {
|
||||
$this->sessionConfiguration = $session_configuration;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Request $request) {
|
||||
if (!$this->sessionConfiguration->hasSession($request)) {
|
||||
return static::ALLOW;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
54
core/lib/Drupal/Core/PageCache/RequestPolicyInterface.php
Normal file
54
core/lib/Drupal/Core/PageCache/RequestPolicyInterface.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\RequestPolicyInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
||||
/**
|
||||
* Defines the interface for request policy implementations.
|
||||
*
|
||||
* The request policy is evaluated in order to determine whether delivery of a
|
||||
* cached page should be attempted. The caller should do so if static::ALLOW is
|
||||
* returned from the check() method.
|
||||
*/
|
||||
interface RequestPolicyInterface {
|
||||
|
||||
/**
|
||||
* Allow delivery of cached pages.
|
||||
*/
|
||||
const ALLOW = 'allow';
|
||||
|
||||
/**
|
||||
* Deny delivery of cached pages.
|
||||
*/
|
||||
const DENY = 'deny';
|
||||
|
||||
/**
|
||||
* Determines whether delivery of a cached page should be attempted.
|
||||
*
|
||||
* Note that the request-policy check runs very early. In particular it is
|
||||
* not possible to determine the logged in user. Also the current route match
|
||||
* is not yet present when the check runs. Therefore, request-policy checks
|
||||
* need to be designed in a way such that they do not depend on any other
|
||||
* service and only take in account the information present on the incoming
|
||||
* request.
|
||||
*
|
||||
* When matching against the request path, special attention is needed to
|
||||
* support path prefixes which are often used on multilingual sites.
|
||||
*
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The incoming request object.
|
||||
*
|
||||
* @return string|NULL
|
||||
* One of static::ALLOW, static::DENY or NULL. Calling code may attempt to
|
||||
* deliver a cached page if static::ALLOW is returned. Returns NULL if the
|
||||
* policy is not specified for the given request.
|
||||
*/
|
||||
public function check(Request $request);
|
||||
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ResponsePolicy\DenyNoCacheRoutes.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache\ResponsePolicy;
|
||||
|
||||
use Drupal\Core\PageCache\ResponsePolicyInterface;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Cache policy for routes with the 'no_cache' option set.
|
||||
*
|
||||
* This policy rule denies caching of responses generated for routes that have
|
||||
* the 'no_cache' option set to TRUE.
|
||||
*/
|
||||
class DenyNoCacheRoutes implements ResponsePolicyInterface {
|
||||
|
||||
/**
|
||||
* The current route match.
|
||||
*
|
||||
* @var \Drupal\Core\Routing\RouteMatchInterface
|
||||
*/
|
||||
protected $routeMatch;
|
||||
|
||||
/**
|
||||
* Constructs a deny node preview page cache policy.
|
||||
*
|
||||
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
|
||||
* The current route match.
|
||||
*/
|
||||
public function __construct(RouteMatchInterface $route_match) {
|
||||
$this->routeMatch = $route_match;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Response $response, Request $request) {
|
||||
if (($route = $this->routeMatch->getRouteObject()) && $route->getOption('no_cache')) {
|
||||
return static::DENY;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
42
core/lib/Drupal/Core/PageCache/ResponsePolicy/KillSwitch.php
Normal file
42
core/lib/Drupal/Core/PageCache/ResponsePolicy/KillSwitch.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ResponsePolicy\KillSwitch.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache\ResponsePolicy;
|
||||
|
||||
use Drupal\Core\PageCache\ResponsePolicyInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* A policy evaluating to static::DENY when the kill switch was triggered.
|
||||
*/
|
||||
class KillSwitch implements ResponsePolicyInterface {
|
||||
|
||||
/**
|
||||
* A flag indicating whether the kill switch was triggered.
|
||||
*
|
||||
* @var bool
|
||||
*/
|
||||
protected $kill = FALSE;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Response $response, Request $request) {
|
||||
if ($this->kill) {
|
||||
return static::DENY;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Deny any page caching on the current request.
|
||||
*/
|
||||
public function trigger() {
|
||||
$this->kill = TRUE;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ResponsePolicy\NoServerError.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache\ResponsePolicy;
|
||||
|
||||
use Drupal\Core\PageCache\ResponsePolicyInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* A policy denying caching of a server error (HTTP 5xx) responses.
|
||||
*/
|
||||
class NoServerError implements ResponsePolicyInterface {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function check(Response $response, Request $request) {
|
||||
if ($response->isServerError()) {
|
||||
return static::DENY;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
42
core/lib/Drupal/Core/PageCache/ResponsePolicyInterface.php
Normal file
42
core/lib/Drupal/Core/PageCache/ResponsePolicyInterface.php
Normal file
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
/**
|
||||
* @file
|
||||
* Contains \Drupal\Core\PageCache\ResponsePolicyInterface.
|
||||
*/
|
||||
|
||||
namespace Drupal\Core\PageCache;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
|
||||
/**
|
||||
* Defines the interface for response policy implementations.
|
||||
*
|
||||
* The response policy is evaluated in order to determine whether a page should
|
||||
* be stored a in the cache. Calling code should do so unless static::DENY is
|
||||
* returned from the check() method.
|
||||
*/
|
||||
interface ResponsePolicyInterface {
|
||||
|
||||
/**
|
||||
* Deny storage of a page in the cache.
|
||||
*/
|
||||
const DENY = 'deny';
|
||||
|
||||
/**
|
||||
* Determines whether it is save to store a page in the cache.
|
||||
*
|
||||
* @param \Symfony\Component\HttpFoundation\Response $response
|
||||
* The response which is about to be sent to the client.
|
||||
* @param \Symfony\Component\HttpFoundation\Request $request
|
||||
* The request object.
|
||||
*
|
||||
* @return string|NULL
|
||||
* Either static::DENY or NULL. Calling code may attempt to store a page in
|
||||
* the cache unless static::DENY is returned. Returns NULL if the policy
|
||||
* policy is not specified for the given response.
|
||||
*/
|
||||
public function check(Response $response, Request $request);
|
||||
|
||||
}
|
Reference in a new issue