annotate core/lib/Drupal/Component/Version/Constraint.php @ 5:12f9dff5fda9 tip

Update to Drupal core 8.7.1
author Chris Cannam
date Thu, 09 May 2019 15:34:47 +0100
parents
children
rev   line source
Chris@5 1 <?php
Chris@5 2
Chris@5 3 namespace Drupal\Component\Version;
Chris@5 4
Chris@5 5 /**
Chris@5 6 * A value object representing a Drupal version constraint.
Chris@5 7 */
Chris@5 8 class Constraint {
Chris@5 9
Chris@5 10 /**
Chris@5 11 * The constraint represented as a string. For example '>=8.x-5.x'.
Chris@5 12 *
Chris@5 13 * @var string
Chris@5 14 */
Chris@5 15 protected $constraint;
Chris@5 16
Chris@5 17 /**
Chris@5 18 * A list of associative arrays representing the constraint.
Chris@5 19 *
Chris@5 20 * Each containing the keys:
Chris@5 21 * - 'op': can be one of: '=', '==', '!=', '<>', '<', '<=', '>', or '>='.
Chris@5 22 * - 'version': A complete version, e.g. '4.5-beta3'.
Chris@5 23 *
Chris@5 24 * @var array[]
Chris@5 25 */
Chris@5 26 protected $constraintArray = [];
Chris@5 27
Chris@5 28 /**
Chris@5 29 * Constraint constructor.
Chris@5 30 *
Chris@5 31 * @param string $constraint
Chris@5 32 * The constraint string to create the object from. For example, '>8.x-1.1'.
Chris@5 33 * @param string $core_compatibility
Chris@5 34 * Core compatibility declared for the current version of Drupal core.
Chris@5 35 * Normally this is set to \Drupal::CORE_COMPATIBILITY by the caller.
Chris@5 36 */
Chris@5 37 public function __construct($constraint, $core_compatibility) {
Chris@5 38 $this->constraint = $constraint;
Chris@5 39 $this->parseConstraint($constraint, $core_compatibility);
Chris@5 40 }
Chris@5 41
Chris@5 42 /**
Chris@5 43 * Gets the constraint as a string.
Chris@5 44 *
Chris@5 45 * Can be used in the UI for reporting incompatibilities.
Chris@5 46 *
Chris@5 47 * @return string
Chris@5 48 * The constraint as a string.
Chris@5 49 */
Chris@5 50 public function __toString() {
Chris@5 51 return $this->constraint;
Chris@5 52 }
Chris@5 53
Chris@5 54 /**
Chris@5 55 * A list of associative arrays representing the constraint.
Chris@5 56 *
Chris@5 57 * Each containing the keys:
Chris@5 58 * - 'op': can be one of: '=', '==', '!=', '<>', '<', '<=', '>', or '>='.
Chris@5 59 * - 'version': A complete version, e.g. '4.5-beta3'.
Chris@5 60 *
Chris@5 61 * @return array[]
Chris@5 62 * The constraint represented as an array.
Chris@5 63 *
Chris@5 64 * @deprecated in Drupal 8.7.0, will be removed before Drupal 9.0.0.
Chris@5 65 * Only exists to provide a backwards compatibility layer.
Chris@5 66 *
Chris@5 67 * @see https://www.drupal.org/node/2756875
Chris@5 68 */
Chris@5 69 public function toArray() {
Chris@5 70 @trigger_error(sprintf('%s() only exists to provide a backwards compatibility layer. See https://www.drupal.org/node/2756875', __METHOD__), E_USER_DEPRECATED);
Chris@5 71 return $this->constraintArray;
Chris@5 72 }
Chris@5 73
Chris@5 74 /**
Chris@5 75 * Determines if the provided version is satisfied by this constraint.
Chris@5 76 *
Chris@5 77 * @param string $version
Chris@5 78 * The version to check, for example '4.2'.
Chris@5 79 *
Chris@5 80 * @return bool
Chris@5 81 * TRUE if the provided version is satisfied by this constraint, FALSE if
Chris@5 82 * not.
Chris@5 83 */
Chris@5 84 public function isCompatible($version) {
Chris@5 85 foreach ($this->constraintArray as $constraint) {
Chris@5 86 if (!version_compare($version, $constraint['version'], $constraint['op'])) {
Chris@5 87 return FALSE;
Chris@5 88 }
Chris@5 89 }
Chris@5 90 return TRUE;
Chris@5 91 }
Chris@5 92
Chris@5 93 /**
Chris@5 94 * Parses a constraint string.
Chris@5 95 *
Chris@5 96 * @param string $constraint_string
Chris@5 97 * The constraint string to parse.
Chris@5 98 * @param string $core_compatibility
Chris@5 99 * Core compatibility declared for the current version of Drupal core.
Chris@5 100 * Normally this is set to \Drupal::CORE_COMPATIBILITY by the caller.
Chris@5 101 */
Chris@5 102 private function parseConstraint($constraint_string, $core_compatibility) {
Chris@5 103 // We use named subpatterns and support every op that version_compare
Chris@5 104 // supports. Also, op is optional and defaults to equals.
Chris@5 105 $p_op = '(?<operation>!=|==|=|<|<=|>|>=|<>)?';
Chris@5 106 // Core version is always optional: 8.x-2.x and 2.x is treated the same.
Chris@5 107 $p_core = '(?:' . preg_quote($core_compatibility) . '-)?';
Chris@5 108 $p_major = '(?<major>\d+)';
Chris@5 109 // By setting the minor version to x, branches can be matched.
Chris@5 110 $p_minor = '(?<minor>(?:\d+|x)(?:-[A-Za-z]+\d+)?)';
Chris@5 111 foreach (explode(',', $constraint_string) as $constraint) {
Chris@5 112 if (preg_match("/^\s*$p_op\s*$p_core$p_major\.$p_minor/", $constraint, $matches)) {
Chris@5 113 $op = !empty($matches['operation']) ? $matches['operation'] : '=';
Chris@5 114 if ($matches['minor'] == 'x') {
Chris@5 115 // Drupal considers "2.x" to mean any version that begins with
Chris@5 116 // "2" (e.g. 2.0, 2.9 are all "2.x"). PHP's version_compare(),
Chris@5 117 // on the other hand, treats "x" as a string; so to
Chris@5 118 // version_compare(), "2.x" is considered less than 2.0. This
Chris@5 119 // means that >=2.x and <2.x are handled by version_compare()
Chris@5 120 // as we need, but > and <= are not.
Chris@5 121 if ($op == '>' || $op == '<=') {
Chris@5 122 $matches['major']++;
Chris@5 123 }
Chris@5 124 // Equivalence can be checked by adding two restrictions.
Chris@5 125 if ($op == '=' || $op == '==') {
Chris@5 126 $this->constraintArray[] = ['op' => '<', 'version' => ($matches['major'] + 1) . '.x'];
Chris@5 127 $op = '>=';
Chris@5 128 }
Chris@5 129 }
Chris@5 130 $this->constraintArray[] = ['op' => $op, 'version' => $matches['major'] . '.' . $matches['minor']];
Chris@5 131 }
Chris@5 132 }
Chris@5 133 }
Chris@5 134
Chris@5 135 }