Chris@18: 8.x-1.1'. Chris@18: */ Chris@18: public function __construct($name, $project, $constraint) { Chris@18: $this->name = $name; Chris@18: $this->project = $project; Chris@18: $this->constraintString = $constraint; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the dependency's name. Chris@18: * Chris@18: * @return string Chris@18: * The dependency's name. Chris@18: */ Chris@18: public function getName() { Chris@18: return $this->name; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the dependency's project namespace. Chris@18: * Chris@18: * @return string Chris@18: * The dependency's project namespace. Chris@18: */ Chris@18: public function getProject() { Chris@18: return $this->project; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets constraint string from the dependency. Chris@18: * Chris@18: * @return string Chris@18: * The constraint string. Chris@18: */ Chris@18: public function getConstraintString() { Chris@18: return $this->constraintString; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Gets the Constraint object. Chris@18: * Chris@18: * @return \Drupal\Component\Version\Constraint Chris@18: * The Constraint object. Chris@18: */ Chris@18: protected function getConstraint() { Chris@18: if (!$this->constraint) { Chris@18: $this->constraint = new Constraint($this->constraintString, \Drupal::CORE_COMPATIBILITY); Chris@18: } Chris@18: return $this->constraint; Chris@18: } Chris@18: Chris@18: /** Chris@18: * Determines if the provided version is compatible with this dependency. Chris@18: * Chris@18: * @param string $version Chris@18: * The version to check, for example '4.2'. Chris@18: * Chris@18: * @return bool Chris@18: * TRUE if compatible with the provided version, FALSE if not. Chris@18: */ Chris@18: public function isCompatible($version) { Chris@18: return $this->getConstraint()->isCompatible($version); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function offsetExists($offset) { Chris@18: @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@18: return in_array($offset, ['name', 'project', 'original_version', 'versions'], TRUE); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function offsetGet($offset) { Chris@18: switch ($offset) { Chris@18: case 'name': Chris@18: @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@18: return $this->getName(); Chris@18: Chris@18: case 'project': Chris@18: @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@18: return $this->getProject(); Chris@18: Chris@18: case 'original_version': Chris@18: @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@18: $constraint = $this->getConstraintString(); Chris@18: if ($constraint) { Chris@18: $constraint = ' (' . $constraint . ')'; Chris@18: } Chris@18: return $constraint; Chris@18: Chris@18: case 'versions': Chris@18: @trigger_error(sprintf('Array access to the %s versions property is deprecated. See https://www.drupal.org/node/2756875', __CLASS__), E_USER_DEPRECATED); Chris@18: return $this->getConstraint()->toArray(); Chris@18: } Chris@18: throw new \InvalidArgumentException("The $offset key is not supported"); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function offsetSet($offset, $value) { Chris@18: throw new \BadMethodCallException(sprintf('%s() is not supported', __METHOD__)); Chris@18: } Chris@18: Chris@18: /** Chris@18: * {@inheritdoc} Chris@18: */ Chris@18: public function offsetUnset($offset) { Chris@18: throw new \BadMethodCallException(sprintf('%s() is not supported', __METHOD__)); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Creates a new instance of this class from a dependency string. Chris@18: * Chris@18: * @param string $dependency Chris@18: * A dependency string, which specifies a module or theme dependency, and Chris@18: * optionally the project it comes from and a constraint string that Chris@18: * determines the versions that are supported. Supported formats include: Chris@18: * - 'module' Chris@18: * - 'project:module' Chris@18: * - 'project:module (>=version, <=version)'. Chris@18: * Chris@18: * @return static Chris@18: */ Chris@18: public static function createFromString($dependency) { Chris@18: if (strpos($dependency, ':') !== FALSE) { Chris@18: list($project, $dependency) = explode(':', $dependency); Chris@18: } Chris@18: else { Chris@18: $project = ''; Chris@18: } Chris@18: $parts = explode('(', $dependency, 2); Chris@18: $name = trim($parts[0]); Chris@18: $version_string = isset($parts[1]) ? rtrim($parts[1], ") ") : ''; Chris@18: return new static($name, $project, $version_string); Chris@18: } Chris@18: Chris@18: /** Chris@18: * Prevents unnecessary serialization of constraint objects. Chris@18: * Chris@18: * @return array Chris@18: * The properties to seriailize. Chris@18: */ Chris@18: public function __sleep() { Chris@18: return ['name', 'project', 'constraintString']; Chris@18: } Chris@18: Chris@18: }