Enhance 的服务器端渲染 PHP 库

v0.0.5 2024-05-03 14:29 UTC

This package is auto-updated.

Last update: 2024-09-23 18:55:25 UTC


README

一个用于在 PHP 中服务器端渲染兼容 Enhance SSR (Enhance.dev) 的网络组件库。

运行时:WASM 或原生 PHP

此包包含增强 ssr 的 WASM 和原生 PHP 版本。WASM 版本允许用 JavaScript 编写的组件定义,但可能对托管环境有特定要求,可能具有挑战性。示例目录包含两个版本的示例。

安装

此包可以使用 Composer 进行管理和安装

composer require enhance-dev/ssr

当前的 Extism 依赖项需要在 composer.json 文件中指定 "minimum-stability":"dev"

运行示例

要运行原生和 WASM 示例,分别运行 composer serve-nativecomposer serve-wasm

使用方法

请参阅示例目录中的原生 PHP 和 WASM 的使用示例。

原生 PHP

<?php
require "../../../vendor/autoload.php";

use Enhance\Enhancer;
use Enhance\Elements;
use Enhance\ShadyStyles;

$elementPath = __DIR__ . "/../resources";
$elements = new Elements($elementPath);
$scopeMyStyle = new ShadyStyles();
$enhance = new Enhancer([
    "elements" => $elements,
    "initialState" => [],
    "styleTransforms" => [[$scopeMyStyle, "styleTransform"]],
    "enhancedAttr" => true,
    "bodyContent" => false,
]);

$htmlString = <<<HTMLDOC
<!DOCTYPE html>
       <html>
       <head>
       </head>
       <body>
           <my-header><h1>Hello World</h1></my-header>
       </body>
       </html>
HTMLDOC;

$output = $enhance->ssr($htmlString);

echo $output;

WASM

<?php
require "../../../vendor/autoload.php";

use Enhance\EnhanceWASM;
use Enhance\Elements;

$elementPath = "../resources";
$elements = new Elements($elementPath, ["wasm" => true]);
$enhance = new EnhanceWASM(["elements" => $elements->wasmElements]);

$input = [
    "markup" => "<my-header>Hello World</my-header>",
    "initialState" => [],
];

$output = $enhance->ssr($input);

$htmlDocument = $output->document;

echo $htmlDocument . "\n";

安装 Extism 运行时依赖(仅限 WASM)

对于 WASM 版本,有一些额外的要求。对于这个库,您首先需要通过遵循 PHP SDK 仓库中的说明 安装 Extism 运行时。

致谢

感谢 @mariohamann 在 Extism 中原型化 PHP 示例 https://github.com/mariohamann/enhance-ssr-wasm/tree/experiment/extism