HandleComponents
Magewire specific (since: 3.0.0)
HandleComponents (sort order 1100) is the heart of the pipeline. It runs a component's lifecycle
and owns the snapshot: the serialisable representation of a component's state that travels
between the server and the browser.
Where ResolveComponents decides which component a block becomes, HandleComponents
decides what happens to it: boot, mount or hydrate, apply updates, render, dehydrate.
What it does
| Operation | Method | When |
|---|---|---|
| Mount | mount($name, $params, $key, $block, $component) |
Initial render: build a fresh component and run mount(). |
| Update | update($snapshot, $updates, $calls, $block) |
An XHR update: restore from snapshot, apply property updates and method calls, re-render. |
| From snapshot | fromSnapshot($snapshot, $block) |
Rebuild a component instance from an incoming snapshot. |
| Snapshot | snapshot($component, $context) |
Dehydrate a component into a Snapshot. |
| Update a property | updateProperty($component, $path, $value, $context) |
Apply a single incoming property change (public properties only). |
Each operation runs the lifecycle events other parts of the framework hook into: mount,
hydrate, update, call, render, dehydrate (see Component Hooks and
Lifecycle Hooks).
The snapshot
A Snapshot is three things:
| Part | Holds |
|---|---|
data |
The component's public property values (dehydrated through synthesizers). |
memo |
Metadata needed to reconstruct the component: name, id, resolver accessor, layout handles, feature flags. |
checksum |
An integrity hash over data + memo. |
On every update the incoming snapshot's checksum is verified before component reconstruction and property
hydration. Route validation, request setup, and the form-key check happen earlier. If the checksum does not match, the
payload was tampered with and the request is rejected
(CorruptComponentPayloadException). This protects the serialized snapshot, but public properties can still be
changed through component updates and must not be trusted blindly. See Security.
Synthesizers
Property values aren't always plain scalars. Synthesizers teach HandleComponents how to
dehydrate and hydrate richer types: arrays, \stdClass, backed enums, and Magento's
\Magento\Framework\DataObject. The DataObject synth is registered but its Magewire 3.7 array-cast implementation
does not guarantee a correct round trip. Each public property is matched to a synth during snapshot and restore.
Register your own by adding it to the synthesizers argument of the mechanism in DI:
<type name="Magewirephp\Magewire\Mechanisms\HandleComponents\HandleComponents">
<arguments>
<argument name="synthesizers" xsi:type="array">
<item name="my_type" xsi:type="string">
Vendor\Module\Magewire\Synthesizers\MyTypeSynth
</item>
</argument>
</arguments>
</type>
See Synthesizers for writing one.
Related
- Mechanisms: the pipeline overview.
- Resolvers: what runs before this mechanism.
- HandleRequests: what drives this mechanism on an XHR.
- Synthesizers: custom property (de)hydration.
- Hydration: the snapshot round-trip concept.