annotate core/lib/Drupal/Core/Extension/Dependency.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\Core\Extension;
Chris@5 4
Chris@5 5 use Drupal\Component\Version\Constraint;
Chris@5 6
Chris@5 7 /**
Chris@5 8 * A value object representing dependency information.
Chris@5 9 *
Chris@5 10 * This class implements \ArrayAccess to provide a backwards compatibility layer
Chris@5 11 * for Drupal 8.x. This will be removed before Drupal 9.x.
Chris@5 12 *
Chris@5 13 * @see https://www.drupal.org/node/2756875
Chris@5 14 */
Chris@5 15 class Dependency implements \ArrayAccess {
Chris@5 16
Chris@5 17 /**
Chris@5 18 * The name of the dependency.
Chris@5 19 *
Chris@5 20 * @var string
Chris@5 21 */
Chris@5 22 protected $name;
Chris@5 23
Chris@5 24 /**
Chris@5 25 * The project namespace for the dependency.
Chris@5 26 *
Chris@5 27 * @var string
Chris@5 28 */
Chris@5 29 protected $project;
Chris@5 30
Chris@5 31 /**
Chris@5 32 * The constraint string.
Chris@5 33 *
Chris@5 34 * @var \Drupal\Component\Version\Constraint
Chris@5 35 */
Chris@5 36 protected $constraintString;
Chris@5 37
Chris@5 38 /**
Chris@5 39 * The Constraint object from the constraint string.
Chris@5 40 *
Chris@5 41 * @var \Drupal\Component\Version\Constraint
Chris@5 42 */
Chris@5 43 protected $constraint;
Chris@5 44
Chris@5 45 /**
Chris@5 46 * Dependency constructor.
Chris@5 47 *
Chris@5 48 * @param string $name
Chris@5 49 * The name of the dependency.
Chris@5 50 * @param string $project
Chris@5 51 * The project namespace for the dependency.
Chris@5 52 * @param string $constraint
Chris@5 53 * The constraint string. For example, '>8.x-1.1'.
Chris@5 54 */
Chris@5 55 public function __construct($name, $project, $constraint) {
Chris@5 56 $this->name = $name;
Chris@5 57 $this->project = $project;
Chris@5 58 $this->constraintString = $constraint;
Chris@5 59 }
Chris@5 60
Chris@5 61 /**
Chris@5 62 * Gets the dependency's name.
Chris@5 63 *
Chris@5 64 * @return string
Chris@5 65 * The dependency's name.
Chris@5 66 */
Chris@5 67 public function getName() {
Chris@5 68 return $this->name;
Chris@5 69 }
Chris@5 70
Chris@5 71 /**
Chris@5 72 * Gets the dependency's project namespace.
Chris@5 73 *
Chris@5 74 * @return string
Chris@5 75 * The dependency's project namespace.
Chris@5 76 */
Chris@5 77 public function getProject() {
Chris@5 78 return $this->project;
Chris@5 79 }
Chris@5 80
Chris@5 81 /**
Chris@5 82 * Gets constraint string from the dependency.
Chris@5 83 *
Chris@5 84 * @return string
Chris@5 85 * The constraint string.
Chris@5 86 */
Chris@5 87 public function getConstraintString() {
Chris@5 88 return $this->constraintString;
Chris@5 89 }
Chris@5 90
Chris@5 91 /**
Chris@5 92 * Gets the Constraint object.
Chris@5 93 *
Chris@5 94 * @return \Drupal\Component\Version\Constraint
Chris@5 95 * The Constraint object.
Chris@5 96 */
Chris@5 97 protected function getConstraint() {
Chris@5 98 if (!$this->constraint) {
Chris@5 99 $this->constraint = new Constraint($this->constraintString, \Drupal::CORE_COMPATIBILITY);
Chris@5 100 }
Chris@5 101 return $this->constraint;
Chris@5 102 }
Chris@5 103
Chris@5 104 /**
Chris@5 105 * Determines if the provided version is compatible with this dependency.
Chris@5 106 *
Chris@5 107 * @param string $version
Chris@5 108 * The version to check, for example '4.2'.
Chris@5 109 *
Chris@5 110 * @return bool
Chris@5 111 * TRUE if compatible with the provided version, FALSE if not.
Chris@5 112 */
Chris@5 113 public function isCompatible($version) {
Chris@5 114 return $this->getConstraint()->isCompatible($version);
Chris@5 115 }
Chris@5 116
Chris@5 117 /**
Chris@5 118 * {@inheritdoc}
Chris@5 119 */
Chris@5 120 public function offsetExists($offset) {
Chris@5 121 @trigger_error(sprintf('Array access to %s properties is deprecated. Use accessor methods instead. See https://www.drupal.org/node/2756875', __CLASS__), E_USER_DEPRECATED);
Chris@5 122 return in_array($offset, ['name', 'project', 'original_version', 'versions'], TRUE);
Chris@5 123 }
Chris@5 124
Chris@5 125 /**
Chris@5 126 * {@inheritdoc}
Chris@5 127 */
Chris@5 128 public function offsetGet($offset) {
Chris@5 129 switch ($offset) {
Chris@5 130 case 'name':
Chris@5 131 @trigger_error(sprintf('Array access to the %s name property is deprecated. Use %s::getName() instead. See https://www.drupal.org/node/2756875', __CLASS__, __CLASS__), E_USER_DEPRECATED);
Chris@5 132 return $this->getName();
Chris@5 133
Chris@5 134 case 'project':
Chris@5 135 @trigger_error(sprintf('Array access to the %s project property is deprecated. Use %s::getProject() instead. See https://www.drupal.org/node/2756875', __CLASS__, __CLASS__), E_USER_DEPRECATED);
Chris@5 136 return $this->getProject();
Chris@5 137
Chris@5 138 case 'original_version':
Chris@5 139 @trigger_error(sprintf('Array access to the %s original_version property is deprecated. Use %s::getConstraintString() instead. See https://www.drupal.org/node/2756875', __CLASS__, __CLASS__), E_USER_DEPRECATED);
Chris@5 140 $constraint = $this->getConstraintString();
Chris@5 141 if ($constraint) {
Chris@5 142 $constraint = ' (' . $constraint . ')';
Chris@5 143 }
Chris@5 144 return $constraint;
Chris@5 145
Chris@5 146 case 'versions':
Chris@5 147 @trigger_error(sprintf('Array access to the %s versions property is deprecated. See https://www.drupal.org/node/2756875', __CLASS__), E_USER_DEPRECATED);
Chris@5 148 return $this->getConstraint()->toArray();
Chris@5 149 }
Chris@5 150 throw new \InvalidArgumentException("The $offset key is not supported");
Chris@5 151 }
Chris@5 152
Chris@5 153 /**
Chris@5 154 * {@inheritdoc}
Chris@5 155 */
Chris@5 156 public function offsetSet($offset, $value) {
Chris@5 157 throw new \BadMethodCallException(sprintf('%s() is not supported', __METHOD__));
Chris@5 158 }
Chris@5 159
Chris@5 160 /**
Chris@5 161 * {@inheritdoc}
Chris@5 162 */
Chris@5 163 public function offsetUnset($offset) {
Chris@5 164 throw new \BadMethodCallException(sprintf('%s() is not supported', __METHOD__));
Chris@5 165 }
Chris@5 166
Chris@5 167 /**
Chris@5 168 * Creates a new instance of this class from a dependency string.
Chris@5 169 *
Chris@5 170 * @param string $dependency
Chris@5 171 * A dependency string, which specifies a module or theme dependency, and
Chris@5 172 * optionally the project it comes from and a constraint string that
Chris@5 173 * determines the versions that are supported. Supported formats include:
Chris@5 174 * - 'module'
Chris@5 175 * - 'project:module'
Chris@5 176 * - 'project:module (>=version, <=version)'.
Chris@5 177 *
Chris@5 178 * @return static
Chris@5 179 */
Chris@5 180 public static function createFromString($dependency) {
Chris@5 181 if (strpos($dependency, ':') !== FALSE) {
Chris@5 182 list($project, $dependency) = explode(':', $dependency);
Chris@5 183 }
Chris@5 184 else {
Chris@5 185 $project = '';
Chris@5 186 }
Chris@5 187 $parts = explode('(', $dependency, 2);
Chris@5 188 $name = trim($parts[0]);
Chris@5 189 $version_string = isset($parts[1]) ? rtrim($parts[1], ") ") : '';
Chris@5 190 return new static($name, $project, $version_string);
Chris@5 191 }
Chris@5 192
Chris@5 193 /**
Chris@5 194 * Prevents unnecessary serialization of constraint objects.
Chris@5 195 *
Chris@5 196 * @return array
Chris@5 197 * The properties to seriailize.
Chris@5 198 */
Chris@5 199 public function __sleep() {
Chris@5 200 return ['name', 'project', 'constraintString'];
Chris@5 201 }
Chris@5 202
Chris@5 203 }