function ExecutableFinder::getLocalComposerPath

Tries to find the Composer binary installed in the project.

Return value

string|null The path of the `composer` binary installed in the project's vendor dependencies, or NULL if it is not installed or cannot be found.

1 call to ExecutableFinder::getLocalComposerPath()
ExecutableFinder::find in core/modules/package_manager/src/ExecutableFinder.php

File

core/modules/package_manager/src/ExecutableFinder.php, line 100

Class

ExecutableFinder
An executable finder which looks for executable paths in configuration.

Namespace

Drupal\package_manager

Code

private function getLocalComposerPath() : ?string {
  // Composer is not installed in the project, so there's nothing to do.
  if ($this->composerPackagePath === FALSE) {
    return NULL;
  }
  // This is a bit expensive to compute, so statically cache it.
  if ($this->composerBinaryPath) {
    return $this->composerBinaryPath;
  }
  $composer_json = file_get_contents($this->composerPackagePath . '/composer.json');
  $composer_json = Json::decode($composer_json);
  foreach ($composer_json['bin'] ?? [] as $bin) {
    if (str_ends_with($bin, '/composer')) {
      $bin = $this->composerPackagePath . '/' . $bin;
      // For extra security, try to disable the binary's execute permission.
      // If that fails, it's worth warning about but is not an actual problem.
      if (is_executable($bin) && !$this->fileSystem
        ->chmod($bin, 0644)) {
        $this->logger?->warning('Composer was found at %path, but could not be made read-only.', [
          '%path' => $bin,
        ]);
      }
      return $this->composerBinaryPath = $bin;
    }
  }
  return NULL;
}

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