You are here

function libraries_detect in Libraries API 8.3

Same name and namespace in other branches
  1. 7.3 libraries.module \libraries_detect()
  2. 7.2 libraries.module \libraries_detect()

Tries to detect a library and its installed version.

Parameters

$name: The machine name of a library to return registered information for.

Return value

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.

Deprecated

Will be removed before a stable Drupal 8 release. Please use the new library load and managment concepts described at: https://www.drupal.org/node/2170763

See also

libraries_info()

4 calls to libraries_detect()
LibrariesWebTest::testCallbacks in src/Tests/LibrariesWebTest.php
Tests the applying of callbacks.
LibrariesWebTest::testLibrariesDetect in src/Tests/LibrariesWebTest.php
Tests libraries_detect().
libraries_detect_dependencies in ./libraries.module
Library post-detect callback to process and detect dependencies.
libraries_load in ./libraries.module
Loads a library.

File

./libraries.module, line 547
External library handling for Drupal modules.

Code

function libraries_detect($name) {

  // Re-use the statically cached value of libraries_info() to save memory.
  $library =& libraries_info($name);
  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'] = 'not found';
    $library['error message'] = t('The %library library could not be found.', [
      '%library' => $library['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
    // indexed array of multiple parameters.
    if (isset($library['version arguments'][0])) {

      // Add the library as the first argument.
      $library['version'] = call_user_func_array($library['version callback'], array_merge([
        $library,
      ], $library['version arguments']));
    }
    else {
      $library['version'] = $library['version callback']($library, $library['version arguments']);
    }
    if (empty($library['version'])) {
      $library['error'] = 'not detected';
      $library['error message'] = t('The version of the %library library could not be detected.', [
        '%library' => $library['name'],
      ]);
      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'] = 'not supported';
      $library['error message'] = t('The installed version %version of the %library library is not supported.', [
        '%version' => $library['version'],
        '%library' => $library['name'],
      ]);
      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 {

        // We support both a single parameter, which is an associative array,
        // and an indexed array of multiple parameters.
        if (isset($variant['variant arguments'][0])) {

          // Add the library as the first argument, and the variant name as the
          // second.
          $variant['installed'] = call_user_func_array($variant['variant callback'], array_merge([
            $library,
            $variant_name,
          ], $variant['variant arguments']));
        }
        else {
          $variant['installed'] = $variant['variant callback']($library, $variant_name, $variant['variant arguments']);
        }
        if (!$variant['installed']) {
          $variant['error'] = 'not found';
          $variant['error message'] = t('The %variant variant of the %library library could not be found.', [
            '%variant' => $variant_name,
            '%library' => $library['name'],
          ]);
        }
      }
    }
  }

  // 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;
}