You are here

function ctools_css_cache in Chaos Tool Suite (ctools) 7

Same name and namespace in other branches
  1. 6 includes/css.inc \ctools_css_cache()

Write a chunk of CSS to a temporary cache file and return the file name.

This function optionally filters the CSS (always compressed, if so) and generates a unique filename based upon md5. It returns that filename that can be used with drupal_add_css(). Note that as a cache file, technically this file is volatile so it should be checked before it is used to ensure that it exists.

You can use file_exists() to test for the file and file_delete() to remove it if it needs to be cleared.

Parameters

$css: A chunk of well-formed CSS text to cache.

bool $filter: If TRUE the css will be filtered. If FALSE the text will be cached as-is.

Return value

string The filename the CSS will be cached in.

3 calls to ctools_css_cache()
CtoolsCSSObjectCache::testCssCache in tests/css_cache.test
Tests the custom CSS cache handler.
ctools_css_retrieve in includes/css.inc
Retrieve a filename associated with an id of previously cached CSS.
ctools_css_store in includes/css.inc
Store CSS with a given id and return the filename to use.
1 string reference to 'ctools_css_cache'
ctools_update_6002 in ./ctools.install
Add the new css cache table.

File

includes/css.inc, line 159
CSS filtering functions. Contains a disassembler, filter, compressor, and decompressor.

Code

function ctools_css_cache($css, $filter = TRUE) {
  if ($filter) {
    $css = ctools_css_filter($css);
  }

  // Create the css/ within the files folder.
  $path = 'public://ctools/css';
  if (!file_prepare_directory($path, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) {
    drupal_set_message(t('Unable to create CTools CSS cache directory. Check the permissions on your files directory.'), 'error');
    return;
  }

  // @todo Is this slow? Does it matter if it is?
  $filename = $path . '/' . md5($css) . '.css';

  // Generally md5 is considered unique enough to sign file downloads.
  // So this replaces already existing files based on the assumption that two
  // files with the same hash are identical content wise.
  // If we rename, the cache folder can potentially fill up with thousands of
  // files with the same content.
  $filename = file_unmanaged_save_data($css, $filename, FILE_EXISTS_REPLACE);
  return $filename;
}