function ComposerInspector::getInstalledPackagesList

Returns the installed packages list.

Parameters

string $working_dir: The working directory in which to run Composer. Should contain a `composer.lock` file.

Return value

\Drupal\package_manager\InstalledPackagesList The installed packages list for the directory.

Throws

\UnexpectedValueException Thrown if a package reports that its install path is the same as the working directory, and it is not of the `metapackage` type.

File

core/modules/package_manager/src/ComposerInspector.php, line 290

Class

ComposerInspector
Defines a class to get information from Composer.

Namespace

Drupal\package_manager

Code

public function getInstalledPackagesList(string $working_dir) : InstalledPackagesList {
    $working_dir = realpath($working_dir);
    $this->validate($working_dir);
    if (array_key_exists($working_dir, $this->packageLists)) {
        return $this->packageLists[$working_dir];
    }
    $packages_data = $this->show($working_dir);
    $packages_data = $this->getPackageTypes($packages_data, $working_dir);
    foreach ($packages_data as $name => $package) {
        $path = $package['path'];
        // For packages installed as dev snapshots from certain version control
        // systems, `composer show` displays the version like `1.0.x-dev 0a1b2c`,
        // which will cause an exception if we try to parse it as a legitimate
        // semantic version. Since we don't need the abbreviated commit hash, just
        // remove it.
        if (str_contains($package['version'], '-dev ')) {
            $packages_data[$name]['version'] = explode(' ', $package['version'], 2)[0];
        }
        // We expect Composer to report that metapackages' install paths are the
        // same as the working directory, in which case InstalledPackage::$path
        // should be NULL. For all other package types, we consider it invalid
        // if the install path is the same as the working directory.
        if (isset($package['type']) && $package['type'] === 'metapackage') {
            if ($path !== NULL) {
                throw new \UnexpectedValueException("Metapackage '{$name}' is installed at unexpected path: '{$path}', expected NULL");
            }
            $packages_data[$name]['path'] = $path;
        }
        elseif ($path === $working_dir) {
            throw new \UnexpectedValueException("Package '{$name}' cannot be installed at path: '{$path}'");
        }
        else {
            $packages_data[$name]['path'] = realpath($path);
        }
    }
    $packages_data = array_map(InstalledPackage::createFromArray(...), $packages_data);
    $list = new InstalledPackagesList($packages_data);
    $this->packageLists[$working_dir] = $list;
    return $list;
}

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