|
1 | | -# AI Agent Guidelines for `node-jsphp` |
| 1 | +# AI Agent Guidelines for `node-jsphp` (`@nexustools/php`) |
2 | 2 |
|
3 | | -This document provides operational context and guidance for AI agents working on the `node-jsphp` codebase. |
| 3 | +This document provides operational context, architecture directives, and guidance for AI agents working on the `@nexustools/php` codebase. |
4 | 4 |
|
5 | 5 | ## Core Directives |
6 | 6 |
|
7 | 7 | 1. **Synchronous PHP / Asynchronous JS**: |
8 | | - PHP code semantics appear synchronous to the PHP script, but generated JavaScript code MUST run asynchronously using `async/await` so Node.js can handle concurrent requests without blocking. |
| 8 | + PHP code semantics appear synchronous to the PHP script, but transpiled JavaScript code MUST run asynchronously using `async/await` so Node.js can handle concurrent execution without blocking the event loop. |
9 | 9 |
|
10 | | -2. **Stack Trace Masking**: |
11 | | - Never expose raw JavaScript execution stack frames to PHP. Standardize all stack traces in `PHPError` to reflect PHP line numbers or `[INTERNAL]`. |
| 10 | +2. **Stack Trace Virtualization**: |
| 11 | + Never expose raw JavaScript execution stack frames to PHP. Standardize all stack traces in `PHPError` to reflect PHP source line numbers or `[INTERNAL]`. |
12 | 12 |
|
13 | 13 | 3. **No Build Output Directory for Sources**: |
14 | | - TypeScript source files in `src/` must compile to JavaScript `.js` and declaration `.d.ts` files placed directly alongside their corresponding `.ts` sources. Do NOT use a `dist/` or `out/` folder. |
| 14 | + TypeScript source files in `src/` compile to JavaScript `.js` and declaration `.d.ts` files placed directly alongside their corresponding `.ts` sources. Do NOT use a `dist/` or `out/` folder. |
15 | 15 |
|
16 | 16 | 4. **AST Optimization Rules**: |
17 | | - Always ensure optimizer handles `extension_loaded(...)`, `defined(...)`, and boolean expressions at compile time. Eliminate unreachable `if` branches before generating JS output. |
| 17 | + Ensure `ASTOptimizer` optimizes `extension_loaded(...)`, `defined(...)`, `constant(...)`, constant references (`name` / `constref`), and boolean expressions at compile time. Eliminate dead `if` branches prior to code generation. |
18 | 18 |
|
19 | | -5. **WordPress & MySQL Compatibility**: |
20 | | - Maintain full compatibility with WordPress database operations and standard themes. |
| 19 | +5. **CLI & Server Binaries**: |
| 20 | + - `php` (`bin/php.js`): Single-file CLI execution binary. |
| 21 | + - `php-fpm` (`bin/php-fpm.js`): FastCGI (FCGI 1.0) binary socket server for Nginx/Apache proxying. |
| 22 | + - `php-http-server` (`bin/php-http-server.js`): HTTP web server supporting static file serving and smart URL resolution (`/` -> `index.php`/`index.html`, `/test` -> `test.php`/`test.html`/`test/index.php`). |
| 23 | + |
| 24 | +6. **Internal Variables (`internalVars`)**: |
| 25 | + `PHPEngine` and `PHPContext` maintain `internalVars` (`Map<string, any>`) that are invisible to PHP userland (`$GLOBALS`, `get_defined_vars()`, Reflection), but accessible to internal engine methods, AST optimization, and server handlers. |
| 26 | + |
| 27 | +7. **Header & Response Lifecycle**: |
| 28 | + HTTP response functions (`header`, `setcookie`, `setrawcookie`, `header_remove`, `headers_list`, `headers_sent`, `http_response_code`) operate on `PHPResponse` attached to `PHPContext`. Calling header functions in CLI mode without a registered server response handler throws a `PHPWarning` / `PHPError`. |
| 29 | + |
| 30 | +8. **Error Handling & `set_error_handler`**: |
| 31 | + Supports `set_error_handler`, `restore_error_handler`, `trigger_error`, `error_reporting`, and `ErrorException`. Errors thrown within PHP methods use subclasses of `PHPError` (`PHPFatalError`, `PHPWarning`, `PHPNotice`, `PHPTypeError`, etc.). |
| 32 | + |
| 33 | +9. **Reflection Metadata & JS Source Fallback**: |
| 34 | + Function/method metadata is tracked via `.phpMeta` attached by `defineFunction`. If `.phpMeta` is absent, `ReflectionFunction` and `ReflectionMethod` fall back to parsing the function's JavaScript `.toString()` source declaration. |
| 35 | + |
| 36 | +10. **Optional Node.js Interop (`nodejs` extension)**: |
| 37 | + Provides Node.js module/package loading (`nodejs_require`), global variable access (`nodejs_global`), eval (`nodejs_eval`), and instantiation (`nodejs_new` / `NodeJS` class) wrapped via `NodeJSObject`. |
| 38 | + |
| 39 | +11. **WordPress & MySQL Compatibility**: |
| 40 | + Maintain full compatibility with WordPress database operations (`mysqli`), installation flows, and theme/admin dashboard rendering tested against a local MySQL database. |
0 commit comments