melmuk/luasandbox-wrapper 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

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

melmuk/luasandbox-wrapper

最新稳定版本:1.1.0

Composer 安装命令:

composer require melmuk/luasandbox-wrapper

包简介

PHP wrapper around the luasandbox extension.

README 文档

README

PHP wrapper around the luasandbox extension.

Why this wrapper

ext-luasandbox is powerful but low-level. This package gives you:

  • simple execute(array $data, LuaCode $code): mixed API
  • deterministic per-run sandbox isolation by default
  • typed wrapper exceptions
  • explicit conversion modes: strict and native-compatible
  • configurable memory/CPU limits
  • pluggable print output sinks with output-size guard
  • rich execution result (value, output, metrics)

Installation

composer require melmuk/luasandbox-wrapper

Requirements:

  • PHP ^8.1
  • ext-luasandbox

Quick start

<?php

use Melmuk\LuaSandboxWrapper\LuaCode;
use Melmuk\LuaSandboxWrapper\LuaExecutor;

$executor = new LuaExecutor();

$code = new LuaCode(<<<'LUA'
function execute(data)
    print("hello", data.id)
    data.handled = true
    return data
end
LUA);

$result = $executor->execute(['id' => 42], $code);

Advanced usage

<?php

use Melmuk\LuaSandboxWrapper\LuaCode;
use Melmuk\LuaSandboxWrapper\Conversion\ConversionMode;
use Melmuk\LuaSandboxWrapper\LuaExecutor;
use Melmuk\LuaSandboxWrapper\Output\BufferedOutputSink;
use Melmuk\LuaSandboxWrapper\SandboxConfig;

$sink = new BufferedOutputSink();

$config = SandboxConfig::defaults()
    ->withMemoryLimitBytes(32 * 1024 * 1024)
    ->withCpuLimitSeconds(0.5)
    ->withConversionMode(ConversionMode::STRICT)
    ->blacklistLuaGlobals(['math.random'])
    ->withOutputSink($sink)
    ->withMaxOutputBytes(64 * 1024);

$executor = new LuaExecutor($config);

$execution = $executor->run(['id' => 42], LuaCode::forFunction(<<<'LUA'
function execute(data)
    print("processing", data.id)
    data.ok = true
    return data
end
LUA));

$value = $execution->value();
$output = $execution->output();
$durationMs = $execution->durationMs();

Error handling

Catch typed exceptions from Melmuk\LuaSandboxWrapper\Exception:

  • LuaSandboxExtensionMissingException
  • LuaCompilationException
  • LuaRuntimeException
  • LuaFunctionNotFoundException
  • OutputLimitExceededException
  • LuaExecutionException (base type)
<?php

use Melmuk\LuaSandboxWrapper\Exception\LuaExecutionException;

try {
    $executor->execute($data, $code);
} catch (LuaExecutionException $e) {
    // $e->functionName(), $e->phase(), $e->getMessage()
}

Output sinks

Included sinks:

  • StdoutOutputSink (default)
  • BufferedOutputSink

You can implement Melmuk\LuaSandboxWrapper\Output\OutputSink to route print output to logs, queues, etc.

Function Access Tuning

Default behavior uses a blacklist overlay on top of LuaSandbox defaults.

<?php

$config = SandboxConfig::defaults()
    ->blacklistLuaGlobals(['math.random', 'debug.traceback'])
    ->blacklistLuaLibraries(['os']);

Switch to explicit whitelist mode when you need a strict script contract:

<?php

$config = SandboxConfig::defaults()
    ->whitelistLuaGlobals(['pairs', 'ipairs'])
    ->whitelistLuaLibraries(['string']);

Testing

composer install
composer test

Tests include config, code object, and integration behavior for execution, output capture, and exception mapping.

Docs

  • docs/quickstart.md
  • docs/conversion.md
  • docs/wrapper-vs-extension.md
  • docs/reference.md

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: ISC
  • 更新时间: 2026-03-01

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固