承接 wilaak/datastar-swoldier 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wilaak/datastar-swoldier

最新稳定版本:v0.2.0

Composer 安装命令:

composer require wilaak/datastar-swoldier

包简介

Datastar SDK for Swoldier

README 文档

README

Datastar SDK for Swoldier

This package offers an SDK for working with Datastar in Swoldier.

Install

composer require wilaak/datastar-swoldier

Usage

A minimal example of using Datastar with Swoldier:

<?php 

use Swoole\{Runtime, Http\Server};
use Swoldier\{Router, HttpContext};

use DatastarSwoldier\{SSE, ElementPatchMode};

// Replace blocking PHP functions with coroutine-friendly versions
Runtime::enableCoroutine();

$server = new Server("0.0.0.0", 8082);
$router = new Router($server);

$router->map('GET', '/', function (HttpContext $ctx) {
    if (SSE::isDatastarRequest($ctx)) {
        $sse = new SSE($ctx);
        $sse->patchElements('<h1 id="message">Welcome to Datastar with Swoldier!</h1>');
        return;
    }
    $ctx->html(<<<'HTML'
     <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">
        <script type="module" src="/path/to/datastar.js"></script>
    </head>
    <body data-init="@get('/')">
        <div id="message"></div>
    </body>
    </html>
    HTML);
});

$server->start();

This example covers most of the usage possible with this SDK:

use Swoole\{Runtime, Http\Server};
use Swoldier\{Router, HttpContext};

use DatastarSwoldier\{SSE, ElementPatchMode};

// Replace blocking PHP functions with coroutine-friendly versions
Runtime::enableCoroutine();

$server = new Server("0.0.0.0", 8082);
$router = new Router($server);

$router->map('GET', '/sse-endpoint', function (HttpContext $ctx) {

    // Creates a new SSE instance.
    $sse = new SSE($ctx);

    // Reads signals from the request.
    $signals = $sse->readSignals();

    // Patches elements into the DOM.
    $sse->patchElements('<div></div>', [
        'selector' => '#my-div',
        'mode' => ElementPatchMode::Append,
        'useViewTransition' => true,
    ]);

    // Removes elements from the DOM.
    $sse->removeElements('#my-div', [
        'useViewTransition' => true,
    ]);

    // Patches signals.
    $sse->patchSignals(['foo' => 123], [
        'onlyIfMissing' => true,
    ]);

    // Executes JavaScript in the browser.
    $sse->executeScript('console.log("Hello, world!")', [
        'autoRemove' => true,
        'attributes' => [
            'type' => 'application/javascript',
        ],
    ]);

    // Redirects the browser by setting the location to the provided URI.
    $sse->location('/guide');
});

$server->start();

License

MIT

统计信息

  • 总下载量: 5
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 0
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-11-10