# InstalledPackages

> Represents the packages Composer installed in a project, read from its vendor/composer/installed.php.

[API reference](/docs/api/index.md) › [Scaffold](/docs/extensions/scaffold/reference/index.md) › [Config](/docs/extensions/scaffold/reference/config/index.md) › InstalledPackages

| final readonly class | `PHPForge\Scaffold\Config\InstalledPackages` |
| --- | --- |
| Source | [`src/Config/InstalledPackages.php`](https://github.com/php-forge/scaffold/blob/main/src/Config/InstalledPackages.php#L18) |

Represents the packages Composer installed in a project, read from its `vendor/composer/installed.php`.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates an installed-package view from normalized Composer metadata, or an unknown view from `null`. | `InstalledPackages` |
| [`detect()`](#method-detect) static | Reads the installed packages of the project from `<vendorDir>/composer/installed.php`, then from the `Composer\InstalledVersions` runtime data when its root package is `$projectRoot`. | `InstalledPackages` |
| [`fromRawData()`](#method-fromRawData) static | Builds the list from the array returned by a `vendor/composer/installed.php` file. | `InstalledPackages` |
| [`has()`](#method-has) | Returns whether `$name` is installed; `false` when the installed packages are unknown. | `InstalledPackages` |
| [`installPath()`](#method-installPath) | Returns the absolute install path of `$name`, or `null` when it is not installed or unknown. | `InstalledPackages` |
| [`isKnown()`](#method-isKnown) | Returns whether the installed packages are known. | `InstalledPackages` |
| [`version()`](#method-version) | Returns the installed pretty version of `$name`, or `null` when it is not installed or unknown. | `InstalledPackages` |

## Method details

### __construct() <span id="method-__construct"></span>

```php
public function __construct(array<string, array{version: string|null, path: string|null}>|null $packages)
```

Creates an installed-package view from normalized Composer metadata, or an unknown view from `null`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$packages` | `array<string, array{version: string\|null, path: string\|null}>\|null` | Installed packages keyed by name, with their pretty version and absolute install path, or `null` when the installed packages are unknown. |

### detect() <span id="method-detect"></span>

```php
public static function detect(string $vendorDir, string $projectRoot): self
```

Reads the installed packages of the project from `<vendorDir>/composer/installed.php`, then from the
`Composer\InstalledVersions` runtime data when its root package is `$projectRoot`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$vendorDir` | `string` | Absolute path to the Composer vendor directory of the project. |
| `$projectRoot` | `string` | Absolute path to the project root. |

**Returns**

`self`: Installed packages, or an unknown list when neither source describes the project.

### fromRawData() <span id="method-fromRawData"></span>

```php
public static function fromRawData(mixed $data): self
```

Builds the list from the array returned by a `vendor/composer/installed.php` file.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$data` | `mixed` | Decoded installed data with a `versions` map; any other value yields an unknown list. |

**Returns**

`self`: Installed packages that declare an install path, or an unknown list when `$data` is malformed.

### has() <span id="method-has"></span>

```php
public function has(string $name): bool
```

Returns whether `$name` is installed; `false` when the installed packages are unknown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Package name to look up. |

**Returns**

`bool`: `true` when the package is installed, `false` when it is not installed or the list is unknown.

### installPath() <span id="method-installPath"></span>

```php
public function installPath(string $name): string|null
```

Returns the absolute install path of `$name`, or `null` when it is not installed or unknown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Package name to look up. |

**Returns**

`string|null`: Absolute install path, or `null` when the package is not installed or the list is unknown.

### isKnown() <span id="method-isKnown"></span>

```php
public function isKnown(): bool
```

Returns whether the installed packages are known.

**Returns**

`bool`: `true` when an installed package source describes the project, `false` otherwise.

### version() <span id="method-version"></span>

```php
public function version(string $name): string|null
```

Returns the installed pretty version of `$name`, or `null` when it is not installed or unknown.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$name` | `string` | Package name to look up. |

**Returns**

`string|null`: Pretty version, or `null` when it is missing, not installed, or the list is unknown.