final class version_support

internal  
 

Infrastructure service to normalize and compare Moodle Core versions.

This class normalizes core data to a "semver-like" format (x.y.z) and exposes convenient comparators (>=, ranges, feature gate via declarative matrix). Values are cached in memory after the first read from {\local_middag\framework\support\moodle\bootstrap()}.

Methods

static string
version_semver()

Retrieves the "semver-like" version of Moodle (e.g.: "4.4.0").

static int
branch()

Retrieves the numeric Moodle branch (e.g.: 404 for 4.4).

static int
build()

Retrieves the numeric Moodle build (e.g.: 2024042200).

static bool
compare(string $operator, string $constraint)

Compares the current Moodle version with a simple constraint.

static bool
at_least(string $min)

Checks if the current version is at least the specified one.

static bool
between(string $min, string $max)

Checks if the current version is between [min, max], inclusive.

static void
assert_min(string $min, string|null $msg = null)

Ensures the minimum version is met; otherwise throws an exception.

static bool
supports(string $feature, array $matrix)

Checks if a feature is supported according to a declarative matrix.

static array
major_minor()

Returns a [major, minor] pair, e.g.: [4, 4].

Details

at line 58
static string version_semver()

Retrieves the "semver-like" version of Moodle (e.g.: "4.4.0").

The representation is preferably derived from {$CFG->branch} (more predictable) and, as a fallback, from parsing {$CFG->release}.

Return Value

string

Normalized version in x.y.z format.

Examples

if (moodle_versions::version_semver() === '4.4.0') { /* ... *\/ }

at line 73
static int branch()

Retrieves the numeric Moodle branch (e.g.: 404 for 4.4).

Return Value

int

current branch (major*100 + minor)

Examples

[$major, $minor] = moodle_versions::major_minor(); // [4, 4]

at line 87
static int build()

Retrieves the numeric Moodle build (e.g.: 2024042200).

Useful for patch/build comparisons when necessary.

Return Value

int

current build number

at line 110
static bool compare(string $operator, string $constraint)

Compares the current Moodle version with a simple constraint.

The constraint accepts "x.y" or "x.y.z". The operator is passed to {\local_middag\framework\support\moodle\version_compare()}.

Parameters

string $operator

valid version_compare() operator: "<", "<=", ">", ">=", "==", "!="

string $constraint

Target version ("4.2" or "4.2.1").

Return Value

bool

true if the comparison is satisfied

Exceptions

InvalidArgumentException

Examples

moodle_versions::compare('>=', '4.2');   // true/false
moodle_versions::compare('<',  '4.5.0'); // true/false

at line 131
static bool at_least(string $min)

Checks if the current version is at least the specified one.

Syntactic sugar for {\local_middag\framework\support\moodle\compare()} with ">=" operator.

Parameters

string $min

Minimum version ("x.y" or "x.y.z").

Return Value

bool

True if at least the specified version

Examples

if (moodle_versions::at_least('4.1')) {
}

at line 148
static bool between(string $min, string $max)

Checks if the current version is between [min, max], inclusive.

Parameters

string $min

Minimum version ("x.y" or "x.y.z").

string $max

Maximum version ("x.y" or "x.y.z").

Return Value

bool

True if within range

Examples

if (moodle_versions::between('4.0', '4.2')) {
}

at line 167
static void assert_min(string $min, string|null $msg = null)

Ensures the minimum version is met; otherwise throws an exception.

If no message is provided, an internationalized message is used ({local_middag/lang/* requiresmoodlemin}).

Parameters

string $min

Required minimum version ("x.y" or "x.y.z").

string|null $msg

Custom message (already translated), optional

Return Value

void

Exceptions

RuntimeException

Examples

moodle_versions::assert_min('4.0'); // throws RuntimeException if < 4.0

at line 198
static bool supports(string $feature, array $matrix)

Checks if a feature is supported according to a declarative matrix.

Each map entry accepts the keys:

  • "since" => minimum version (inclusive)
  • "until" => maximum version (inclusive)

Parameters

string $feature

feature name (map key)

array $matrix

Return Value

bool

true if the current version satisfies the feature rules

Examples

$matrix = [
    'new_tasks_api'   => ['since' => '4.3'],
    'legacy_renderer' => ['until' => '4.2'],
    'cool_api'        => ['since' => '4.2', 'until' => '4.5'],
];
if (moodle_versions::supports('cool_api', $matrix)) {
}

at line 221
static array major_minor()

Returns a [major, minor] pair, e.g.: [4, 4].

Useful for quick switches by major/minor version.

Return Value

array