function javascript_libraries_cache in JavaScript Libraries Manager 7
Download/Synchronize/Cache tracking code file locally.
Stolen from google analytics module.
Parameters
$uri: The full URL to the external javascript file.
$sync_cached_file: Synchronize tracking code and update if remote file have changed.
Return value
mixed The path to the local javascript file on success, boolean FALSE on failure.
3 calls to javascript_libraries_cache()
- JavaScriptLibrariesCustomTestCase::testAddRemoveCustom in tests/
javascript_libraries.test - Test adding and removing custom URLs and files.
- javascript_libraries_add_js in ./
javascript_libraries.module - Helper function that calls drupal_add_js().
- javascript_libraries_cron in ./
javascript_libraries.module - Implements hook_cron().
File
- ./
javascript_libraries.module, line 297 - Toggle the inclusion of Drupal system libraries. Upload and reference custom libraries as well.
Code
function javascript_libraries_cache($uri, $sync_cached_file = FALSE) {
$path = 'public://javascript_libraries';
$file_destination = $path . '/js_' . drupal_hash_base64($uri) . '.js';
$file_exists = file_exists($file_destination);
// If the file exists, we can assume success.
$success = $file_exists;
if (!$file_exists || $sync_cached_file) {
// Download the library.
$result = drupal_http_request($uri);
if ($result->code != 200) {
watchdog('javascript_libraries', 'Error fetching remote file @uri HTTP code: @code.', array(
'@uri' => $uri,
'@code' => $result->code,
), WATCHDOG_ERROR);
return FALSE;
}
if ($file_exists) {
// Only do a (potentially somewhat slow/expensive) file write if the
// content changed.
$current_contents = file_get_contents($file_destination);
if ($result->data != $current_contents) {
// Save updated javascript file to disk.
$success = javascript_libraries_file_unmanaged_save_data($result->data, $file_destination);
watchdog('javascript_libraries', 'Locally cached javascript library has been updated.', array(), WATCHDOG_INFO);
drupal_clear_js_cache();
}
}
elseif (file_prepare_directory($path, FILE_CREATE_DIRECTORY)) {
// There is no need to flush JS here as core refreshes JS caches
// automatically if new files are added.
$success = javascript_libraries_file_unmanaged_save_data($result->data, $file_destination);
watchdog('javascript_libraries', 'Locally cached javascript library has been saved.', array(), WATCHDOG_INFO);
}
}
if ($success) {
// Return the local JS file path.
return $file_destination;
}
return FALSE;
}