Commit 49d97eae authored by Aegir user's avatar Aegir user

Issue #1023258 by tstoeckler: Make 'files' consistently keyed by filename.

parent f455d71f
Libraries 7.x-2.x, xxxx-xx-xx
-----------------------------
#1023258 by tstoeckler: Make 'files' consistently keyed by filename.
#958162 by sun, tstoeckler: Add pre-detect callback group.
#958162 by sun, tstoeckler: Make tests debuggable and provide libraries_info_defaults().
#961476 by tstoeckler: Changed libraries_get_path() to return FALSE by default.
......
......@@ -114,9 +114,11 @@
* library with a certain part of the libraries array passed as $library
* each time.
* - $version: If the $library array belongs to a certain version (see
* above), a string containing the version. NULL, otherwise.
* above), a string containing the version. This argument may be empty, so
* NULL should be specified as the default value.
* - $variant: If the $library array belongs to a certain variant (see
* above), a string containing the variant name. NULL, otherwise.
* above), a string containing the variant name. This argument may be
* empty, so NULL should be specified as the default value.
* Valid callback groups are:
* - info: Callbacks registered in this group are applied after the library
* information has been retrieved via hook_libraries_info() or info files.
......
......@@ -203,6 +203,64 @@ function libraries_traverse_library(&$library, $callback) {
}
}
/**
* Helper callback to make 'files' property of libraries consistent.
*
* This turns libraries' file information declared as e.g.
* @code
* $library['files']['js'] = array('example_1.js', 'example_2.js');
* @endcode
* into
* @code
* $library['files']['js'] = array(
* 'example_1.js' => array(),
* 'example_2.js' => array(),
* );
* @endcode
* It does the same for the 'integration files' property.
*
* @param $library
* An associative array of library information or a part of it, passed by
* reference.
* @param $version
* If the library information belongs to a specific version, the version
* string. NULL otherwise.
* @param $variant
* If the library information belongs to a specific variant, the variant name.
* NULL otherwise.
*
* @see libraries_info()
* @see libraries_invoke()
*/
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();
if (isset($library['files'])) {
$file_types[] = &$library['files'];
}
if (isset($library['integration files'])) {
// Integration files are additionally keyed by module.
foreach ($library['integration files'] as &$integration_files) {
$file_types[] = &$integration_files;
}
}
foreach ($file_types as &$files) {
// Go through all supported types of files.
foreach (array('js', 'css', 'php') as $type) {
if (isset($files[$type])) {
foreach ($files[$type] as $key => $value) {
// Unset numeric keys and turn the respective values into keys.
if (is_numeric($key)) {
$files[$type][$value] = array();
unset($files[$type][$key]);
}
}
}
}
}
}
/**
* Returns information about registered libraries.
*
......@@ -292,6 +350,10 @@ function libraries_info_defaults(&$library, $name) {
'post-detect' => array(),
'load' => array(),
);
// Add our own callbacks before any others.
$library['callbacks']['info'] = array_merge(array('libraries_prepare_files'), $library['callbacks']['info']);
return $library;
}
......@@ -548,7 +610,7 @@ function libraries_load_files($library) {
// Load PHP files.
if (!empty($library['files']['php'])) {
foreach ($library['files']['php'] as $file) {
foreach ($library['files']['php'] as $file => $array) {
$file_path = DRUPAL_ROOT . '/' . $path . '/' . $file;
if (file_exists($file_path)) {
require_once $file_path;
......
......@@ -32,15 +32,33 @@ class LibrariesTestCase extends DrupalWebTestCase {
// Test libraries_get_path().
$this->assertEqual(libraries_get_path('example'), FALSE, 'libraries_get_path() returns FALSE for a missing library.');
// Test libraries_prepare_files().
$expected = array(
'files' => array(
'js' => array('example.js' => array()),
'css' => array('example.css' => array()),
'php' => array('example.php' => array()),
),
);
$library = array(
'files' => array(
'js' => array('example.js'),
'css' => array('example.css'),
'php' => array('example.php'),
),
);
libraries_prepare_files($library, NULL, NULL);
$this->assertEqual($expected, $library, 'libraries_prepare_files() works correctly.');
// Test that library information is found correctly.
$expected = array(
'name' => 'Example files',
'library path' => drupal_get_path('module', 'libraries') . '/tests/example',
'version' => '1',
'files' => array(
'js' => array('example_1.js'),
'css' => array('example_1.css'),
'php' => array('example_1.php'),
'js' => array('example_1.js' => array()),
'css' => array('example_1.css' => array()),
'php' => array('example_1.php' => array()),
),
'module' => 'libraries_test',
);
......@@ -107,9 +125,9 @@ class LibrariesTestCase extends DrupalWebTestCase {
// Test a top-level files property.
$library = libraries_detect('example_files');
$files = array(
'js' => array('example_1.js'),
'css' => array('example_1.css'),
'php' => array('example_1.php'),
'js' => array('example_1.js' => array()),
'css' => array('example_1.css' => array()),
'php' => array('example_1.php' => array()),
);
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library['files'], $files, 'Top-level files property works.');
......@@ -117,9 +135,9 @@ class LibrariesTestCase extends DrupalWebTestCase {
// Test version-specific library files.
$library = libraries_detect('example_versions');
$files = array(
'js' => array('example_2.js'),
'css' => array('example_2.css'),
'php' => array('example_2.php'),
'js' => array('example_2.js' => array()),
'css' => array('example_2.css' => array()),
'php' => array('example_2.php' => array()),
);
$this->verbose('<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library['files'], $files, 'Version-specific library files found.');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment