class CacheContexts in Render cache 7.2
Same name in this branch
- 7.2 src/Cache/CacheContexts.php \Drupal\render_cache\Cache\CacheContexts
- 7.2 lib/Drupal/Core/Cache/CacheContexts.php \Drupal\Core\Cache\CacheContexts
Defines the CacheContexts service.
Converts string placeholders into their final string values, to be used as a cache key.
Hierarchy
- class \Drupal\Core\Cache\CacheContexts
Expanded class hierarchy of CacheContexts
1 file declares its use of CacheContexts
- CacheContexts.php in src/
Cache/ CacheContexts.php - Contains \Drupal\render_cache\Cache\CacheContexts
File
- lib/
Drupal/ Core/ Cache/ CacheContexts.php, line 19 - Contains \Drupal\Core\Cache\CacheContexts.
Namespace
Drupal\Core\CacheView source
class CacheContexts {
/**
* The service container.
*
* @var \Symfony\Component\DependencyInjection\ContainerInterface
*/
protected $container;
/**
* Available cache contexts and corresponding labels.
*
* @var array
*/
protected $contexts;
/**
* Constructs a CacheContexts object.
*
* @param \Symfony\Component\DependencyInjection\ContainerInterface $container
* The current service container.
* @param array $contexts
* An array of key-value pairs, where the keys are service names (which also
* serve as the corresponding cache context token) and the values are the
* cache context labels.
*/
public function __construct(ContainerInterface $container, array $contexts) {
$this->container = $container;
$this->contexts = $contexts;
}
/**
* Provides an array of available cache contexts.
*
* @return array
* An array of available cache contexts.
*/
public function getAll() {
return $this->contexts;
}
/**
* Provides an array of available cache context labels.
*
* To be used in cache configuration forms.
*
* @return array
* An array of available cache contexts and corresponding labels.
*/
public function getLabels() {
$with_labels = array();
foreach ($this->contexts as $context) {
$with_labels[$context] = $this
->getService($context)
->getLabel();
}
return $with_labels;
}
/**
* Converts cache context tokens to string representations of the context.
*
* Cache keys may either be static (just strings) or tokens (placeholders
* that are converted to static keys by the @cache_contexts service, depending
* depending on the request). This is the default cache contexts service.
*
* @param array $keys
* An array of cache keys that may or may not contain cache context tokens.
*
* @return array
* A copy of the input, with cache context tokens converted.
*/
public function convertTokensToKeys(array $keys) {
$context_keys = array_intersect($keys, $this
->getAll());
$new_keys = $keys;
// Iterate over the indices instead of the values so that the order of the
// cache keys are preserved.
foreach (array_keys($context_keys) as $index) {
$new_keys[$index] = $this
->getContext($keys[$index]);
}
return $new_keys;
}
/**
* Provides the string representation of a cache context.
*
* @param string $context
* A cache context token of an available cache context service.
*
* @return string
* The string representation of a cache context.
*/
protected function getContext($context) {
return $this
->getService($context)
->getContext();
}
/**
* Retrieves a service from the container.
*
* @param string $service
* The ID of the service to retrieve.
*
* @return mixed
* The specified service.
*/
protected function getService($service) {
return $this->container
->get($service);
}
}
Members
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
CacheContexts:: |
protected | property | The service container. | 1 |
CacheContexts:: |
protected | property | Available cache contexts and corresponding labels. | |
CacheContexts:: |
public | function | Converts cache context tokens to string representations of the context. | |
CacheContexts:: |
public | function | Provides an array of available cache contexts. | |
CacheContexts:: |
protected | function | Provides the string representation of a cache context. | |
CacheContexts:: |
public | function | Provides an array of available cache context labels. | |
CacheContexts:: |
protected | function | Retrieves a service from the container. | |
CacheContexts:: |
public | function | Constructs a CacheContexts object. | 1 |