Commit 3db1b3ed authored by tstoeckler's avatar tstoeckler

Issue #1321372 by Rob Loach: Provide a 'post-load' callback group.

parent e2472883
Libraries 7.x-2.x, xxxx-xx-xx
-----------------------------
#1321372 by Rob Loach: Provide a 'post-load' callback group.
#1205854 by tstoeckler, sun: Test library caching.
......
......@@ -159,10 +159,13 @@
* variant, a boolean indicating whether the variant is installed or
* not.
* Note that in this group the 'versions' property is no longer available.
* - load: Callbacks registered in this group are applied directly
* - pre-load: Callbacks registered in this group are applied directly
* before this library is loaded. At this point the library contains
* variant-specific information, if specified. Note that in this group the
* 'variants' property is no longer available.
* - post-load: Callbacks registered in this group are applied directly
* after this library is loaded. At this point, the library contains a
* 'loaded' key, which contains the number of files that were loaded.
* Additional top-level properties can be registered as needed.
*
* @see hook_library()
......@@ -275,11 +278,27 @@ function hook_libraries_info() {
),
),
// Optionally register callbacks to apply to the library during different
// stages of its lifetime ('callback groups'). Here, a callback is
// registered in the 'detect' group.
// stages of its lifetime ('callback groups').
'callbacks' => array(
'detect' => array(
'mymodule_example_detect_callback',
// Used to alter the info associated with the library.
'info' => array(
'mymodule_example_libraries_info_callback',
),
// Called before detecting the given library.
'pre-detect' => array(
'mymodule_example_libraries_predetect_callback',
),
// Called after detecting the library.
'post-detect' => array(
'mymodule_example_libraries_postdetect_callback',
),
// Called before the library is loaded.
'pre-load' => array(
'mymodule_example_libraries_preload_callback',
),
// Called after the library is loaded.
'post-load' => array(
'mymodule_example_libraries_postload_callback',
),
),
);
......
......@@ -398,7 +398,8 @@ function libraries_info_defaults(&$library, $name) {
'info' => array(),
'pre-detect' => array(),
'post-detect' => array(),
'load' => array(),
'pre-load' => array(),
'post-load' => array(),
);
// Add our own callbacks before any others.
......@@ -596,10 +597,14 @@ function libraries_load($name, $variant = NULL) {
}
}
// Invoke callbacks in the 'load' group.
libraries_invoke('load', $library);
// Invoke callbacks in the 'pre-load' group.
libraries_invoke('pre-load', $library);
// Load all the files associated with the library.
$library['loaded'] = libraries_load_files($library);
// Invoke callbacks in the 'post-load' group.
libraries_invoke('post-load', $library);
}
$loaded[$name] = $library;
}
......
......@@ -255,13 +255,15 @@ class LibrariesTestCase extends DrupalWebTestCase {
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
),
),
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
),
),
'variants' => array(
......@@ -269,19 +271,22 @@ class LibrariesTestCase extends DrupalWebTestCase {
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
),
),
'callbacks' => array(
'info' => array('_libraries_test_info_callback'),
'pre-detect' => array('_libraries_test_pre_detect_callback'),
'post-detect' => array('_libraries_test_post_detect_callback'),
'load' => array('_libraries_test_load_callback'),
'pre-load' => array('_libraries_test_pre_load_callback'),
'post-load' => array('_libraries_test_post_load_callback'),
),
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
'module' => 'libraries_test',
);
libraries_info_defaults($expected, 'example_callback');
......@@ -296,7 +301,7 @@ class LibrariesTestCase extends DrupalWebTestCase {
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library, $expected, 'Prepare callback was applied correctly.');
// Test a callback in the 'pre-detect' and 'post-detect' phase.
// Test a callback in the 'pre-detect' and 'post-detect' phases.
// Successfully detected libraries should only contain version information
// for the detected version and thus, be marked as installed.
unset($expected['versions']);
......@@ -317,16 +322,17 @@ class LibrariesTestCase extends DrupalWebTestCase {
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library, $expected, 'Detect callback was applied correctly.');
// Test a callback in the 'load' phase.
// Test a callback in the 'pre-load' and 'post-load' phases.
// Successfully loaded libraries should only contain information about the
// already loaded variant.
unset($expected['variants']);
$expected['loaded'] = 0;
$expected['load callback'] = 'applied (top-level)';
$expected['pre-load callback'] = 'applied (top-level)';
$expected['post-load callback'] = 'applied (top-level)';
$library = libraries_load('example_callback');
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library, $expected, 'Load callback was applied correctly.');
$this->assertEqual($library, $expected, 'Pre-load and post-load callbacks were applied correctly.');
// This is not recommended usually and is only used for testing purposes.
drupal_static_reset('libraries_load');
// Successfully loaded library variants are supposed to contain the specific
......@@ -337,7 +343,7 @@ class LibrariesTestCase extends DrupalWebTestCase {
$library = libraries_load('example_callback', 'example_variant');
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library, $expected, 'Load callback was applied correctly to a variant.');
$this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.');
// Test loading of a simple library with a top-level files property.
$this->drupalGet('libraries_test/files');
......@@ -370,14 +376,16 @@ class LibrariesTestCase extends DrupalWebTestCase {
$this->assertRaw('The <em>info</em> callback group was invoked.', 'Info callback invoked for uncached libraries.');
$this->assertRaw('The <em>pre-detect</em> callback group was invoked.', 'Pre-detect callback invoked for uncached libraries.');
$this->assertRaw('The <em>post-detect</em> callback group was invoked.', 'Post-detect callback invoked for uncached libraries.');
$this->assertRaw('The <em>load</em> callback group was invoked.', 'Load callback invoked for uncached libraries.');
// When the library information is cached only the load callback group should
// be invoked.
$this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for uncached libraries.');
$this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for uncached libraries.');
// When the library information is cached only the 'pre-load' and
// 'post-load' callback groups should be invoked.
$this->drupalGet('libraries_test/cache');
$this->assertNoRaw('The <em>info</em> callback group was not invoked.', 'Info callback not invoked for cached libraries.');
$this->assertNoRaw('The <em>pre-detect</em> callback group was not invoked.', 'Pre-detect callback not invoked for cached libraries.');
$this->assertNoRaw('The <em>post-detect</em> callback group was not invoked.', 'Post-detect callback not invoked for cached libraries.');
$this->assertRaw('The <em>load</em> callback group was invoked.', 'Load callback invoked for cached libraries.');
$this->assertRaw('The <em>pre-load</em> callback group was invoked.', 'Pre-load callback invoked for cached libraries.');
$this->assertRaw('The <em>post-load</em> callback group was invoked.', 'Post-load callback invoked for cached libraries.');
variable_set('libraries_test_cache', FALSE);
}
......
......@@ -245,14 +245,16 @@ function libraries_test_libraries_info() {
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
),
),
// These keys are for testing purposes only.
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
),
),
'variants' => array(
......@@ -261,20 +263,23 @@ function libraries_test_libraries_info() {
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
),
),
'callbacks' => array(
'info' => array('_libraries_test_info_callback'),
'pre-detect' => array('_libraries_test_pre_detect_callback'),
'post-detect' => array('_libraries_test_post_detect_callback'),
'load' => array('_libraries_test_load_callback'),
'pre-load' => array('_libraries_test_pre_load_callback'),
'post-load' => array('_libraries_test_post_load_callback'),
),
// These keys are for testing purposes only.
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'pre-load callback' => 'not applied',
'post-load callback' => 'not applied',
);
return $libraries;
......@@ -392,14 +397,25 @@ function _libraries_test_post_detect_callback(&$library, $version, $variant) {
}
/**
* Sets the 'load callback' key.
* Sets the 'pre-load callback' key.
*
* This function is used as a test callback for the 'load' callback group.
* This function is used as a test callback for the 'pre-load' callback group.
*
* @see _libraries_test_callback()
*/
function _libraries_test_load_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'load');
function _libraries_test_pre_load_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'pre-load');
}
/**
* Sets the 'post-load callback' key.
*
* This function is used as a test callback for the 'post-load' callback group.
*
* @see _libraries_test_callback()
*/
function _libraries_test_post_load_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'post-load');
}
/**
......
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