ExtensionVersion.php in Drupal 9
File
core/lib/Drupal/Core/Extension/ExtensionVersion.php
View source
<?php
namespace Drupal\Core\Extension;
final class ExtensionVersion {
const CORE_PREFIX = '8.x-';
protected $majorVersion;
protected $minorVersion;
protected $versionExtra;
public static function createFromVersionString(string $version_string) : ExtensionVersion {
$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 {
$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\\Core\\Extension\\ExtensionVersion is: 8.x-");
}
}
}
$version_parts = explode('.', $version_string);
$major_version = $version_parts[0];
$version_parts_count = count($version_parts);
if ($version_parts_count === 2) {
$minor_version = NULL;
}
elseif ($version_parts_count === 3) {
$minor_version = $version_parts[1];
}
$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, $minor_version, $version_extra);
}
private function __construct(string $major_version, ?string $minor_version, ?string $version_extra) {
$this->majorVersion = $major_version;
$this->minorVersion = $minor_version;
$this->versionExtra = $version_extra;
}
public static function createFromSupportBranch(string $branch) : ExtensionVersion {
if (substr($branch, -1) !== '.') {
throw new \UnexpectedValueException("Invalid support branch: {$branch}");
}
return static::createFromVersionString($branch . '0');
}
public function getMajorVersion() : string {
return $this->majorVersion;
}
public function getMinorVersion() : ?string {
return $this->minorVersion;
}
public function getVersionExtra() : ?string {
return $this->versionExtra;
}
}