Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/continuous-integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ jobs:
with:
old_stable: '["8.2", "8.3", "8.4"]'
current_stable: 8.5
script: demo/compile.php && php demo/run.php
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ build
/coverage.xml
*.bak
.claude
/demo/.compiled
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
"autoload-dev": {
"psr-4": {
"Ray\\Compiler\\": ["tests", "tests/Fake"],
"Ray\\Compiler\\Demo\\": "demo",
"Ray\\Di\\": "tests/Fake/Assisted"
},
"files": ["tests/deleteFiles.php"]
Expand Down
15 changes: 15 additions & 0 deletions demo/AppModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

declare(strict_types=1);

namespace Ray\Compiler\Demo;

use Ray\Di\AbstractModule;

final class AppModule extends AbstractModule
{
protected function configure(): void
{
$this->bind(GreeterInterface::class)->to(Greeter::class);
}
}
17 changes: 17 additions & 0 deletions demo/Greeter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Ray\Compiler\Demo;

final class Greeter implements GreeterInterface
{
public function __construct(private readonly string $greeting = 'Hello')
{
}

public function greet(string $name): string
{
return sprintf('%s, %s!', $this->greeting, $name);
}
}
10 changes: 10 additions & 0 deletions demo/GreeterInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace Ray\Compiler\Demo;

interface GreeterInterface
{
public function greet(string $name): string;
}
98 changes: 98 additions & 0 deletions demo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Ray.Compiler Demo

This demo shows how Ray.Compiler pre-compiles dependency injection bindings into executable PHP scripts.

## Architecture

Ray.Compiler uses a **two-phase architecture**:

### Compile-time (Development/Build)
- Uses Ray.Di's full dependency resolution
- Analyzes bindings, constructs dependency graph
- Generates optimized PHP scripts

### Runtime (Production)
- Uses minimal `CompiledInjector`
- Executes pre-compiled scripts
- **No reflection, no dependency resolution**

## Files

- `GreeterInterface.php` - Service interface
- `Greeter.php` - Service implementation
- `AppModule.php` - Dependency injection bindings
- `compile.php` - Compilation script (run at build time)
- `run.php` - Execution script (uses compiled scripts)

## Usage

### Step 1: Compile (Build Time)

```bash
php demo/compile.php
```

This generates optimized PHP scripts in `demo/.compiled/`:
- `Ray_Compiler_Demo_GreeterInterface-.php` (`_` replaces namespace `\`, trailing `-` indicates unnamed binding)
- Other dependency scripts

### Step 2: Run (Runtime)

```bash
php demo/run.php
```

Output:
```
Hello, World!
Hello, Ray.Compiler!

✓ Using pre-compiled dependency injection!
✓ No reflection, no dependency resolution at runtime
✓ Check demo/.compiled/ for generated PHP scripts
```

## Inspecting Generated Code

After compilation, check the generated scripts:

```bash
cat demo/.compiled/Ray_Compiler_Demo_GreeterInterface-.php
```

You'll see plain PHP code with direct instantiation (`new Greeter()`) and simple return statements - no reflection, no dependency resolution logic!

## Benefits

1. **Performance**: No runtime reflection or dependency resolution
2. **Zero Overhead**: Compiled scripts are simple `new` and `return` statements
3. **Debuggable**: Generated code is readable PHP
4. **Production Ready**: No heavy DI framework in production

## How It Works

```
┌─────────────────┐ ┌──────────────┐
│ Ray.Di Module │────────▶│ Compiler │
│ (Bindings) │ │ │
└─────────────────┘ └──────┬───────┘
┌──────────────┐
│ PHP Scripts │
│ (Optimized) │
└──────┬───────┘
┌──────────────────┐
│ CompiledInjector │
│ (Lightweight) │
└──────────────────┘
```

## Next Steps

- Add more complex dependencies (interfaces, providers, AOP)
- Try scopes (singleton vs prototype)
- Explore assisted injection
- Use with your real application modules
18 changes: 18 additions & 0 deletions demo/compile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

declare(strict_types=1);

use Ray\Compiler\Compiler;
use Ray\Compiler\Demo\AppModule;

require dirname(__DIR__) . '/vendor/autoload.php';

$scriptDir = __DIR__ . '/.compiled';
$module = new AppModule();

// Compile the dependency injection container
$compiler = new Compiler();
$compiler->compile($module, $scriptDir);
Comment thread
koriym marked this conversation as resolved.

echo "✓ Compilation complete! Scripts saved to: {$scriptDir}\n";
echo "✓ Run 'php demo/run.php' to execute the compiled application\n";
24 changes: 24 additions & 0 deletions demo/run.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

use Ray\Compiler\CompiledInjector;
use Ray\Compiler\Demo\GreeterInterface;

require dirname(__DIR__) . '/vendor/autoload.php';

$scriptDir = __DIR__ . '/.compiled';

// Use the compiled injector (no Ray.Di runtime overhead)
$injector = new CompiledInjector($scriptDir);

/** @var GreeterInterface $greeter */
Comment on lines +10 to +15
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

suggestion: Consider handling injector instantiation errors for robustness.

If CompiledInjector fails due to missing or corrupted scripts, exceptions may occur. Adding validation or error handling here will make the code more robust.

Suggested change
$scriptDir = __DIR__ . '/.compiled';
// Use the compiled injector (no Ray.Di runtime overhead)
$injector = new CompiledInjector($scriptDir);
/** @var GreeterInterface $greeter */
$scriptDir = __DIR__ . '/.compiled';
// Use the compiled injector (no Ray.Di runtime overhead) with error handling
try {
$injector = new CompiledInjector($scriptDir);
} catch (\Throwable $e) {
fwrite(STDERR, "Failed to instantiate CompiledInjector: " . $e->getMessage() . "\n");
exit(1);
}
/** @var GreeterInterface $greeter */

$greeter = $injector->getInstance(GreeterInterface::class);

echo $greeter->greet('World') . "\n";
echo $greeter->greet('Ray.Compiler') . "\n";

echo "\n";
echo "✓ Using pre-compiled dependency injection!\n";
echo "✓ No reflection, no dependency resolution at runtime\n";
echo "✓ Check {$scriptDir}/ for generated PHP scripts\n";
Loading