Skip to content

Instantly share code, notes, and snippets.

@Berdir
Created March 11, 2015 12:12
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Berdir/fae8136a615a59abd3ea to your computer and use it in GitHub Desktop.
Save Berdir/fae8136a615a59abd3ea to your computer and use it in GitHub Desktop.
Minify tags
<?php
/**
* Minifies and removes unneeded cache tags.
*
* @param array $cache_tags
* List of cache tags.
*
* @return array
* Shortened and cleaned up cache tag list.
*/
public function minifyTags(array $cache_tags) {
$minified_tags = [];
$has_node_list_tag = in_array('node_list', $cache_tags);
$has_block_list_tag = in_array('config:block_list', $cache_tags);
$add_menu_list_tag = FALSE;
foreach ($cache_tags as $tag) {
// Skip specific node tags if the list cache tag is present.
if ($has_node_list_tag && strpos($tag, 'node:') !== FALSE) {
continue;
}
// Skip specific block cache tags if the list cache tag is present.
if ($has_block_list_tag && strpos($tag, 'config:block.block.') !== FALSE) {
continue;
}
// Skip specific node tags if the list cache tag is present.
if (strpos($tag, 'config:system.menu.') !== FALSE) {
$add_menu_list_tag = TRUE;
continue;
}
$minified_tags[] = $tag;
};
// @todo Replace this with a fancy algorithm.
$replacements = [
'config:' => 'c:',
'block_content:' => 'bc:',
'page_manager.page.' => 'pmp.',
'file:' => 'f:',
'taxonomy_term:' => 'tm:',
'theme_global_setting' => 'tgs',
'user:' => 'u:',
];
// Shorten known cache tags.
$minified_tags = str_replace(array_keys($replacements), array_values($replacements), $minified_tags);
if ($add_menu_list_tag) {
$minified_tags[] = 'c:menu_list';
}
return $minified_tags;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment