diff --git a/src/Illuminate/Support/Composer.php b/src/Illuminate/Support/Composer.php index 7b9b29196dbd..2f229256c03c 100644 --- a/src/Illuminate/Support/Composer.php +++ b/src/Illuminate/Support/Composer.php @@ -38,6 +38,22 @@ public function __construct(Filesystem $files, $workingPath = null) $this->workingPath = $workingPath; } + /** + * Determine if the given Composer package is installed. + * + * @param string $package + * @return bool + * + * @throw \RuntimeException + */ + protected function hasPackage($package) + { + $composer = json_decode(file_get_contents($this->findComposerFile()), true); + + return array_key_exists($package, $composer['require'] ?? []) + || array_key_exists($package, $composer['require-dev'] ?? []); + } + /** * Install the given Composer packages into the application. * @@ -104,11 +120,7 @@ public function removePackages(array $packages, bool $dev = false, Closure|Outpu */ public function modify(callable $callback) { - $composerFile = "{$this->workingPath}/composer.json"; - - if (! file_exists($composerFile)) { - throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}]."); - } + $composerFile = $this->findComposerFile(); $composer = json_decode(file_get_contents($composerFile), true, 512, JSON_THROW_ON_ERROR); @@ -147,7 +159,7 @@ public function dumpOptimized() } /** - * Get the composer command for the environment. + * Get the Composer binary / command for the environment. * * @return array */ @@ -160,6 +172,24 @@ public function findComposer() return ['composer']; } + /** + * Get the path to the "composer.json" file. + * + * @return string + * + * @throw \RuntimeException + */ + protected function findComposerFile() + { + $composerFile = "{$this->workingPath}/composer.json"; + + if (! file_exists($composerFile)) { + throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}]."); + } + + return $composerFile; + } + /** * Get the PHP binary. *