You are here

function libraries_get_version in Libraries API 8.3

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

Gets the version information from an arbitrary library.

Parameters

$library: An associative array containing all information about the library.

$options: An associative array containing with the following keys:

  • file: The filename to parse for the version, relative to the library path. For example: 'docs/changelog.txt'.
  • pattern: A string containing a regular expression (PCRE) to match the library version. For example: '@version\s+([0-9a-zA-Z\.-]+)@'. Note that the returned version is not the match of the entire pattern (i.e. '@version 1.2.3' in the above example) but the match of the first sub-pattern (i.e. '1.2.3' in the above example).
  • lines: (optional) The maximum number of lines to search the pattern in. Defaults to 20.
  • cols: (optional) The maximum number of characters per line to take into account. Defaults to 200. In case of minified or compressed files, this prevents reading the entire file into memory.

Return value

A string containing the version of the library.

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_get_path()

1 string reference to 'libraries_get_version'
libraries_info_defaults in ./libraries.module
Applies default properties to a library definition.

File

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

Code

function libraries_get_version($library, $options) {

  // Provide defaults.
  $options += [
    'file' => '',
    'pattern' => '',
    'lines' => 20,
    'cols' => 200,
  ];
  $file = DRUPAL_ROOT . '/' . $library['library path'] . '/' . $options['file'];
  if (empty($options['file']) || !file_exists($file)) {
    return;
  }
  $file = fopen($file, 'r');
  while ($options['lines'] && ($line = fgets($file, $options['cols']))) {
    if (preg_match($options['pattern'], $line, $version)) {
      fclose($file);
      return $version[1];
    }
    $options['lines']--;
  }
  fclose($file);
}