function Constraint::parseConstraint

Same name in other branches
  1. 9 core/lib/Drupal/Component/Version/Constraint.php \Drupal\Component\Version\Constraint::parseConstraint()
  2. 10 core/lib/Drupal/Component/Version/Constraint.php \Drupal\Component\Version\Constraint::parseConstraint()
  3. 11.x core/lib/Drupal/Component/Version/Constraint.php \Drupal\Component\Version\Constraint::parseConstraint()

Parses a constraint string.

Parameters

string $constraint_string: The constraint string to parse.

string $core_compatibility: Core compatibility declared for the current version of Drupal core. Normally this is set to \Drupal::CORE_COMPATIBILITY by the caller.

1 call to Constraint::parseConstraint()
Constraint::__construct in core/lib/Drupal/Component/Version/Constraint.php
Constraint constructor.

File

core/lib/Drupal/Component/Version/Constraint.php, line 102

Class

Constraint
A value object representing a Drupal version constraint.

Namespace

Drupal\Component\Version

Code

private function parseConstraint($constraint_string, $core_compatibility) {
    // We use named subpatterns and support every op that version_compare
    // supports. Also, op is optional and defaults to equals.
    $p_op = '(?<operation>!=|==|=|<|<=|>|>=|<>)?';
    // Core version is always optional: 8.x-2.x and 2.x is treated the same.
    $p_core = '(?:' . preg_quote($core_compatibility) . '-)?';
    $p_major = '(?<major>\\d+)';
    // By setting the minor version to x, branches can be matched.
    $p_minor = '(?<minor>(?:\\d+|x)(?:-[A-Za-z]+\\d+)?)';
    foreach (explode(',', $constraint_string) as $constraint) {
        if (preg_match("/^\\s*{$p_op}\\s*{$p_core}{$p_major}\\.{$p_minor}/", $constraint, $matches)) {
            $op = !empty($matches['operation']) ? $matches['operation'] : '=';
            if ($matches['minor'] == 'x') {
                // Drupal considers "2.x" to mean any version that begins with
                // "2" (e.g. 2.0, 2.9 are all "2.x"). PHP's version_compare(),
                // on the other hand, treats "x" as a string; so to
                // version_compare(), "2.x" is considered less than 2.0. This
                // means that >=2.x and <2.x are handled by version_compare()
                // as we need, but > and <= are not.
                if ($op == '>' || $op == '<=') {
                    $matches['major']++;
                }
                // Equivalence can be checked by adding two restrictions.
                if ($op == '=' || $op == '==') {
                    $this->constraintArray[] = [
                        'op' => '<',
                        'version' => $matches['major'] + 1 . '.x',
                    ];
                    $op = '>=';
                }
            }
            $this->constraintArray[] = [
                'op' => $op,
                'version' => $matches['major'] . '.' . $matches['minor'],
            ];
        }
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.