ContextHandler.php in Drupal 10
File
core/lib/Drupal/Core/Plugin/Context/ContextHandler.php
View source
<?php
namespace Drupal\Core\Plugin\Context;
use Drupal\Component\Plugin\Definition\ContextAwarePluginDefinitionInterface;
use Drupal\Component\Plugin\Exception\ContextException;
use Drupal\Component\Plugin\Exception\MissingValueContextException;
use Drupal\Core\Cache\CacheableDependencyInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
class ContextHandler implements ContextHandlerInterface {
public function filterPluginDefinitionsByContexts(array $contexts, array $definitions) {
$checked_requirements = [];
return array_filter($definitions, function ($plugin_definition) use ($contexts, &$checked_requirements) {
$context_definitions = $this
->getContextDefinitions($plugin_definition);
if ($context_definitions) {
$context_definitions_key = hash('sha256', serialize($context_definitions));
if (!isset($checked_requirements[$context_definitions_key])) {
$checked_requirements[$context_definitions_key] = $this
->checkRequirements($contexts, $context_definitions);
}
return $checked_requirements[$context_definitions_key];
}
return TRUE;
});
}
protected function getContextDefinitions($plugin_definition) {
if ($plugin_definition instanceof ContextAwarePluginDefinitionInterface) {
return $plugin_definition
->getContextDefinitions();
}
if (is_array($plugin_definition) && isset($plugin_definition['context_definitions'])) {
return $plugin_definition['context_definitions'];
}
return NULL;
}
public function checkRequirements(array $contexts, array $requirements) {
foreach ($requirements as $requirement) {
if ($requirement
->isRequired() && !$this
->getMatchingContexts($contexts, $requirement)) {
return FALSE;
}
}
return TRUE;
}
public function getMatchingContexts(array $contexts, ContextDefinitionInterface $definition) {
return array_filter($contexts, function (ContextInterface $context) use ($definition) {
return $definition
->isSatisfiedBy($context);
});
}
public function applyContextMapping(ContextAwarePluginInterface $plugin, $contexts, $mappings = []) {
$mappings += $plugin
->getContextMapping();
$missing_value = [];
foreach ($plugin
->getContextDefinitions() as $plugin_context_id => $plugin_context_definition) {
$context_id = $mappings[$plugin_context_id] ?? $plugin_context_id;
if (!empty($contexts[$context_id])) {
unset($mappings[$plugin_context_id]);
$plugin_context = $plugin
->getContext($plugin_context_id);
if ($plugin_context instanceof ContextInterface && $contexts[$context_id] instanceof CacheableDependencyInterface) {
$plugin_context
->addCacheableDependency($contexts[$context_id]);
}
if ($contexts[$context_id]
->hasContextValue()) {
$plugin
->setContext($plugin_context_id, $contexts[$context_id]);
}
elseif ($plugin_context_definition
->isRequired()) {
$missing_value[] = $plugin_context_id;
}
continue;
}
try {
$context = $plugin
->getContext($context_id);
} catch (ContextException $e) {
$context = NULL;
}
if ($context && $context
->hasContextValue()) {
unset($mappings[$plugin_context_id]);
continue;
}
if ($plugin_context_definition
->isRequired()) {
$missing_value[] = $plugin_context_id;
continue;
}
unset($mappings[$plugin_context_id]);
}
if (!empty($mappings)) {
throw new ContextException('Assigned contexts were not satisfied: ' . implode(',', array_keys($mappings)));
}
if ($missing_value) {
throw new MissingValueContextException($missing_value);
}
}
}