class DrupalCoreComposer

Same name in other branches
  1. 8.9.x composer/Generator/Util/DrupalCoreComposer.php \Drupal\Composer\Generator\Util\DrupalCoreComposer
  2. 10 composer/Generator/Util/DrupalCoreComposer.php \Drupal\Composer\Generator\Util\DrupalCoreComposer
  3. 11.x composer/Generator/Util/DrupalCoreComposer.php \Drupal\Composer\Generator\Util\DrupalCoreComposer

Utilities for accessing composer.json data from drupal/drupal and drupal/core.

Some data is stored in the root composer.json file, while others is found in the core/composer.json file.

Hierarchy

Expanded class hierarchy of DrupalCoreComposer

6 files declare their use of DrupalCoreComposer
BuilderInterface.php in composer/Generator/BuilderInterface.php
ComponentGenerator.php in composer/Generator/ComponentGenerator.php
DrupalPackageBuilder.php in composer/Generator/Builder/DrupalPackageBuilder.php
Fixtures.php in core/tests/Drupal/Tests/Composer/Generator/Fixtures.php
MetapackageUpdateTest.php in core/tests/Drupal/Tests/Composer/Generator/MetapackageUpdateTest.php

... See full list

File

composer/Generator/Util/DrupalCoreComposer.php, line 11

Namespace

Drupal\Composer\Generator\Util
View source
class DrupalCoreComposer {
    
    /**
     * Cached composer.json data.
     *
     * @var array
     */
    protected $composerJson = [];
    
    /**
     * Cached composer.lock data.
     *
     * @var array
     */
    protected $composerLock = [];
    
    /**
     * DrupalCoreComposer constructor.
     *
     * @param array $composerJson
     *   The composer.json data.
     * @param array $composerLock
     *   The composer.lock data.
     */
    public function __construct(array $composerJson, array $composerLock) {
        $this->composerJson = $composerJson;
        $this->composerLock = $composerLock;
    }
    
    /**
     * DrupalCoreComposer factory.
     *
     * @param string $repositoryPath
     *   Path to a directory containing a composer.json and composer.lock files.
     *
     * @return static
     *   New DrupalCoreComposer object containing composer.json and lock data.
     */
    public static function createFromPath(string $repositoryPath) {
        $composerJson = static::loadJsonFromPath("{$repositoryPath}/composer.json");
        $composerLock = static::loadJsonFromPath("{$repositoryPath}/composer.lock");
        return new self($composerJson, $composerLock);
    }
    
    /**
     * Fetch the composer data from the root drupal/drupal project.
     *
     * @return array
     *   Composer json data
     */
    public function rootComposerJson() {
        return $this->composerJson;
    }
    
    /**
     * Fetch the composer lock data.
     *
     * @return array
     *   Composer lock data
     */
    public function composerLock() {
        return $this->composerLock;
    }
    
    /**
     * Return the "require-dev" section from root or core composer.json file.
     *
     * The require-dev constraints moved from core/composer.json (8.7.x and
     * earlier) to the root composer.json file (8.8.x and later).
     *
     * @return array
     *   The contents of the "require-dev" section.
     */
    public function getRequireDev() {
        $composerJsonData = $this->rootComposerJson();
        return $composerJsonData['require-dev'] ?? [];
    }
    
    /**
     * Look up the info for one package in the composer.lock file.
     *
     * @param string $packageName
     *   Name of package to find, e.g. 'behat/mink-selenium2-driver'.
     * @param bool $dev
     *   TRUE: consider only 'packages-dev'. Default: consider only 'packages'
     *
     * @return array
     *   Package info from composer.lock.
     */
    public function packageLockInfo($packageName, $dev = FALSE) {
        $packagesSection = $dev ? 'packages-dev' : 'packages';
        foreach ($this->composerLock[$packagesSection] as $info) {
            if ($info['name'] == $packageName) {
                return $info;
            }
        }
        return [];
    }
    
    /**
     * Load json data from the specified path.
     *
     * @param string $path
     *   Relative path to the json file to load.
     *
     * @return array
     *   The contents of the json data for the specified file.
     */
    protected static function loadJsonFromPath($path) {
        return file_exists($path) ? json_decode(file_get_contents($path), TRUE) : [];
    }

}

Members

Title Sort descending Modifiers Object type Summary
DrupalCoreComposer::$composerJson protected property Cached composer.json data.
DrupalCoreComposer::$composerLock protected property Cached composer.lock data.
DrupalCoreComposer::composerLock public function Fetch the composer lock data.
DrupalCoreComposer::createFromPath public static function DrupalCoreComposer factory.
DrupalCoreComposer::getRequireDev public function Return the "require-dev" section from root or core composer.json file.
DrupalCoreComposer::loadJsonFromPath protected static function Load json data from the specified path.
DrupalCoreComposer::packageLockInfo public function Look up the info for one package in the composer.lock file.
DrupalCoreComposer::rootComposerJson public function Fetch the composer data from the root drupal/drupal project.
DrupalCoreComposer::__construct public function DrupalCoreComposer constructor.

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