Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / Context / Context.php
1 <?php
2
3 namespace Drupal\Core\Plugin\Context;
4
5 use Drupal\Component\Plugin\Context\Context as ComponentContext;
6 use Drupal\Component\Plugin\Exception\ContextException;
7 use Drupal\Core\Cache\CacheableDependencyInterface;
8 use Drupal\Core\Cache\CacheableMetadata;
9 use Drupal\Core\TypedData\TypedDataInterface;
10 use Drupal\Core\TypedData\TypedDataTrait;
11
12 /**
13  * A Drupal specific context wrapper class.
14  */
15 class Context extends ComponentContext implements ContextInterface {
16
17   use TypedDataTrait;
18
19   /**
20    * The data associated with the context.
21    *
22    * @var \Drupal\Core\TypedData\TypedDataInterface
23    */
24   protected $contextData;
25
26   /**
27    * The definition to which a context must conform.
28    *
29    * @var \Drupal\Core\Plugin\Context\ContextDefinitionInterface
30    */
31   protected $contextDefinition;
32
33   /**
34    * The cacheability metadata.
35    *
36    * @var \Drupal\Core\Cache\CacheableMetadata
37    */
38   protected $cacheabilityMetadata;
39
40   /**
41    * Create a context object.
42    *
43    * @param \Drupal\Core\Plugin\Context\ContextDefinitionInterface $context_definition
44    *   The context definition.
45    * @param mixed|null $context_value
46    *   The context value object.
47    */
48   public function __construct(ContextDefinitionInterface $context_definition, $context_value = NULL) {
49     parent::__construct($context_definition, NULL);
50     $this->cacheabilityMetadata = new CacheableMetadata();
51     if (!is_null($context_value)) {
52       $this->setContextValue($context_value);
53     }
54   }
55
56   /**
57    * {@inheritdoc}
58    */
59   public function getContextValue() {
60     if (!isset($this->contextData)) {
61       $definition = $this->getContextDefinition();
62       $default_value = $definition->getDefaultValue();
63
64       if (isset($default_value)) {
65         // Keep the default value here so that subsequent calls don't have to
66         // look it up again.
67         $this->setContextValue($default_value);
68       }
69       elseif ($definition->isRequired()) {
70         $type = $definition->getDataType();
71         throw new ContextException("The '$type' context is required and not present.");
72       }
73       return $default_value;
74     }
75     return $this->getTypedDataManager()->getCanonicalRepresentation($this->contextData);
76   }
77
78   /**
79    * {@inheritdoc}
80    */
81   public function hasContextValue() {
82     return (bool) $this->contextData || parent::hasContextValue();
83   }
84
85   /**
86    * Sets the context value.
87    *
88    * @param mixed $value
89    *   The value of this context, matching the context definition.
90    */
91   protected function setContextValue($value) {
92     // Add the value as a cacheable dependency only if implements the interface
93     // to prevent it from disabling caching with a max-age 0.
94     if ($value instanceof CacheableDependencyInterface) {
95       $this->addCacheableDependency($value);
96     }
97     if ($value instanceof TypedDataInterface) {
98       $this->contextData = $value;
99     }
100     else {
101       $this->contextData = $this->getTypedDataManager()->create($this->contextDefinition->getDataDefinition(), $value);
102     }
103   }
104
105   /**
106    * {@inheritdoc}
107    */
108   public function getConstraints() {
109     return $this->contextDefinition->getConstraints();
110   }
111
112   /**
113    * {@inheritdoc}
114    */
115   public function getContextData() {
116     if (!isset($this->contextData)) {
117       $definition = $this->getContextDefinition();
118       $default_value = $definition->getDefaultValue();
119       // Store the default value so that subsequent calls don't have to look
120       // it up again.
121       $this->contextData = $this->getTypedDataManager()->create($definition->getDataDefinition(), $default_value);
122     }
123     return $this->contextData;
124   }
125
126
127   /**
128    * {@inheritdoc}
129    */
130   public function getContextDefinition() {
131     return $this->contextDefinition;
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   public function validate() {
138     return $this->getContextData()->validate();
139   }
140
141   /**
142    * {@inheritdoc}
143    */
144   public function addCacheableDependency($dependency) {
145     $this->cacheabilityMetadata = $this->cacheabilityMetadata->merge(CacheableMetadata::createFromObject($dependency));
146     return $this;
147   }
148
149   /**
150    * {@inheritdoc}
151    */
152   public function getCacheContexts() {
153     return $this->cacheabilityMetadata->getCacheContexts();
154   }
155
156   /**
157    * {@inheritdoc}
158    */
159   public function getCacheTags() {
160     return $this->cacheabilityMetadata->getCacheTags();
161   }
162
163   /**
164    * {@inheritdoc}
165    */
166   public function getCacheMaxAge() {
167     return $this->cacheabilityMetadata->getCacheMaxAge();
168   }
169
170   /**
171    * {@inheritdoc}
172    */
173   public static function createFromContext(ContextInterface $old_context, $value) {
174     $context = new static($old_context->getContextDefinition(), $value);
175     $context->addCacheableDependency($old_context);
176     if (method_exists($old_context, 'getTypedDataManager')) {
177       $context->setTypedDataManager($old_context->getTypedDataManager());
178     }
179     return $context;
180   }
181
182 }