You are here

public function CacheMetadataExtractor::extractCacheMetadata in Twig Tweak 3.x

Same name and namespace in other branches
  1. 3.1.x src/CacheMetadataExtractor.php \Drupal\twig_tweak\CacheMetadataExtractor::extractCacheMetadata()

Extracts cache metadata from object or render array.

Parameters

\Drupal\Core\Cache\CacheableDependencyInterface|array $input: The cacheable object or render array.

Return value

array A render array with extracted cache metadata.

File

src/CacheMetadataExtractor.php, line 23

Class

CacheMetadataExtractor
Cache metadata extractor service.

Namespace

Drupal\twig_tweak

Code

public function extractCacheMetadata($input) : array {
  if ($input instanceof CacheableDependencyInterface) {
    $cache_metadata = CacheableMetadata::createFromObject($input);
  }
  elseif (is_array($input)) {
    $cache_metadata = self::extractFromArray($input);
  }
  else {
    $message = sprintf('The input should be either instance of %s or array. %s was given.', CacheableDependencyInterface::class, \get_debug_type($input));
    throw new \InvalidArgumentException($message);
  }
  $build = [];
  $cache_metadata
    ->applyTo($build);
  return $build;
}