|
| 1 | +# CLAUDE.md |
| 2 | + |
| 3 | +This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository. |
| 4 | + |
| 5 | +## Project Overview |
| 6 | + |
| 7 | +@platformatic/php-node is a Node.js native addon that embeds PHP within the same process as Node.js, enabling seamless communication without network overhead. It's built with Rust using NAPI-RS for safe and performant bindings. |
| 8 | + |
| 9 | +## Essential Commands |
| 10 | + |
| 11 | +### Development |
| 12 | +```bash |
| 13 | +# Build release version |
| 14 | +npm run build |
| 15 | + |
| 16 | +# Build debug version (faster compilation, includes debug symbols) |
| 17 | +npm run build:debug |
| 18 | + |
| 19 | +# Run all tests |
| 20 | +npm test |
| 21 | + |
| 22 | +# Run specific test file |
| 23 | +npx ava __test__/headers.spec.mjs |
| 24 | + |
| 25 | +# Lint JavaScript code |
| 26 | +npm run lint |
| 27 | + |
| 28 | +# Create universal binary (macOS) |
| 29 | +npm run universal |
| 30 | + |
| 31 | +# Version bump |
| 32 | +npm run version |
| 33 | +``` |
| 34 | + |
| 35 | +### Rust-specific builds |
| 36 | +```bash |
| 37 | +# Build with proper rpath for linking libphp |
| 38 | +RUSTFLAGS="-C link-args=-Wl,-rpath,\$ORIGIN" npm run build |
| 39 | + |
| 40 | +# Build specific crate |
| 41 | +cargo build --manifest-path crates/php_node/Cargo.toml --release |
| 42 | +``` |
| 43 | + |
| 44 | +## Architecture |
| 45 | + |
| 46 | +### Multi-language Structure |
| 47 | +- **Rust** (`/crates`): Core implementation using Cargo workspace |
| 48 | + - `lang_handler`: Generic language handler abstractions |
| 49 | + - `php`: PHP embedding and SAPI implementation |
| 50 | + - `php_node`: NAPI bindings exposing Rust to Node.js |
| 51 | +- **JavaScript**: Node.js API layer (`index.js`, `index.d.ts`) |
| 52 | +- **PHP**: Embedded runtime via libphp.{so,dylib} |
| 53 | + |
| 54 | +### Key Components |
| 55 | + |
| 56 | +1. **PHP Class** (`index.js`): Main entry point for creating PHP environments |
| 57 | + - Manages rewriter rules for URL routing |
| 58 | + - Handles request/response lifecycle |
| 59 | + - Supports both sync and async request handling |
| 60 | + |
| 61 | +2. **Request/Response Model**: Web standards-compatible implementation |
| 62 | + - `Request` class with headers, body, method |
| 63 | + - `Response` class with status, headers, body |
| 64 | + - `Headers` class with case-insensitive header handling |
| 65 | + |
| 66 | +3. **Rewriter System**: Apache mod_rewrite-like functionality |
| 67 | + - Conditional rules with regex patterns |
| 68 | + - Environment variable support |
| 69 | + - Rule chaining with [L], [R], [C] flags |
| 70 | + |
| 71 | +4. **SAPI Implementation**: Custom PHP SAPI in Rust |
| 72 | + - Direct Zend API usage for performance |
| 73 | + - Thread-safe with TSRM support |
| 74 | + - Reusable PHP environments across requests |
| 75 | + |
| 76 | +## Critical Development Notes |
| 77 | + |
| 78 | +1. **System Dependencies Required**: |
| 79 | + - Linux: `libssl-dev libcurl4-openssl-dev libxml2-dev libsqlite3-dev libonig-dev re2c libpq5` |
| 80 | + - macOS: `openssl@3 curl sqlite libxml2 oniguruma postgresql@16` |
| 81 | + |
| 82 | +2. **PHP Runtime**: Must have `libphp.so` (Linux) or `libphp.dylib` (macOS) in project root |
| 83 | + |
| 84 | +3. **Testing**: AVA framework with 3-minute timeout due to PHP startup overhead |
| 85 | + |
| 86 | +4. **Type Definitions**: `index.d.ts` is auto-generated by NAPI-RS - do not edit manually |
| 87 | + |
| 88 | +5. **Platform Support**: x64 Linux, x64/arm64 macOS (pre-built binaries in `/npm`) |
| 89 | + |
| 90 | +6. **Recent Architecture Changes**: |
| 91 | + - `lang_handler` crate no longer uses `napi` features directly (removed from dependencies) |
| 92 | + - `php` crate uses custom fork of ext-php-rs from platformatic GitHub org |
| 93 | + |
| 94 | +## Common Tasks |
| 95 | + |
| 96 | +### Adding New NAPI Functions |
| 97 | +1. Implement in Rust under `crates/php_node/src/` |
| 98 | +2. Use `#[napi]` attributes for exposed functions/classes |
| 99 | +3. Run `npm run build` to regenerate TypeScript definitions |
| 100 | + |
| 101 | +### Modifying Request/Response Handling |
| 102 | +- Core logic in `crates/php/src/sapi.rs` |
| 103 | +- JavaScript wrapper in `index.js` |
| 104 | +- Headers handling in `crates/php_node/src/headers.rs` |
| 105 | + |
| 106 | +### Debugging PHP Issues |
| 107 | +- Check INTERNALS.md for PHP embedding details |
| 108 | +- Use `npm run build:debug` for debug symbols |
| 109 | +- PHP superglobals set via `SG(...)` macro in Rust code |
| 110 | + |
| 111 | +### Working with Rewriter Rules |
| 112 | +The rewriter system supports Apache mod_rewrite-like functionality: |
| 113 | +- Create rules with conditions (header, host, method, path, query) |
| 114 | +- Apply rewriters (header, href, method, path, query, status) |
| 115 | +- Use flags like [L] (last), [R] (redirect), [C] (chain) |
| 116 | +- Example: `new Rewriter([{ conditions: [{type: 'path', args: ['^/old/(.*)$']}], rewriters: [{type: 'path', args: ['^/old/(.*)$', '/new/$1']}] }])` |
| 117 | + |
| 118 | +## Project Files Reference |
| 119 | + |
| 120 | +- `index.js`: Main JavaScript API, exports PHP, Request, Response, Headers, Rewriter classes |
| 121 | +- `crates/php_node/src/lib.rs`: NAPI bindings entry point |
| 122 | +- `crates/php/src/sapi.rs`: PHP SAPI implementation (core request handling) |
| 123 | +- `crates/lang_handler/src/`: Generic language handler abstractions (request/response/rewriter) |
| 124 | +- `__test__/*.spec.mjs`: Test files for each component |
0 commit comments