# InstalledState

> Reads and writes the hidden state file of an install directory: the lock entry of every package the last successful native installation left in place, the counterpart of npm's node_modules/.package-lock.json.

[API reference](/docs/api/index.md) › [Foxy](/docs/extensions/foxy/reference/index.md) › [Native](/docs/extensions/foxy/reference/native/index.md) › InstalledState

| final readonly class | `Foxy\Native\InstalledState` |
| --- | --- |
| Source | [`src/Native/InstalledState.php`](https://github.com/php-forge/foxy/blob/main/src/Native/InstalledState.php#L28) |

Reads and writes the hidden state file of an install directory: the lock entry of every package the last successful
native installation left in place, the counterpart of npm's `node_modules/.package-lock.json`.

The state is a cache: a missing or malformed file reads as empty, so the installer extracts every registry package
again.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates an installed-state store for one native install directory. | `InstalledState` |
| [`read()`](#method-read) | Returns the recorded lock entries keyed by package name, in the shape of [`ResolvedPackage::toLock()`](/docs/extensions/foxy/reference/native/resolvedpackage/index.md#method-toLock). | `InstalledState` |
| [`write()`](#method-write) | Writes the state file as `{"packages": {...}}`, with the names sorted byte-wise. | `InstalledState` |

## Method details

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

```php
public function __construct(string $path, Composer\IO\IOInterface $io)
```

Creates an installed-state store for one native install directory.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$path` | `string` | State file path. |
| `$io` | `Composer\IO\IOInterface` | Output for the verbose notice of a malformed file. |

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

```php
public function read(): array<array-key, array<string, string>>
```

Returns the recorded lock entries keyed by package name, in the shape of [`ResolvedPackage::toLock()`](/docs/extensions/foxy/reference/native/resolvedpackage/index.md#method-toLock).

A missing file reads as empty. A file that is not valid JSON, has no `packages` map, or holds an entry of
another shape also reads as empty, with a verbose warning.

Keys are package names; PHP stores a numeric name such as `123` as an `int` key.

**Returns**

`array<array-key, array<string, string>>`

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

```php
public function write(array<array-key, array<string, string>> $entries): void
```

Writes the state file as `{"packages": {...}}`, with the names sorted byte-wise.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$entries` | `array<array-key, array<string, string>>` | Lock entries keyed by package name. |