/** * Tests the applying of callbacks. */ function testCallbacks() { $expected = array('name' => 'Example callback', 'library path' => drupal_get_path('module', 'libraries') . '/tests/example', 'version' => '1', 'versions' => array('1' => array('variants' => array('example_variant' => array('info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect 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', 'pre-load callback' => 'not applied', 'post-load callback' => 'not applied')), 'variants' => array('example_variant' => array('info callback' => 'not applied', 'pre-detect callback' => 'not applied', 'post-detect 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'), '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', 'pre-load callback' => 'not applied', 'post-load callback' => 'not applied', 'module' => 'libraries_test'); libraries_info_defaults($expected, 'example_callback'); // 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 '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']); $expected['installed'] = TRUE; // Additionally, version-specific properties of the detected version are // supposed to override the corresponding top-level properties. $expected['info callback'] = 'applied (version 1)'; $expected['variants']['example_variant']['installed'] = TRUE; $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>'); $this->assertEqual($library, $expected, 'Detect callback was applied correctly.'); // 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['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, '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 // variant information only. $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>'); $this->assertEqual($library, $expected, 'Pre-detect and post-detect callbacks were applied correctly to a variant.'); }
/** * Returns information about registered libraries. * * The returned information is unprocessed; i.e., as registered by plugins. * * @param $library * (optional) The machine name of a library to return registered information for. If omitted, information about all * registered libraries is returned. * * @return array|false * An associative array containing registered information for all libraries, the registered information for the * library specified by $name, or FALSE if the library $name is not registered. */ function &libraries_info($library = null) { // This static cache is re-used by libraries_detect() to save memory. static $libraries; if (!isset($libraries)) { $libraries = array(); $plugPrefs = e107::getPlugConfig('libraries')->getPref(); $addonsList = vartrue($plugPrefs['addon_list'], array()); // Gather information from libraries_info() in enabled plugins/themes. foreach ($addonsList as $type => $items) { foreach ($items as $name) { if ($type == 'plugin') { e107_require_once(e_PLUGIN . $name . '/e_libraries.php'); } if ($type == 'theme') { e107_require_once(e_THEME . $name . '/e_libraries.php'); } $addonClass = $name . '_libraries'; if (class_exists($addonClass)) { $class = new $addonClass(); if (method_exists($class, 'libraries_info')) { $info = $class->libraries_info(); if (is_array($info)) { foreach ($info as $machine_name => $properties) { if ($type == 'plugin') { $properties['info type'] = 'plugin'; $properties['plugin'] = $name; $libraries[$machine_name] = $properties; } if ($type == 'theme') { $properties['info type'] = 'theme'; $properties['theme'] = $name; $libraries[$machine_name] = $properties; } } } } } } } // Provide defaults. foreach ($libraries as $machine_name => &$properties) { libraries_info_defaults($properties, $machine_name); } // Allow enabled plugins/themes to alter the registered libraries. foreach ($addonsList as $type => $items) { foreach ($items as $name) { if ($type == 'plugin') { e107_require_once(e_PLUGIN . $name . '/e_libraries.php'); } if ($type == 'theme') { e107_require_once(e_THEME . $name . '/e_libraries.php'); } $addonClass = $name . '_libraries'; if (class_exists($addonClass)) { $class = new $addonClass(); if (method_exists($class, 'libraries_info_alter')) { $class->libraries_info_alter($libraries); } } } } // TODO: // Invoke callbacks in the 'info' group. foreach ($libraries as &$properties) { libraries_invoke('info', $properties); } } if (isset($library)) { if (!empty($libraries[$library])) { return $libraries[$library]; } else { $false = false; return $false; } } return $libraries; }