You are here

public static function ModuleVersion::createFromVersionString in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/update/src/ModuleVersion.php \Drupal\update\ModuleVersion::createFromVersionString()

Constructs a module version object from a version string.

Parameters

string $version_string: The version string.

Return value

\Drupal\update\ModuleVersion The module version instance.

Throws

\UnexpectedValueException Thrown when a legacy version string has a core prefix other than "8.x-" for example, version strings such as "7.x-1.0" are not supported.

5 calls to ModuleVersion::createFromVersionString()
ModuleVersion::createFromSupportBranch in core/modules/update/src/ModuleVersion.php
Constructs a module version object from a support branch.
ModuleVersionTest::testGetMajorVersion in core/modules/update/tests/src/Unit/ModuleVersionTest.php
@covers ::getMajorVersion
ModuleVersionTest::testGetVersionExtra in core/modules/update/tests/src/Unit/ModuleVersionTest.php
@covers ::getVersionExtra
ModuleVersionTest::testInvalidVersionCorePrefix in core/modules/update/tests/src/Unit/ModuleVersionTest.php
@covers ::createFromVersionString
ModuleVersionTest::testInvalidVersionNumber in core/modules/update/tests/src/Unit/ModuleVersionTest.php
@covers ::createFromVersionString

File

core/modules/update/src/ModuleVersion.php, line 55

Class

ModuleVersion
Provides a module version value object.

Namespace

Drupal\update

Code

public static function createFromVersionString($version_string) {
  $original_version = $version_string;
  if (strpos($version_string, static::CORE_PREFIX) === 0 && $version_string !== '8.x-dev') {
    $version_string = preg_replace('/8\\.x-/', '', $version_string, 1);
  }
  else {

    // Ensure the version string has no unsupported core prefixes.
    $dot_x_position = strpos($version_string, '.x-');
    if ($dot_x_position === 1 || $dot_x_position === 2) {
      $after_core_prefix = explode('.x-', $version_string)[1];
      if ($after_core_prefix !== 'dev') {
        throw new \UnexpectedValueException("Unexpected version core prefix in {$version_string}. The only core prefix expected in \\Drupal\\update\\ModuleVersion is: 8.x-");
      }
    }
  }
  $version_parts = explode('.', $version_string);
  $major_version = $version_parts[0];
  $version_parts_count = count($version_parts);
  $last_part_split = explode('-', $version_parts[count($version_parts) - 1]);
  $version_extra = count($last_part_split) === 1 ? NULL : $last_part_split[1];
  if ($version_parts_count > 3 || $version_parts_count < 2 || !is_numeric($major_version) || $version_parts_count === 3 && !is_numeric($version_parts[1]) || !is_numeric($last_part_split[0]) && $last_part_split !== 'x' && $version_extra !== 'dev') {
    throw new \UnexpectedValueException("Unexpected version number in: {$original_version}");
  }
  return new static($major_version, $version_extra);
}