Skip to content

Commit

Permalink
[10.x] Add hasPackage method to Composer class (#48124)
Browse files Browse the repository at this point in the history
* Update composer.json

* extract logic for locating the composer.json file

* formatting

---------

Co-authored-by: Taylor Otwell <[email protected]>
  • Loading branch information
emargareten and taylorotwell authored Aug 21, 2023
1 parent 00b3cae commit 66b8ac1
Showing 1 changed file with 36 additions and 6 deletions.
42 changes: 36 additions & 6 deletions src/Illuminate/Support/Composer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -147,7 +159,7 @@ public function dumpOptimized()
}

/**
* Get the composer command for the environment.
* Get the Composer binary / command for the environment.
*
* @return array
*/
Expand All @@ -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.
*
Expand Down

0 comments on commit 66b8ac1

Please sign in to comment.