This repository has been archived on 2026-03-06. You can view files and clone it, but cannot push or open issues or pull requests.
go-php/extract.go
Claude 5436df2cf4
feat: FrankenPHP Go embedding library
Extracted from host-uk/core/cmd/core-app/ into a reusable package.

Provides:
- Handler: http.Handler serving PHP via in-process FrankenPHP
- Extract: embed.FS to temp dir extraction for PHP runtime
- PrepareEnvironment: .env, SQLite, persistent storage setup
- Bridge: localhost HTTP API for PHP → Go communication

Requires CGo + PHP headers. Build with: -tags nowatcher

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-23 02:49:46 +00:00

49 lines
1.1 KiB
Go

package php
import (
"fmt"
"io/fs"
"os"
"path/filepath"
)
// Extract copies an embedded Laravel app (from embed.FS) to a temporary directory.
// FrankenPHP needs real filesystem paths — it cannot serve from embed.FS.
// The prefix is the embed directory name (e.g. "laravel").
// Returns the path to the extracted Laravel root. Caller must os.RemoveAll on cleanup.
func Extract(fsys fs.FS, prefix string) (string, error) {
tmpDir, err := os.MkdirTemp("", "go-php-laravel-*")
if err != nil {
return "", fmt.Errorf("create temp dir: %w", err)
}
err = fs.WalkDir(fsys, prefix, func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
relPath, err := filepath.Rel(prefix, path)
if err != nil {
return err
}
targetPath := filepath.Join(tmpDir, relPath)
if d.IsDir() {
return os.MkdirAll(targetPath, 0o755)
}
data, err := fs.ReadFile(fsys, path)
if err != nil {
return fmt.Errorf("read embedded %s: %w", path, err)
}
return os.WriteFile(targetPath, data, 0o644)
})
if err != nil {
os.RemoveAll(tmpDir)
return "", fmt.Errorf("extract Laravel: %w", err)
}
return tmpDir, nil
}