# ProjectContext

> Represents the project that the scaffold commands inspect: its root, vendor directory, configuration and installed packages.

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

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

Represents the project that the scaffold commands inspect: its root, vendor directory, configuration and installed
packages.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-config"></span>`$config` readonly constructor | [`ProjectConfig`](/docs/extensions/scaffold/reference/config/projectconfig/index.md) | Consumer settings from the project `composer.json`. | `ProjectContext` |
| <span id="property-installed"></span>`$installed` readonly constructor | [`InstalledPackages`](/docs/extensions/scaffold/reference/config/installedpackages/index.md) | Packages installed in the project. | `ProjectContext` |
| <span id="property-projectRoot"></span>`$projectRoot` readonly constructor | `string` | Absolute path to the project root. | `ProjectContext` |
| <span id="property-vendorDir"></span>`$vendorDir` readonly constructor | `string` | Absolute path to the Composer vendor directory. | `ProjectContext` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a project context from its resolved filesystem and Composer metadata. | `ProjectContext` |
| [`create()`](#method-create) static | Creates the context of the project rooted at `$projectRoot` from its `composer.json` and installed packages. | `ProjectContext` |
| [`isInstalled()`](#method-isInstalled) | Returns whether the provider is installed: from the installed packages when known, otherwise from the existence of its resolved root directory. | `ProjectContext` |
| [`providerRoot()`](#method-providerRoot) | Resolves the root of a provider from its Composer install path, then its lock-recorded path inside the vendor directory, then `<vendorDir>/<providerName>`. | `ProjectContext` |
| [`providers()`](#method-providers) | Returns the allowed providers that are installed, as descriptors whose `extra` the scaffold core reads from their `composer.json`. | `ProjectContext` |

## Method details

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

```php
public function __construct(string $projectRoot, string $vendorDir, PHPForge\Scaffold\Config\ProjectConfig $config, PHPForge\Scaffold\Config\InstalledPackages $installed)
```

Creates a project context from its resolved filesystem and Composer metadata.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$projectRoot` | `string` | Absolute path to the project root. |
| `$vendorDir` | `string` | Absolute path to the Composer vendor directory. |
| `$config` | [`ProjectConfig`](/docs/extensions/scaffold/reference/config/projectconfig/index.md) | Consumer settings from the project `composer.json`. |
| `$installed` | [`InstalledPackages`](/docs/extensions/scaffold/reference/config/installedpackages/index.md) | Packages installed in the project. |

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

```php
public static function create(string $projectRoot): self
```

Creates the context of the project rooted at `$projectRoot` from its `composer.json` and installed packages.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$projectRoot` | `string` | Absolute path to the project root. |

**Returns**

`self`: Project context with the configuration and installed packages of the project.

**Throws**

- [`RuntimeException`](https://www.php.net/manual/en/class.runtimeexception.php): when the project `composer.json` is missing or invalid.

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

```php
public function isInstalled(string $providerName, mixed $providerLock = null): bool
```

Returns whether the provider is installed: from the installed packages when known, otherwise from the
existence of its resolved root directory.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$providerName` | `string` | Provider package name. |
| `$providerLock` | `mixed` | Decoded `providers.<name>` entry of `scaffold-lock.json`, or `null` when absent. |

**Returns**

`bool`: `true` when the provider is installed in the project, `false` otherwise.

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

```php
public function providerRoot(string $providerName, mixed $providerLock = null): array{root: string, warning: string|null}
```

Resolves the root of a provider from its Composer install path, then its lock-recorded path inside the vendor
directory, then `<vendorDir>/<providerName>`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$providerName` | `string` | Provider package name. |
| `$providerLock` | `mixed` | Decoded `providers.<name>` entry of `scaffold-lock.json`, or `null` when absent. |

**Returns**

`array{root: string, warning: string|null}`: Provider root, and a warning when the lock-recorded path resolves outside the vendor directory.

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

```php
public function providers(array<string, mixed> $lockProviders): array{providers: array<string, PHPForge\Scaffold\Config\ProviderPackage>, warnings: array<string, string>}
```

Returns the allowed providers that are installed, as descriptors whose `extra` the scaffold core reads from
their `composer.json`.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$lockProviders` | `array<string, mixed>` | `providers` map of `scaffold-lock.json`, used for recorded paths and, when the installed packages are unknown, recorded versions. |

**Returns**

`array{providers: array<string,` [`ProviderPackage`](/docs/extensions/scaffold/reference/config/providerpackage/index.md)`>, warnings: array<string, string>}`: Providers keyed by name in `allowed-packages` order, and warnings about provider roots keyed by provider name.