diff --git a/core/modules/update/src/Form/UpdateManagerUpdate.php b/core/modules/update/src/Form/UpdateManagerUpdate.php index b4e50be9db..fb5385c026 100644 --- a/core/modules/update/src/Form/UpdateManagerUpdate.php +++ b/core/modules/update/src/Form/UpdateManagerUpdate.php @@ -136,7 +136,7 @@ public function buildForm(array $form, FormStateInterface $form_state) { $recommended_release = $project['releases'][$project['recommended']]; $recommended_version = '{{ release_version }} ({{ release_notes }})'; - $recommended_version_parser = new ModuleVersion($recommended_release['version']); + $recommended_version_parser = ModuleVersion::createFromVersionString($recommended_release['version']); if ($recommended_version_parser->getMajorVersion() != $project['existing_major']) { $recommended_version .= '
{{ major_update_warning_text }}
'; } diff --git a/core/modules/update/src/ModuleVersion.php b/core/modules/update/src/ModuleVersion.php index 2982ca14ef..b43e63680a 100644 --- a/core/modules/update/src/ModuleVersion.php +++ b/core/modules/update/src/ModuleVersion.php @@ -10,11 +10,9 @@ class ModuleVersion { /** - * The version_string. - * - * @var string + * The core compatibility prefix used in version strings. */ - protected $version; + const CORE_COMPATIBILITY_PREFIX = \Drupal::CORE_COMPATIBILITY . '-'; /** * The major version. @@ -45,28 +43,64 @@ class ModuleVersion { protected $versionExtra; /** - * Constructs a ModuleVersion object. + * Whether the core compatibility prefix should be used. + * + * @var bool + */ + protected $useCorePrefix; + + /** + * Constructs a module version object from a version string. * - * @param string $version + * @param string $version_string * The version string. + * + * @return \Drupal\update\ModuleVersion + * The module version instance. */ - public function __construct($version) { - $this->version = $version; - $version_parts = explode('.', $this->getVersionStringWithoutCoreCompatibility()); - $this->majorVersion = $version_parts[0]; + public static function createFromVersionString($version_string) { + $use_compatibility_prefix = strpos($version_string, static::CORE_COMPATIBILITY_PREFIX) === 0; + if ($use_compatibility_prefix) { + $version_string = str_replace(static::CORE_COMPATIBILITY_PREFIX, '', $version_string); + } + $version_parts = explode('.', $version_string); + $major_version = $version_parts[0]; if (count($version_parts) === 2) { $last_version_part = $version_parts[1]; - $this->minorVersion = NULL; + $minor_version = NULL; } else { $last_version_part = $version_parts[2]; - $this->minorVersion = $version_parts[1]; + $minor_version = $version_parts[1]; } $last_version_split = explode('-', $last_version_part); // If patch equals 'x' this instance was created from a branch and the patch // version cannot be determined. - $this->patchVersion = $last_version_split[0] === 'x' ? NULL : $last_version_split[0]; - $this->versionExtra = count($last_version_split) === 1 ? NULL : $last_version_split[1]; + $patch_version = $last_version_split[0] === 'x' ? NULL : $last_version_split[0]; + $version_extra = count($last_version_split) === 1 ? NULL : $last_version_split[1]; + return new static($major_version, $minor_version, $patch_version, $version_extra, $use_compatibility_prefix); + } + + /** + * Constructs a ModuleVersion object. + * + * @param string $major_version + * The major version. + * @param string|null $minor_version + * The minor version. + * @param string|null $patch_version + * The patch version. + * @param string|null $version_extra + * The extra version string. + * @param bool $use_core_compatibility_prefix + * Whether to use the core compatibility prefix. + */ + protected function __construct($major_version, $minor_version, $patch_version, $version_extra, $use_core_compatibility_prefix) { + $this->majorVersion = $major_version; + $this->minorVersion = $minor_version; + $this->patchVersion = $patch_version; + $this->versionExtra = $version_extra; + $this->useCorePrefix = $use_core_compatibility_prefix; } /** @@ -82,7 +116,7 @@ public function __construct($version) { * The module version instance. */ public static function createFromSupportBranch($branch) { - return new static($branch . 'x'); + return static::createFromVersionString($branch . 'x'); } /** @@ -115,16 +149,6 @@ public function getPatchVersion() { return $this->patchVersion; } - /** - * Gets the version string with the core compatibility prefix removed. - * - * @return string - * The version string. - */ - private function getVersionStringWithoutCoreCompatibility() { - return strpos($this->version, \Drupal::CORE_COMPATIBILITY) === 0 ? str_replace('8.x-', '', $this->version) : $this->version; - } - /** * Gets the version extra string at the end of the version number. * @@ -142,13 +166,12 @@ public function getVersionExtra() { * The support branch as is used in update XML files. */ public function getSupportBranch() { - $version = $this->version; - if ($extra = $this->getVersionExtra()) { - $version = str_replace("-$extra", '', $version); + $branch = $this->useCorePrefix ? static::CORE_COMPATIBILITY_PREFIX : ''; + $branch .= $this->majorVersion . '.'; + if ($this->minorVersion) { + $branch .= $this->minorVersion . '.'; } - $parts = explode('.', $version); - array_pop($parts); - return implode('.', $parts) . '.'; + return $branch; } } diff --git a/core/modules/update/tests/src/Unit/ModuleVersionTest.php b/core/modules/update/tests/src/Unit/ModuleVersionTest.php index dfe0ca8a26..53ec2079f0 100644 --- a/core/modules/update/tests/src/Unit/ModuleVersionTest.php +++ b/core/modules/update/tests/src/Unit/ModuleVersionTest.php @@ -18,7 +18,7 @@ class ModuleVersionTest extends UnitTestCase { * @dataProvider providerVersionInfos */ public function testGetMajorVersion($version, $expected_version_info) { - $version = new ModuleVersion($version); + $version = ModuleVersion::createFromVersionString($version); $this->assertSame($expected_version_info['major'], $version->getMajorVersion()); } @@ -28,7 +28,7 @@ public function testGetMajorVersion($version, $expected_version_info) { * @dataProvider providerVersionInfos */ public function testGetMinorVersion($version, $expected_version_info) { - $version = new ModuleVersion($version); + $version = ModuleVersion::createFromVersionString($version); $this->assertSame($expected_version_info['minor'], $version->getMinorVersion()); } @@ -38,7 +38,7 @@ public function testGetMinorVersion($version, $expected_version_info) { * @dataProvider providerVersionInfos */ public function testGetPatchVersion($version, $expected_version_info) { - $version = new ModuleVersion($version); + $version = ModuleVersion::createFromVersionString($version); $this->assertSame($expected_version_info['patch'], $version->getPatchVersion()); } @@ -48,7 +48,7 @@ public function testGetPatchVersion($version, $expected_version_info) { * @dataProvider providerVersionInfos */ public function testGetVersionExtra($version, $expected_version_info) { - $version = new ModuleVersion($version); + $version = ModuleVersion::createFromVersionString($version); $this->assertSame($expected_version_info['extra'], $version->getVersionExtra()); } @@ -58,7 +58,7 @@ public function testGetVersionExtra($version, $expected_version_info) { * @dataProvider providerVersionInfos */ public function testGetSupportBranch($version, $expected_version_info) { - $version = new ModuleVersion($version); + $version = ModuleVersion::createFromVersionString($version); $this->assertSame($expected_version_info['branch'], $version->getSupportBranch()); } diff --git a/core/modules/update/update.compare.inc b/core/modules/update/update.compare.inc index 27aaccb6a0..ab191fed5b 100644 --- a/core/modules/update/update.compare.inc +++ b/core/modules/update/update.compare.inc @@ -254,7 +254,7 @@ function update_calculate_project_update_status(&$project_data, $available) { } // Figure out the target major version. - $existing_module_version = new ModuleVersion($project_data['existing_version']); + $existing_module_version = ModuleVersion::createFromVersionString($project_data['existing_version']); $existing_major = $existing_module_version->getMajorVersion(); $supported_branches = []; if (isset($available['supported_branches'])) { @@ -307,7 +307,7 @@ function update_calculate_project_update_status(&$project_data, $available) { return; } foreach ($available['releases'] as $version => $release) { - $release_module_version = new ModuleVersion($release['version']); + $release_module_version = ModuleVersion::createFromVersionString($release['version']); // First, if this is the existing release, check a few conditions. if ($project_data['existing_version'] === $version) { if (isset($release['terms']['Release type']) &&