starfederation / datastar-php
A PHP SDK for working with Datastar.
1.0.0-beta.6
2025-01-08 22:27 UTC
Requires
- php: >=8.1
Requires (Dev)
- craftcms/ecs: dev-main
- craftcms/phpstan: dev-main
- pestphp/pest: ^3.5
This package is auto-updated.
Last update: 2025-01-08 22:33:30 UTC
README
Datastar PHP SDK
This package provides a PHP SDK for working with Datastar.
License
This package is licensed for free under the MIT License.
Requirements
This package requires PHP 8.1 or later.
Installation
Install using composer.
composer require starfederation/datastar-php
Usage
use starfederation\datastar\enums\EventType; use starfederation\datastar\enums\FragmentMergeMode; use starfederation\datastar\ServerSentEventGenerator; // Creates a new `ServerSentEventGenerator` instance. $sse = new ServerSentEventGenerator(); // Sends the response headers. // If your framework has its own way of sending response headers, manually send the headers returned by `ServerSentEventGenerator::headers()` instead. $sse->sendHeaders(); // Merges HTML fragments into the DOM. $sse->mergeFragments('<div></div>', [ 'selector' => '#my-div', 'mergeMode' => FragmentMergeMode::Append, 'settleDuration' => 1000, 'useViewTransition' => true, ]); // Removes HTML fragments from the DOM. $sse->removeFragments('#my-div'); // Merges signals. $sse->mergeSignals('{foo: 123}', [ 'onlyIfMissing' => true, ]); // Removes signals. $sse->removeSignals(['foo', 'bar']); // Executes JavaScript in the browser. $sse->executeScript('console.log("Hello, world!")');
use starfederation\datastar\ServerSentEventGenerator; $signals = ServerSentEventGenerator::readSignals();