You are here

function _libraries_test_get_version in Libraries API 8.3

Gets the version information from an arbitrary library.

Test function for a version callback with multiple arguments. This is an exact copy of libraries_get_version(), which uses a single $option argument, except for the fact that it uses multiple arguments. Since we support both type of version callbacks, detecting the version of a test library with this function ensures that the arguments are passed correctly. This function might be a useful reference for a custom version callback that uses multiple parameters.

Parameters

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

$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 (\d+)\.(\d+)/'.

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.

See also

libraries_get_version()

1 string reference to '_libraries_test_get_version'
libraries_test_libraries_info in tests/modules/libraries_test/libraries_test.module
Implements hook_libraries_info().

File

tests/modules/libraries_test/libraries_test.module, line 341
Tests the library detection and loading.

Code

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