# UserCollector

> Captures the current yiisoft/user identity and its yiisoft/rbac roles and permissions for the User panel.

[API reference](/docs/api/index.md) › [Yii3 Debug](/docs/extensions/yii3-debug/reference/index.md) › [Collector](/docs/extensions/yii3-debug/reference/collector/index.md) › UserCollector

| final class | `Yii3\Debug\Collector\UserCollector` |
| --- | --- |
| Implements | [`CollectorInterface`](https://github.com/php-forge/debug/blob/e4fbb8c3238a4d011ceef5e0094344abd50deaf4/src/CollectorInterface.php), [`RequestObserverInterface`](/docs/extensions/yii3-debug/reference/collector/requestobserverinterface/index.md) |
| Source | [`src/Collector/UserCollector.php`](https://github.com/yii3/debug/blob/main/src/Collector/UserCollector.php#L35) |

Captures the current `yiisoft/user` identity and its `yiisoft/rbac` roles and permissions for the User panel.

The payload has the shape the Yii2 collector produces, so a capture written by either host renders through the same
[`UserPanel`](https://github.com/php-forge/debug-core/blob/53c259241281d3275cd5cbfa64f08eb986d6bce3/src/Panel/User/UserPanel.php). The identity is read when the response leaves the application, while
the session that restores it is still readable; the capture itself runs after the response was sent.

Both packages stay optional: [`CurrentUser`](https://github.com/yiisoft/user/blob/f982e9faa3acac8a04299968b1c74e5123e4cc14/src/CurrentUser.php) and [`ManagerInterface`](https://github.com/yiisoft/rbac/blob/229a47e0af4ede19dfe6da2220af7cabee724dbf/src/ManagerInterface.php) are resolved from the container only when
it defines them, so an application without RBAC still captures the identity, and one without `yiisoft/user`
captures nothing.

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Creates a user collector that resolves identity and RBAC data through the container. | `UserCollector` |
| [`capture()`](#method-capture) | Encodes the identity into the User panel payload. | `UserCollector` |
| [`collectRequest()`](#method-collectRequest) | Ignores the incoming request, since the identity is only final once the application handled it. | `UserCollector` |
| [`collectResponse()`](#method-collectResponse) | Reads the identity before the response is sent and the session is closed. | `UserCollector` |
| [`id()`](#method-id) | Returns the stable identifier of this collector. | `UserCollector` |
| [`shutdown()`](#method-shutdown) | Stops capturing and discards the identity, or the failure, read for the request. | `UserCollector` |
| [`startup()`](#method-startup) | Starts capturing, discarding anything left from a previous request. | `UserCollector` |

## Method details

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

```php
public function __construct(Psr\Container\ContainerInterface $container, PHPForge\Debug\Capture\CapturePolicy $capturePolicy = new \PHPForge\Debug\Capture\CapturePolicy(), Closure(Yiisoft\Auth\IdentityInterface): array<array-key, mixed>|null $identityData = null)
```

Creates a user collector that resolves identity and RBAC data through the container.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$container` | `Psr\Container\ContainerInterface` | Container resolving the current user and the optional RBAC manager. |
| `$capturePolicy` | [`CapturePolicy`](https://github.com/php-forge/debug-core/blob/b70459f033a1d7a1c5edf7951029aad2061986dd/src/Capture/CapturePolicy.php) | Policy naming the identity attributes, at any depth, stored as the redaction placeholder. |
| `$identityData` | [`Closure`](https://www.php.net/manual/en/class.closure.php)`(Yiisoft\Auth\IdentityInterface): array<array-key, mixed>\|null` | Reader returning the attributes of an identity the default rule cannot see, such as one wrapping an entity; `null` reads `toArray()`, then `jsonSerialize()`, then the public properties. |

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

```php
public function capture(): array<string, mixed>|null
```

Encodes the identity into the User panel payload.

A capture finalized without a response, such as one written when the application exits early, reads the identity
now. A failure kept from the response is thrown here, so the capture records it as a panel failure.

**Returns**

`array<string, mixed>|null`: Encoded User panel payload; `null` when the collector never started or the container defines no current user.

**Throws**

- [`Throwable`](https://www.php.net/manual/en/class.throwable.php): when the current user cannot be resolved, or its identity cannot be restored or read.

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

```php
public function collectRequest(Psr\Http\Message\ServerRequestInterface $request): void
```

Ignores the incoming request, since the identity is only final once the application handled it.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$request` | `Psr\Http\Message\ServerRequestInterface` | Request reaching the debugger middleware. |

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

```php
public function collectResponse(Psr\Http\Message\ResponseInterface $response): void
```

Reads the identity before the response is sent and the session is closed.

A failure never reaches the application, which already produced its response; it is kept for [`capture()`](/docs/extensions/yii3-debug/reference/collector/usercollector/index.md#method-capture).

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$response` | `Psr\Http\Message\ResponseInterface` | Response produced for the captured request. |

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

```php
public function id(): string
```

Returns the stable identifier of this collector.

**Returns**

`string`: Stable ID pairing this collector with its panel.

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

```php
public function shutdown(): void
```

Stops capturing and discards the identity, or the failure, read for the request.

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

```php
public function startup(): void
```

Starts capturing, discarding anything left from a previous request.