mirror of
https://github.com/pestphp/pest.git
synced 2026-12-03 22:45:06 +01:00
dc73ec009e
Co-authored-by: Punyapal Shah <mrpunyapal@gmail.com>
56 lines
1.7 KiB
PHP
56 lines
1.7 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace Pest\Support;
|
|
|
|
use Composer\InstalledVersions;
|
|
|
|
final readonly class InstalledPackages
|
|
{
|
|
/**
|
|
* @param array<int, array{root: array{install_path?: string}, versions: array<string, array{dev_requirement?: bool}>}> $installedSets
|
|
*/
|
|
public function __construct(private array $installedSets) {}
|
|
|
|
public static function fromComposer(): self
|
|
{
|
|
if (! class_exists(InstalledVersions::class)) {
|
|
return new self([]);
|
|
}
|
|
|
|
/** @var array<int, array{root: array{install_path?: string}, versions: array<string, array{dev_requirement?: bool}>}> $installedSets */
|
|
$installedSets = InstalledVersions::getAllRawData();
|
|
|
|
return new self($installedSets);
|
|
}
|
|
|
|
public static function has(string $package, bool $includeDevRequirements = true): bool
|
|
{
|
|
return self::fromComposer()->contains($package, $includeDevRequirements);
|
|
}
|
|
|
|
public function contains(string $package, bool $includeDevRequirements = true): bool
|
|
{
|
|
foreach ($this->installedSets as $installedSet) {
|
|
if ($this->isBundledInPhar($installedSet)) {
|
|
continue;
|
|
}
|
|
|
|
if (isset($installedSet['versions'][$package])) {
|
|
return $includeDevRequirements || ($installedSet['versions'][$package]['dev_requirement'] ?? false) === false;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* @param array{root: array{install_path?: string}} $installedSet
|
|
*/
|
|
private function isBundledInPhar(array $installedSet): bool
|
|
{
|
|
return str_starts_with($installedSet['root']['install_path'] ?? '', 'phar://');
|
|
}
|
|
}
|