# DispatcherAttacher

> Hands every collector named in yii\debug\Module::$dispatchers to the application component the application pairs it with, so the component emits its PSR-14 events into the collector.

[API reference](/docs/api/index.md) › [Debug](/docs/extensions/debug/reference/index.md) › [service](/docs/extensions/debug/reference/service/index.md) › DispatcherAttacher

| class | `yii\debug\service\DispatcherAttacher` |
| --- | --- |
| Source | [`src/service/DispatcherAttacher.php`](https://github.com/yii2-extensions/debug/blob/main/src/service/DispatcherAttacher.php#L35) |

Hands every collector named in [`Module::$dispatchers`](/docs/extensions/debug/reference/module/index.md#property-dispatchers) to the application component the application pairs it
with, so the component emits its PSR-14 events into the collector.

The way the component takes the collector is detected, never declared: a public `eventDispatcher` property (or a Yii
getter/setter pair) wins, then a constructor parameter named `eventDispatcher`. A definition is amended without
instantiating the component; an instantiated component only accepts the property form. A dispatcher the component
already configures is kept, so a populated application dispatcher is never replaced by the collector.

## Properties

| Property | Type | Description | Defined by |
| --- | --- | --- | --- |
| <span id="property-module"></span>`$module` protected readonly constructor | [`yii\debug\Module`](/docs/extensions/debug/reference/module/index.md) | Debug module owning the dispatcher map and the collector coordinator, read at call time. | `DispatcherAttacher` |

## Methods

| Method | Description | Defined by |
| --- | --- | --- |
| [`__construct()`](#method-__construct) | Binds dispatcher attachment to the module's collectors and component map. | `DispatcherAttacher` |
| [`attach()`](#method-attach) | Attaches every configured collector to its application component. | `DispatcherAttacher` |
| [`dispatcherArgumentKey()`](#method-dispatcherArgumentKey) protected | Returns the `__construct()` key a component definition takes its dispatcher under. | `DispatcherAttacher` |

## Method details

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

```php
public function __construct(yii\debug\Module $module)
```

Binds dispatcher attachment to the module's collectors and component map.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$module` | [`yii\debug\Module`](/docs/extensions/debug/reference/module/index.md) | Debug module owning the dispatcher map and the collector coordinator, read at call time. |

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

```php
public function attach(yii\base\Application $app): void
```

Attaches every configured collector to its application component.

Runs once every bootstrap class had its turn, so a component a bootstrap class registers is seen. A collector
disabled through `enabled => false` is skipped; [`CollectorRegistrar`](/docs/extensions/debug/reference/service/collectorregistrar/index.md) already rejected an entry naming no
configured collector.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$app` | [`yii\base\Application`](/docs/api/yii/base/application/index.md) | Application owning the components. |

**Throws**

- [`yii\base\InvalidConfigException`](/docs/api/yii/base/invalidconfigexception/index.md): when a collector is not a PSR-14 dispatcher, or when its component is unknown, instantiated without a writable `eventDispatcher` property, or unable to take the collector.

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

```php
protected function dispatcherArgumentKey(class-string $class, array<array-key, mixed> $arguments): int|string|null
```

Returns the `__construct()` key a component definition takes its dispatcher under.

A definition indexing its arguments by position gets the dispatcher at the position its constructor declares,
because Yii rejects a definition mixing named and positional arguments; every other definition gets it by name.

Override point: a subclass returning its own key attaches the collector to a component whose constructor names
the dispatcher parameter differently.

**Parameters**

| Parameter | Type | Description |
| --- | --- | --- |
| `$class` | `class-string` | Component class whose constructor is inspected. |
| `$arguments` | `array<array-key, mixed>` | Arguments the definition already declares. |

**Returns**

`int|string|null`: Key to write the dispatcher under, or `null` when the constructor declares no `eventDispatcher` parameter.