You are here

public function Blocache::cacheContexts in Blocache (Block Cache Control) 8

Gets the available cache contexts.

Return value

array Returns the available cache contexts.

File

src/Blocache.php, line 16

Class

Blocache
Class Blocache.

Namespace

Drupal\blocache

Code

public function cacheContexts() {
  $contexts = [];
  $container = \Drupal::getContainer();
  $services = $container
    ->getServiceIds();
  foreach ($services as $id) {
    if (strpos($id, 'cache_context.') !== 0) {
      continue;
    }
    $service = $container
      ->get($id);
    $class_name = get_class($service);
    $class = new \ReflectionClass($class_name);
    $parameters = $class
      ->getMethod('getContext')
      ->getParameters();
    $params = [];
    foreach ($parameters as $param) {
      $params[] = $param->name;
    }
    $context = substr($id, 14);
    $contexts[$context] = [
      'id' => $context,
      'params' => $params,
    ];
  }
  return $contexts;
}