diff --git a/libraries.module b/libraries.module index c67ce79..b27bde8 100644 --- a/libraries.module +++ b/libraries.module @@ -9,10 +9,46 @@ * Implements hook_flush_caches(). */ function libraries_flush_caches() { + registry_rebuild(); return array('cache_libraries'); } /** + * Implements hook_registry_files_alter(). + */ +function libraries_registry_files_alter(&$files, $modules) { + // @see libraries_prepare_files(); + $autoload_files = drupal_static('libraries_autoload_files', array()); + + foreach ($autoload_files as $name => &$library_files) { + if (($library = libraries_detect($name)) && $library['installed']) { + foreach ($files as $filename => $info) { + $path = $library['library path']; + $path = ($library['path'] !== '' ? $path . '/' . $library['path'] : $path); + + $module = (isset($library['module']) ? $library['module'] : 'libraries'); + $module_weight = db_query("SELECT weight FROM {system} WHERE type = 'module' and name = :name", array(':name' => $module))->fetchField(); + + $info = array( + 'module' => (isset($library['module']) ? $library['module'] : 'libraries'), + 'weight' => (isset($info['weight']) ? $info['weight'] : $module_weight), + ); + + $library_files["$path/$filename"] = $info; + unset($library_files[$filename]); + } + } + // Make sure we do not try to autoload files for libraries that are not + // installed. + else { + unset($autoload_files[$name]); + } + } + + $files += $autoload_files; +} + +/** * Gets the path of a library. * * @param $name @@ -236,6 +272,9 @@ function libraries_prepare_files(&$library, $version = NULL, $variant = NULL) { // Both the 'files' property and the 'integration files' property contain file // declarations, and we want to make both consistent. $file_types = array(); + // @see libraries_registry_files_alter() + $autoload_files = drupal_static('libraries_autoload_files', array()); + if (isset($library['files'])) { $file_types[] = &$library['files']; } @@ -245,6 +284,7 @@ function libraries_prepare_files(&$library, $version = NULL, $variant = NULL) { $file_types[] = &$integration_files; } } + foreach ($file_types as &$files) { // Go through all supported types of files. foreach (array('js', 'css', 'php') as $type) { @@ -255,6 +295,10 @@ function libraries_prepare_files(&$library, $version = NULL, $variant = NULL) { $files[$type][$value] = array(); unset($files[$type][$key]); } + // Track files to autoload. + if (($type == 'php') && isset($value['autoload']) && $value['autoload']) { + $autoload_files[$name][] = $key; + } } } }