定制 phpdot/bun 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

phpdot/bun

Composer 安装命令:

composer require phpdot/bun

包简介

A PHP wrapper around the Bun binary (oven-sh/bun, MIT licensed): manages a hidden Bun runtime and exposes its CLI as console commands.

README 文档

README

A PHP wrapper around the Bun binary. It manages a hidden Bun binary — downloaded on first use into .phpdot/runtime/, integrity-verified, and pinned to a reproducible version — and exposes Bun's CLI as console commands and as an injectable service.

This package is a thin wrapper. It does not reimplement a bundler, a package manager, or a runtime; it delegates to the Bun binary.

phpdot/bun is a PHP wrapper around Bun (oven-sh/bun), which is MIT licensed.

Requirements

  • PHP 8.4+
  • ext-curl (registry access and binary download)
  • A PSR-18 client + PSR-17 factories — bundled by default via symfony/http-client + nyholm/psr7
  • ext-pcntl (suggested) — forwards SIGINT/SIGTERM to long-lived processes (run dev server, build --watch) for clean shutdown
  • On Alpine/musl, install libstdc++ (apk add libstdc++) — the downloaded Bun binary links against it at runtime. Glibc distros and macOS/Windows already provide it.

Installation

composer require phpdot/bun

Add the runtime directory to your .gitignore — the binary is downloaded per machine, never committed:

/.phpdot/

Configuration

BunConfig is an immutable value object resolved from the container (#[Config('bun')]):

Setting Default Notes
pinnedVersion 1.3.14 Reproducible — never tracks "latest". Override with the BUN_VERSION env var.
registryUrl https://registry.npmjs.org Used for search, metadata, and download — point it at a mirror in one place.
runtimeDir .phpdot/runtime Where the binary is cached, relative to the project root.

Console commands

Registered into your phpdot console application (discovered via #[AsCommand]):

# Discovery (uses the npm registry; never needs the binary)
bun:search chart --limit=20

# Packages
bun:install lodash axios          # bun add
bun:install --dev typescript      # bun add --dev
bun:remove lodash                 # bun remove
bun:view react                    # bun pm view

# Run scripts and tools (output streams live; Ctrl-C is forwarded)
bun:run dev                       # bun run dev
bun:run dev -- --port 3000        # flags for the script go after --
bun:x prettier src -- --write     # bun x — run any installed CLI tool

# Bundle
bun:build src/index.ts --out-dir=dist --minify --splitting --hashed-names
bun:build src/index.ts --watch    # long-lived: rebuilds on change, exits cleanly on signal

Passthrough commands (bun:run, bun:x) forward arguments verbatim. Put flags meant for the script/tool after a -- separator so the console does not parse them.

Build-step tools

There is no special-casing for any tool. Install it and run it with bun:x:

bun:install javascript-obfuscator
bun:x javascript-obfuscator app.js -- --output app.obf.js

Programmatic use

Inject the Bun service — there is no static entry point or facade:

use PHPdot\Bun\Bun;
use PHPdot\Bun\Build\BuildOptions;

final class Deploy
{
    public function __construct(private readonly Bun $bun) {}

    public function bundle(): void
    {
        $this->bun->install(['lodash']);
        $this->bun->build(
            ['src/index.ts'],
            new BuildOptions(outDir: 'dist', minify: true, splitting: true, hashedNames: true),
        );
    }
}

Each call resolves the binary (downloading it on first use) and streams Bun's output through to the console, returning Bun's exit code.

How it works

  1. PlatformDetector resolves the host OS, architecture, and (on Linux) libc flavour.
  2. BinaryResolver checks the cached binary; if missing or version-mismatched it acquires a lock and downloads the matching @oven/bun-* npm package, verifies its sha512 integrity, and extracts it.
  3. If the standard x64 binary fails to execute (e.g. no AVX2 / illegal instruction), it falls back to the -baseline variant automatically.
  4. Concurrent first-use downloads are guarded by a file lock.

License

MIT. Bun itself is MIT licensed; redistribution via download preserves that notice.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2026-06-17