-
Notifications
You must be signed in to change notification settings - Fork 4
Add demo for Ray.Compiler usage #130
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -10,3 +10,4 @@ build | |
| /coverage.xml | ||
| *.bak | ||
| .claude | ||
| /demo/.compiled | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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; | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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); | ||
|
|
||
| echo "✓ Compilation complete! Scripts saved to: {$scriptDir}\n"; | ||
| echo "✓ Run 'php demo/run.php' to execute the compiled application\n"; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||||||||||||||||||||||||||||||||
| $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"; | ||||||||||||||||||||||||||||||||||||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.