function pm_parse_version

8.0.x pm.drush.inc pm_parse_version($version, $is_core = FALSE)
7.x pm.drush.inc pm_parse_version($version, $is_core = FALSE)
master pm.drush.inc pm_parse_version($version, $is_core = FALSE)

Parses a version string and returns its components.

It parses both core and contrib version strings.

Core (semantic versioning):

  • 8.0.0-beta3+252-dev
  • 8.0.0-beta2
  • 8.0.x-dev
  • 8.1.x
  • 8.0.1
  • 8

Core (classic drupal scheme):

  • 7.x-dev
  • 7.x
  • 7.33
  • 7.34+3-dev
  • 7

Contrib:

  • 7.x-1.0-beta1+30-dev
  • 7.x-1.0-beta1
  • 7.x-1.0+30-dev
  • 7.x-1.0
  • 1.0-beta1
  • 1.0
  • 7.x-1.x
  • 7.x-1.x-dev
  • 1.x

Parameters

string $version: A core or project version string.

bool $is_core: Whether this is a core version or a project version.

Return value

array Version string in parts. Example for a contrib version (ex: 7.x-3.2-beta1):

  • version : Fully qualified version string.
  • drupal_version : Core compatibility version (ex: 7.x).
  • version_major : Major version (ex: 3).
  • version_minor : Minor version. Not applicable. Always empty.
  • version_patch : Patch version (ex: 2).
  • version_extra : Extra version (ex: beta1).
  • project_version : Project specific part of the version (ex: 3.2-beta1).

Example for a core version (ex: 8.1.2-beta2 or 7.0-beta2):

  • version : Fully qualified version string.
  • drupal_version : Core compatibility version (ex: 8.x).
  • version_major : Major version (ex: 8).
  • version_minor : Minor version (ex: 1). Empty if not a semver.
  • version_patch : Patch version (ex: 2).
  • version_extra : Extra version (ex: beta2).
  • project_version : Same as 'version'.

See also

pm_parse_request()

6 calls to pm_parse_version()
pmRequestUnitCase::testVersionParserContrib in tests/pmRequestUnitTest.php
Tests for pm_parse_version() with project versions.
pmRequestUnitCase::testVersionParserCore in tests/pmRequestUnitTest.php
Tests for pm_parse_version() with drupal version scheme for core.
pmRequestUnitCase::testVersionParserCoreSemVer in tests/pmRequestUnitTest.php
Tests for pm_parse_version() with semantic versioning.
pm_parse_request in commands/pm/pm.drush.inc
Parse out the project name and version and return as a structured array.
Project::filterReleases in lib/Drush/UpdateService/Project.php
Filter project releases by a criteria and returns a list.

... See full list

File

commands/pm/pm.drush.inc, line 1570
The drush Project Manager

Code

function pm_parse_version($version, $is_core = FALSE) {
  $core_parts = _pm_parse_version_decompound($version);

  // If no major version, we have no version at all. Pick a default.
  $drupal_version_default = drush_drupal_major_version();
  if ($core_parts['major'] == '') {
    $core_parts['major'] = ($drupal_version_default) ? $drupal_version_default : drush_get_option('default-major', 8);
  }

  if ($is_core) {
    $project_version = _pm_parse_version_compound($core_parts);
    $version_parts = array(
      'version' => $project_version,
      'drupal_version' => $core_parts['major'] . '.x',
      'project_version' => $project_version,
      'version_major' => $core_parts['major'],
      'version_minor' => $core_parts['minor'],
      'version_patch' => ($core_parts['patch'] == 'x') ? '' : $core_parts['patch'],
      'version_extra' => ($core_parts['patch'] == 'x') ? 'dev' : $core_parts['extra'],
      'version_offset' => $core_parts['offset'],
    );
  }
  else {
    // If something as 7.x-1.0-beta1, the project specific version is
    // in $version['extra'] and we need to parse it.
    if (strpbrk($core_parts['extra'], '.-')) {
      $nocore_parts = _pm_parse_version_decompound($core_parts['extra']);
      $nocore_parts['offset'] = $core_parts['offset'];
      $project_version = _pm_parse_version_compound($nocore_parts);
      $version_parts = array(
        'version' => $core_parts['major'] . '.x-' . $project_version,
        'drupal_version' => $core_parts['major'] . '.x',
        'project_version' => $project_version,
        'version_major' => $nocore_parts['major'],
        'version_minor' => $core_parts['minor'],
        'version_patch' => ($nocore_parts['patch'] == 'x') ? '' : $nocore_parts['patch'],
        'version_extra' => ($nocore_parts['patch'] == 'x') ? 'dev' : $nocore_parts['extra'],
        'version_offset' => $core_parts['offset'],
      );
    }
    // At this point we have half a version and must decide if this is a drupal major or a project.
    else {
      // If working on a bootstrapped site, core_parts has the project version.
      if ($drupal_version_default) {
        $project_version = _pm_parse_version_compound($core_parts);
        $version = ($project_version) ? $drupal_version_default . '.x-' . $project_version : '';
        $version_parts = array(
          'version' => $version,
          'drupal_version' => $drupal_version_default . '.x',
          'project_version' => $project_version,
          'version_major' => $core_parts['major'],
          'version_minor' => $core_parts['minor'],
          'version_patch' => ($core_parts['patch'] == 'x') ? '' : $core_parts['patch'],
          'version_extra' => ($core_parts['patch'] == 'x') ? 'dev' : $core_parts['extra'],
          'version_offset' => $core_parts['offset'],
        );
      }
      // Not working on a bootstrapped site, core_parts is core version.
      else {
        $version_parts = array(
          'version' => '',
          'drupal_version' => $core_parts['major'] . '.x',
          'project_version' => '',
          'version_major' => '',
          'version_minor' => '',
          'version_patch' => '',
          'version_extra' => '',
          'version_offset' => '',
        );
      }
    }
  }

  return $version_parts;
}