comparison core/lib/Drupal/Core/PageCache/ChainRequestPolicy.php @ 0:4c8ae668cc8c

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