/** * Scan plugin directories and get information from e_libraries.php files. */ function pluginsPage() { e107_require_once(e_PLUGIN . 'libraries/libraries.php'); $mes = e107::getMessage(); $tpl = e107::getTemplate('libraries'); $sc = e107::getScBatch('libraries', true); $tp = e107::getParser(); $addonsList = libraries_update_addon_list(); $summ = count($addonsList); $message = str_replace('[summ]', $summ, LAN_PLUGIN_LIBRARIES_ADMIN_03); $mes->addInfo($message); $this->addTitle(LAN_PLUGIN_LIBRARIES_ADMIN_02); $output = $mes->render(); $libraries = libraries_info(); $output .= $tp->parseTemplate($tpl['TABLE']['HEADER']); foreach ($libraries as $machine_name => $info) { $details = libraries_detect($machine_name); $sc->setVars(array('name' => $details['name'], 'plugin' => varset($details['plugin'], false), 'theme' => varset($details['theme'], false), 'vendor' => $details['vendor url'], 'download' => $details['download url'], 'installed' => array('status' => $details['installed'], 'error' => varset($details['error'], ''), 'message' => varset($details['error message'], '')))); $output .= $tp->parseTemplate($tpl['TABLE']['ROW'], true, $sc); } $output .= $tp->parseTemplate($tpl['TABLE']['FOOTER']); return $output; }
/** * @see libraries_info() * * @throws \Exception * @return mixed */ function getLibrariesInfo() { if (!function_exists('libraries_info')) { // Libraries is at a lower version, which does not have this function. return array(); } # drupal_static_reset('libraries_info'); return libraries_info(); }
/** * 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.'); }
/** * Tries to detect a library and its installed version. * * @param $name * The machine name of a library to return registered information for. * * @return array|false * An associative array containing registered information for the library specified by $name, or FALSE if the library * $name is not registered. In addition to the keys returned by libraries_info(), the following keys are contained: * - installed: A boolean indicating whether the library is installed. Note that not only the top-level library, but * also each variant contains this key. * - version: If the version could be detected, the full version string. * - error: If an error occurred during library detection, one of the following error statuses: * "not found", "not detected", "not supported". * - error message: If an error occurred during library detection, a detailed error message. */ function libraries_detect($name) { // Re-use the statically cached value of libraries_info() to save memory. $library =& libraries_info($name); // Exit early if the library was not found. if ($library === false) { return $library; } // If 'installed' is set, library detection ran already. if (isset($library['installed'])) { return $library; } $library['installed'] = false; // Check whether the library exists. if (!isset($library['library path'])) { $library['library path'] = libraries_get_path($library['machine name']); } if ($library['library path'] === false || !file_exists($library['library path'])) { $library['error'] = LAN_PLUGIN_LIBRARIES_GLOBAL_09; $replace = array('[x]'); $replace_with = array($library['name']); $library['error message'] = str_replace($replace, $replace_with, LAN_PLUGIN_LIBRARIES_GLOBAL_03); return $library; } // TODO: // Invoke callbacks in the 'pre-detect' group. libraries_invoke('pre-detect', $library); // Detect library version, if not hardcoded. if (!isset($library['version'])) { if (varset($library['plugin'], false)) { e107_require_once(e_PLUGIN . $library['plugin'] . '/e_libraries.php'); $addonClass = $library['plugin'] . '_libraries'; } elseif (varset($library['theme'], false)) { e107_require_once(e_THEME . $library['theme'] . '/e_libraries.php'); $addonClass = $library['theme'] . '_libraries'; } // We support both a single parameter, which is an associative array, and an // indexed array of multiple parameters. if (isset($library['version arguments'][0])) { if (isset($addonClass) && class_exists($addonClass)) { $class = new $addonClass(); if (method_exists($class, $library['version callback'])) { // Add the library as the first argument. // Call PLUGIN_libraries::VARIANT_CALLBACK(). $variant['installed'] = call_user_func_array(array($class, $library['version callback']), array_merge(array($library), $library['version arguments'])); } } } else { if (isset($addonClass) && class_exists($addonClass)) { $class = new $addonClass(); if (method_exists($class, $library['version callback'])) { $library['version'] = $class->{$library}['version callback']($library, $library['version arguments']); } } } if (empty($library['version'])) { $library['error'] = LAN_PLUGIN_LIBRARIES_GLOBAL_10; $replace = array('[x]'); $replace_with = array($library['name']); $library['error message'] = str_replace($replace, $replace_with, LAN_PLUGIN_LIBRARIES_GLOBAL_04); return $library; } } // Determine to which supported version the installed version maps. if (!empty($library['versions'])) { ksort($library['versions']); $version = 0; foreach ($library['versions'] as $supported_version => $version_properties) { if (version_compare($library['version'], $supported_version, '>=')) { $version = $supported_version; } } if (!$version) { $library['error'] = LAN_PLUGIN_LIBRARIES_GLOBAL_11; $replace = array('[x]', '[y]'); $replace_with = array($library['version'], $library['name']); $library['error message'] = str_replace($replace, $replace_with, LAN_PLUGIN_LIBRARIES_GLOBAL_05); return $library; } // Apply version specific definitions and overrides. $library = array_merge($library, $library['versions'][$version]); unset($library['versions']); } // Check each variant if it is installed. if (!empty($library['variants'])) { foreach ($library['variants'] as $variant_name => &$variant) { // If no variant callback has been set, assume the variant to be installed. if (!isset($variant['variant callback'])) { $variant['installed'] = true; } else { if (varset($library['plugin'], false)) { e107_require_once(e_PLUGIN . $library['plugin'] . '/e_libraries.php'); $addonClass = $library['plugin'] . '_libraries'; } elseif (varset($library['theme'], false)) { e107_require_once(e_THEME . $library['theme'] . '/e_libraries.php'); $addonClass = $library['theme'] . '_libraries'; } // We support both a single parameter, which is an associative array, and an indexed array of multiple // parameters. if (isset($variant['variant arguments'][0])) { if (isset($addonClass) && class_exists($addonClass)) { $class = new $addonClass(); if (method_exists($class, $variant['variant callback'])) { // Add the library as the first argument, and the variant name as the second. // Call PLUGIN_libraries::VARIANT_CALLBACK(). $variant['installed'] = call_user_func_array(array($class, $variant['variant callback']), array_merge(array($library, $variant_name), $variant['variant arguments'])); } else { $variant['installed'] = true; } } else { $variant['installed'] = true; } } else { if (isset($addonClass) && class_exists($addonClass)) { $class = new $addonClass(); if (method_exists($class, $variant['variant callback'])) { $variant['installed'] = $class->{$variant}['variant callback']($library, $variant_name, $variant['variant arguments']); } else { $variant['installed'] = true; } } else { $variant['installed'] = true; } } if (!$variant['installed']) { $variant['error'] = LAN_PLUGIN_LIBRARIES_GLOBAL_09; $replace = array('[x]', '[y]'); $replace_with = array($variant_name, $library['name']); $variant['error message'] = str_replace($replace, $replace_with, LAN_PLUGIN_LIBRARIES_GLOBAL_06); } } } } // If we end up here, the library should be usable. $library['installed'] = true; // Invoke callbacks in the 'post-detect' group. libraries_invoke('post-detect', $library); return $library; }