class Dependency

Same name in other branches
  1. 9 core/lib/Drupal/Core/Extension/Dependency.php \Drupal\Core\Extension\Dependency
  2. 10 core/lib/Drupal/Core/Extension/Dependency.php \Drupal\Core\Extension\Dependency
  3. 11.x core/lib/Drupal/Core/Extension/Dependency.php \Drupal\Core\Extension\Dependency

A value object representing dependency information.

This class implements \ArrayAccess to provide a backwards compatibility layer for Drupal 8.x. This will be removed before Drupal 9.x.

Hierarchy

  • class \Drupal\Core\Extension\Dependency implements \Drupal\Core\Extension\ArrayAccess

Expanded class hierarchy of Dependency

See also

https://www.drupal.org/node/2756875

5 files declare their use of Dependency
DependencyTest.php in core/tests/Drupal/Tests/Core/Extension/DependencyTest.php
DrupalCheckIncompatibilityTest.php in core/tests/Drupal/KernelTests/Core/Common/DrupalCheckIncompatibilityTest.php
install.inc in core/includes/install.inc
API functions for installing modules and themes.
ModuleDependencyMessageTrait.php in core/modules/system/src/ModuleDependencyMessageTrait.php
system.module in core/modules/system/system.module
Configuration system that lets administrators modify the workings of the site.
5 string references to 'Dependency'
DependencyTest::testModuleEnableOrder in core/modules/system/tests/src/Functional/Module/DependencyTest.php
Tests that module dependencies are enabled in the correct order in the UI.
ModuleHandlerTest::testDependencyResolution in core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php
Tests dependency resolution.
ModuleHandlerTest::testUninstallContentDependency in core/modules/system/tests/src/Kernel/Extension/ModuleHandlerTest.php
Tests uninstalling a module that has content.
module_test_system_info_alter in core/modules/system/tests/modules/module_test/module_test.module
Implements hook_system_info_alter().
SearchExcerptTest::testSearchExcerptSimplified in core/modules/search/tests/src/Kernel/SearchExcerptTest.php
Tests search_excerpt() with search keywords matching simplified words.

File

core/lib/Drupal/Core/Extension/Dependency.php, line 15

Namespace

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

}

Members

Title Sort descending Modifiers Object type Summary
Dependency::$constraint protected property The Constraint object from the constraint string.
Dependency::$constraintString protected property The constraint string.
Dependency::$name protected property The name of the dependency.
Dependency::$project protected property The project namespace for the dependency.
Dependency::createFromString public static function Creates a new instance of this class from a dependency string.
Dependency::getConstraint protected function Gets the Constraint object.
Dependency::getConstraintString public function Gets constraint string from the dependency.
Dependency::getName public function Gets the dependency&#039;s name.
Dependency::getProject public function Gets the dependency&#039;s project namespace.
Dependency::isCompatible public function Determines if the provided version is compatible with this dependency.
Dependency::offsetExists public function
Dependency::offsetGet public function
Dependency::offsetSet public function
Dependency::offsetUnset public function
Dependency::__construct public function Dependency constructor.
Dependency::__sleep public function Prevents unnecessary serialization of constraint objects.

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