|
| 1 | +# Ray.Compiler Demo |
| 2 | + |
| 3 | +This demo shows how Ray.Compiler pre-compiles dependency injection bindings into executable PHP scripts. |
| 4 | + |
| 5 | +## Architecture |
| 6 | + |
| 7 | +Ray.Compiler uses a **two-phase architecture**: |
| 8 | + |
| 9 | +### Compile-time (Development/Build) |
| 10 | +- Uses Ray.Di's full dependency resolution |
| 11 | +- Analyzes bindings, constructs dependency graph |
| 12 | +- Generates optimized PHP scripts |
| 13 | + |
| 14 | +### Runtime (Production) |
| 15 | +- Uses minimal `CompiledInjector` |
| 16 | +- Executes pre-compiled scripts |
| 17 | +- **No reflection, no dependency resolution** |
| 18 | + |
| 19 | +## Files |
| 20 | + |
| 21 | +- `GreeterInterface.php` - Service interface |
| 22 | +- `Greeter.php` - Service implementation |
| 23 | +- `AppModule.php` - Dependency injection bindings |
| 24 | +- `compile.php` - Compilation script (run at build time) |
| 25 | +- `run.php` - Execution script (uses compiled scripts) |
| 26 | + |
| 27 | +## Usage |
| 28 | + |
| 29 | +### Step 1: Compile (Build Time) |
| 30 | + |
| 31 | +```bash |
| 32 | +php demo/compile.php |
| 33 | +``` |
| 34 | + |
| 35 | +This generates optimized PHP scripts in `demo/.compiled/`: |
| 36 | +- `Ray_Compiler_Demo_GreeterInterface-.php` |
| 37 | +- Other dependency scripts |
| 38 | + |
| 39 | +### Step 2: Run (Runtime) |
| 40 | + |
| 41 | +```bash |
| 42 | +php demo/run.php |
| 43 | +``` |
| 44 | + |
| 45 | +Output: |
| 46 | +``` |
| 47 | +Hello, World! |
| 48 | +Hello, Ray.Compiler! |
| 49 | +
|
| 50 | +✓ Using pre-compiled dependency injection! |
| 51 | +✓ No reflection, no dependency resolution at runtime |
| 52 | +✓ Check demo/.compiled/ for generated PHP scripts |
| 53 | +``` |
| 54 | + |
| 55 | +## Inspecting Generated Code |
| 56 | + |
| 57 | +After compilation, check the generated scripts: |
| 58 | + |
| 59 | +```bash |
| 60 | +cat demo/.compiled/Ray_Compiler_Demo_GreeterInterface-.php |
| 61 | +``` |
| 62 | + |
| 63 | +You'll see plain PHP code that instantiates objects - no complex DI logic! |
| 64 | + |
| 65 | +## Benefits |
| 66 | + |
| 67 | +1. **Performance**: No runtime reflection or dependency resolution |
| 68 | +2. **Zero Overhead**: Compiled scripts are simple `new` and `return` statements |
| 69 | +3. **Debuggable**: Generated code is readable PHP |
| 70 | +4. **Production Ready**: No heavy DI framework in production |
| 71 | + |
| 72 | +## How It Works |
| 73 | + |
| 74 | +``` |
| 75 | +┌─────────────────┐ ┌──────────────┐ |
| 76 | +│ Ray.Di Module │────────▶│ Compiler │ |
| 77 | +│ (Bindings) │ │ │ |
| 78 | +└─────────────────┘ └──────┬───────┘ |
| 79 | + │ |
| 80 | + ▼ |
| 81 | + ┌──────────────┐ |
| 82 | + │ PHP Scripts │ |
| 83 | + │ (Optimized) │ |
| 84 | + └──────┬───────┘ |
| 85 | + │ |
| 86 | + ▼ |
| 87 | + ┌──────────────────┐ |
| 88 | + │ CompiledInjector │ |
| 89 | + │ (Lightweight) │ |
| 90 | + └──────────────────┘ |
| 91 | +``` |
| 92 | + |
| 93 | +## Next Steps |
| 94 | + |
| 95 | +- Add more complex dependencies (interfaces, providers, AOP) |
| 96 | +- Try scopes (singleton vs prototype) |
| 97 | +- Explore assisted injection |
| 98 | +- Use with your real application modules |
0 commit comments