Skip to content

Commit 66b8ac1

Browse files
[10.x] Add hasPackage method to Composer class (#48124)
* Update composer.json * extract logic for locating the composer.json file * formatting --------- Co-authored-by: Taylor Otwell <[email protected]>
1 parent 00b3cae commit 66b8ac1

File tree

1 file changed

+36
-6
lines changed

1 file changed

+36
-6
lines changed

src/Illuminate/Support/Composer.php

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,22 @@ public function __construct(Filesystem $files, $workingPath = null)
3838
$this->workingPath = $workingPath;
3939
}
4040

41+
/**
42+
* Determine if the given Composer package is installed.
43+
*
44+
* @param string $package
45+
* @return bool
46+
*
47+
* @throw \RuntimeException
48+
*/
49+
protected function hasPackage($package)
50+
{
51+
$composer = json_decode(file_get_contents($this->findComposerFile()), true);
52+
53+
return array_key_exists($package, $composer['require'] ?? [])
54+
|| array_key_exists($package, $composer['require-dev'] ?? []);
55+
}
56+
4157
/**
4258
* Install the given Composer packages into the application.
4359
*
@@ -104,11 +120,7 @@ public function removePackages(array $packages, bool $dev = false, Closure|Outpu
104120
*/
105121
public function modify(callable $callback)
106122
{
107-
$composerFile = "{$this->workingPath}/composer.json";
108-
109-
if (! file_exists($composerFile)) {
110-
throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}].");
111-
}
123+
$composerFile = $this->findComposerFile();
112124

113125
$composer = json_decode(file_get_contents($composerFile), true, 512, JSON_THROW_ON_ERROR);
114126

@@ -147,7 +159,7 @@ public function dumpOptimized()
147159
}
148160

149161
/**
150-
* Get the composer command for the environment.
162+
* Get the Composer binary / command for the environment.
151163
*
152164
* @return array
153165
*/
@@ -160,6 +172,24 @@ public function findComposer()
160172
return ['composer'];
161173
}
162174

175+
/**
176+
* Get the path to the "composer.json" file.
177+
*
178+
* @return string
179+
*
180+
* @throw \RuntimeException
181+
*/
182+
protected function findComposerFile()
183+
{
184+
$composerFile = "{$this->workingPath}/composer.json";
185+
186+
if (! file_exists($composerFile)) {
187+
throw new RuntimeException("Unable to locate `composer.json` file at [{$this->workingPath}].");
188+
}
189+
190+
return $composerFile;
191+
}
192+
163193
/**
164194
* Get the PHP binary.
165195
*

0 commit comments

Comments
 (0)