Commit f455d71f authored by tstoeckler's avatar tstoeckler

Issue #958162: Add pre-detect callback group. *API CHANGE*

parent e6f845f4
Libraries 7.x-2.x, xxxx-xx-xx
-----------------------------
#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.
#958162 by tstoeckler, sun, good_man: Allow to apply callbacks to libraries.
......
......@@ -118,11 +118,14 @@
* - $variant: If the $library array belongs to a certain variant (see
* above), a string containing the variant name. NULL, otherwise.
* Valid callback groups are:
* - prepare: Callbacks registered in this group are applied as soon as the
* library information has been retrieved via hook_libraries_info() or
* info files.
* - detect: Callbacks registered in this group are applied as soon as
* library detection has been completed. At this point the library
* - info: Callbacks registered in this group are applied after the library
* information has been retrieved via hook_libraries_info() or info files.
* - pre-detect: Callbacks registered in this group are applied after the
* library path has been determined and before the version callback is
* invoked. At this point the following additional information is available:
* - $library['library path']: The path on the file system to the library.
* - post-detect: Callbacks registered in this group are applied after the
* library has been successfully detected. At this point the library
* contains the version-specific information, if specified, and following
* additional information is available:
* - $library['installed']: A boolean indicating whether the library is
......
......@@ -155,7 +155,7 @@ function libraries_scan_info_files() {
*
* @param $group
* A string containing the group of callbacks that is to be applied. Should be
* either 'prepare', 'detect', or 'load'.
* either 'info', 'pre-detect', 'post-detect', or 'load'.
* @param $library
* An array of library information, passed by reference.
*/
......@@ -250,9 +250,9 @@ function libraries_info($name = NULL) {
// Allow modules to alter the registered libraries.
drupal_alter('libraries_info', $libraries);
// Invoke callbacks in the 'prepare' group.
// Invoke callbacks in the 'info' group.
foreach ($libraries as &$properties) {
libraries_invoke('prepare', $properties);
libraries_invoke('info', $properties);
}
}
......@@ -287,8 +287,9 @@ function libraries_info_defaults(&$library, $name) {
'callbacks' => array(),
);
$library['callbacks'] += array(
'prepare' => array(),
'detect' => array(),
'info' => array(),
'pre-detect' => array(),
'post-detect' => array(),
'load' => array(),
);
return $library;
......@@ -342,6 +343,9 @@ function libraries_detect($name) {
return $library;
}
// Invoke callbacks in the 'pre-detect' group.
libraries_invoke('pre-detect', $library);
// Detect library version, if not hardcoded.
if (!isset($library['version'])) {
// We support both a single parameter, which is an associative array, and an
......@@ -417,8 +421,8 @@ function libraries_detect($name) {
// If we end up here, the library should be usable.
$library['installed'] = TRUE;
// Invoke callbacks in the 'detect' group.
libraries_invoke('detect', $library);
// Invoke callbacks in the 'post-detect' group.
libraries_invoke('post-detect', $library);
return $library;
}
......
......@@ -148,57 +148,66 @@ class LibrariesTestCase extends DrupalWebTestCase {
'1' => array(
'variants' => array(
'example_variant' => array(
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'variants' => array(
'example_variant' => array(
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'callbacks' => array(
'prepare' => array('_libraries_test_prepare_callback'),
'detect' => array('_libraries_test_detect_callback'),
'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'),
),
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
'module' => 'libraries_test',
);
libraries_info_defaults($expected, 'example_callback');
// Test a callback in the 'prepare' phase.
$expected['prepare callback'] = 'applied (top-level)';
$expected['versions']['1']['prepare callback'] = 'applied (version 1)';
$expected['versions']['1']['variants']['example_variant']['prepare callback'] = 'applied (version 1, variant example_variant)';
$expected['variants']['example_variant']['prepare callback'] = 'applied (variant example_variant)';
// Test a callback in the 'info' group.
$expected['info callback'] = 'applied (top-level)';
$expected['versions']['1']['info callback'] = 'applied (version 1)';
$expected['versions']['1']['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
$expected['variants']['example_variant']['info callback'] = 'applied (variant example_variant)';
$library = libraries_info('example_callback');
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
$this->assertEqual($library, $expected, 'Prepare callback was applied correctly.');
// Test a callback in the 'detect' phase.
// Test a callback in the 'pre-detect' and 'post-detect' phase.
// Successfully detected libraries should only contain version information
// for the detected version and thus, be marked as installed.
unset($expected['versions']);
$expected['installed'] = TRUE;
// Additionally, version-specific properties of the detected version are
// supposed to override the corresponding top-level properties.
$expected['prepare callback'] = 'applied (version 1)';
$expected['info callback'] = 'applied (version 1)';
$expected['variants']['example_variant']['installed'] = TRUE;
$expected['variants']['example_variant']['prepare callback'] = 'applied (version 1, variant example_variant)';
$expected['detect callback'] = 'applied (top-level)';
$expected['variants']['example_variant']['detect callback'] = 'applied (variant example_variant)';
$expected['variants']['example_variant']['info callback'] = 'applied (version 1, variant example_variant)';
// Version-overloading takes place after the 'pre-detect' callbacks have
// been applied.
$expected['pre-detect callback'] = 'applied (version 1)';
$expected['post-detect callback'] = 'applied (top-level)';
$expected['variants']['example_variant']['pre-detect callback'] = 'applied (version 1, variant example_variant)';
$expected['variants']['example_variant']['post-detect callback'] = 'applied (variant example_variant)';
$library = libraries_detect('example_callback');
$this->verbose('Expected:<pre>' . var_export($expected, TRUE) . '</pre>');
$this->verbose('Actual:<pre>' . var_export($library, TRUE) . '</pre>');
......@@ -218,8 +227,9 @@ class LibrariesTestCase extends DrupalWebTestCase {
drupal_static_reset('libraries_load');
// Successfully loaded library variants are supposed to contain the specific
// variant information only.
$expected['prepare callback'] = 'applied (version 1, variant example_variant)';
$expected['detect callback'] = 'applied (variant example_variant)';
$expected['info callback'] = 'applied (version 1, variant example_variant)';
$expected['pre-detect callback'] = 'applied (version 1, variant example_variant)';
$expected['post-detect callback'] = 'applied (variant example_variant)';
$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>');
......
......@@ -210,33 +210,38 @@ function libraries_test_libraries_info() {
'variants' => array(
'example_variant' => array(
// These keys are for testing purposes only.
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
// These keys are for testing purposes only.
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'variants' => array(
'example_variant' => array(
// These keys are for testing purposes only.
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
),
),
'callbacks' => array(
'prepare' => array('_libraries_test_prepare_callback'),
'detect' => array('_libraries_test_detect_callback'),
'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'),
),
// These keys are for testing purposes only.
'prepare callback' => 'not applied',
'detect callback' => 'not applied',
'info callback' => 'not applied',
'pre-detect callback' => 'not applied',
'post-detect callback' => 'not applied',
'load callback' => 'not applied',
);
......@@ -322,45 +327,36 @@ function _libraries_test_return_installed($library, $name, $installed) {
}
/**
* Sets the 'prepare callback' key.
* Sets the 'info callback' key.
*
* This function is used as a test callback for the 'prepare' callback group.
*
* @param $library
* An array of library information, which may be version- or variant-specific.
* Passed by reference.
* @param $version
* The version the library information passed in $library belongs to, or NULL
* if the passed library information is not version-specific.
* @param $variant
* The variant the library information passed in $library belongs to, or NULL
* if the passed library information is not variant-specific.
* This function is used as a test callback for the 'info' callback group.
*
* @see _libraries_test_callback()
*/
function _libraries_test_prepare_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'prepare');
function _libraries_test_info_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'info');
}
/**
* Sets the 'detect callback' key.
* Sets the 'pre-detect callback' key.
*
* This function is used as a test callback for the 'detect' callback group.
* This function is used as a test callback for the 'pre-detect' callback group.
*
* @param $library
* An array of library information, which may be version- or variant-specific.
* Passed by reference.
* @param $version
* The version the library information passed in $library belongs to, or NULL
* if the passed library information is not version-specific.
* @param $variant
* The variant the library information passed in $library belongs to, or NULL
* if the passed library information is not variant-specific.
* @see _libraries_test_callback()
*/
function _libraries_test_pre_detect_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'pre-detect');
}
/**
* Sets the 'post-detect callback' key.
*
* This function is used as a test callback for the 'post-detect callback group.
*
* @see _libraries_test_callback()
*/
function _libraries_test_detect_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'detect');
function _libraries_test_post_detect_callback(&$library, $version, $variant) {
_libraries_test_callback($library, $version, $variant, 'post-detect');
}
/**
......@@ -368,16 +364,6 @@ function _libraries_test_detect_callback(&$library, $version, $variant) {
*
* This function is used as a test callback for the 'load' callback group.
*
* @param $library
* An array of library information, which may be version- or variant-specific.
* Passed by reference.
* @param $version
* The version the library information passed in $library belongs to, or NULL
* if the passed library information is not version-specific.
* @param $variant
* The variant the library information passed in $library belongs to, or NULL
* if the passed library information is not variant-specific.
*
* @see _libraries_test_callback()
*/
function _libraries_test_load_callback(&$library, $version, $variant) {
......
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