Version 1
[yaffs-website] / web / core / lib / Drupal / Core / Plugin / Context / ContextHandler.php
1 <?php
2
3 namespace Drupal\Core\Plugin\Context;
4
5 use Drupal\Component\Plugin\Exception\ContextException;
6 use Drupal\Core\Cache\CacheableDependencyInterface;
7 use Drupal\Core\Plugin\ContextAwarePluginInterface;
8
9 /**
10  * Provides methods to handle sets of contexts.
11  */
12 class ContextHandler implements ContextHandlerInterface {
13
14   /**
15    * {@inheritdoc}
16    */
17   public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
18     return array_filter($definitions, function ($plugin_definition) use ($contexts) {
19       // If this plugin doesn't need any context, it is available to use.
20       if (!isset($plugin_definition['context'])) {
21         return TRUE;
22       }
23
24       // Check the set of contexts against the requirements.
25       return $this->checkRequirements($contexts, $plugin_definition['context']);
26     });
27   }
28
29   /**
30    * {@inheritdoc}
31    */
32   public function checkRequirements(array $contexts, array $requirements) {
33     foreach ($requirements as $requirement) {
34       if ($requirement->isRequired() && !$this->getMatchingContexts($contexts, $requirement)) {
35         return FALSE;
36       }
37     }
38     return TRUE;
39   }
40
41   /**
42    * {@inheritdoc}
43    */
44   public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition) {
45     return array_filter($contexts, function (ContextInterface $context) use ($definition) {
46       $context_definition = $context->getContextDefinition();
47
48       // If the data types do not match, this context is invalid unless the
49       // expected data type is any, which means all data types are supported.
50       if ($definition->getDataType() != 'any' && $definition->getDataType() != $context_definition->getDataType()) {
51         return FALSE;
52       }
53
54       // If any constraint does not match, this context is invalid.
55       foreach ($definition->getConstraints() as $constraint_name => $constraint) {
56         if ($context_definition->getConstraint($constraint_name) != $constraint) {
57           return FALSE;
58         }
59       }
60
61       // All contexts with matching data type and contexts are valid.
62       return TRUE;
63     });
64   }
65
66   /**
67    * {@inheritdoc}
68    */
69   public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = []) {
70     /** @var $contexts \Drupal\Core\Plugin\Context\ContextInterface[] */
71     $mappings += $plugin->getContextMapping();
72     // Loop through each of the expected contexts.
73
74     $missing_value = [];
75
76     foreach ($plugin->getContextDefinitions() as $plugin_context_id => $plugin_context_definition) {
77       // If this context was given a specific name, use that.
78       $context_id = isset($mappings[$plugin_context_id]) ? $mappings[$plugin_context_id] : $plugin_context_id;
79       if (!empty($contexts[$context_id])) {
80         // This assignment has been used, remove it.
81         unset($mappings[$plugin_context_id]);
82
83         // Plugins have their on context objects, only the value is applied.
84         // They also need to know about the cacheability metadata of where that
85         // value is coming from, so pass them through to those objects.
86         $plugin_context = $plugin->getContext($plugin_context_id);
87         if ($plugin_context instanceof ContextInterface && $contexts[$context_id] instanceof CacheableDependencyInterface) {
88           $plugin_context->addCacheableDependency($contexts[$context_id]);
89         }
90
91         // Pass the value to the plugin if there is one.
92         if ($contexts[$context_id]->hasContextValue()) {
93           $plugin->setContextValue($plugin_context_id, $contexts[$context_id]->getContextData());
94         }
95         elseif ($plugin_context_definition->isRequired()) {
96           // Collect required contexts that exist but are missing a value.
97           $missing_value[] = $plugin_context_id;
98         }
99       }
100       elseif ($plugin_context_definition->isRequired()) {
101         // Collect required contexts that are missing.
102         $missing_value[] = $plugin_context_id;
103       }
104       else {
105         // Ignore mappings for optional missing context.
106         unset($mappings[$plugin_context_id]);
107       }
108     }
109
110     // If there are any required contexts without a value, throw an exception.
111     if ($missing_value) {
112       throw new ContextException(sprintf('Required contexts without a value: %s.', implode(', ', $missing_value)));
113     }
114
115     // If there are any mappings that were not satisfied, throw an exception.
116     if (!empty($mappings)) {
117       throw new ContextException('Assigned contexts were not satisfied: ' . implode(',', array_keys($mappings)));
118     }
119   }
120
121 }