ray/compiler

A dependency injection compiler for Ray.Di

1.10.6 2024-10-21 02:08 UTC

README

Dependency Injection Compiler

Scrutinizer Code Quality codecov Type Coverage Continuous Integration

Ray.Compiler compiles Ray.Di bindings into PHP code, providing a performance boost that makes Dependency Injection couldn't be any faster.

Script Injector

ScriptInjector has the same interface as Ray.Di Injector; whereas Ray.Di Injector resolves dependencies based on memory bindings, ScriptInjector executes pre-compiled PHP code and is faster.

Ray.Di injector

$injector = new Injector(new CarModule); // Ray.Di injector

Ray.Compiler injector

$injector = new ScriptInjector($tmpDir, fn => new CarModule);

Precompile

You will want to compile all dependencies into code before deploying the production. The DiCompiler will compile all bindings into PHP code.

$compiler = new DiCompiler(new CarModule, $tmpDir);
$compiler->compile();

Object graph visualization

Object graph can be visualized with dumpGraph(). Graph HTML files will be output at graph folder under $tmpDir.

$compiler = new DiCompiler(new Module, $tmpDir);
$compiler->compile();
$compiler->dumpGraph();
open tmp/graph/Ray_Compiler_FakeCarInterface-.html

CompileInjector

The CompileInjector gives you the best performance in both development (x2) and production (x10) by switching two injector.

Get the injector by specifying the binding and cache, depending on the execution context of the application.

$injector = new CompileInjector($tmpDir, $injectorContext);

$injectorContext example:

The __invoke() method prepares the modules needed in that context. The getCache() method specifies the cache of the injector itself.

Install DiCompileModule in the context for production. The injector is more optimized and dependency errors are reported at compile-time instead of run-time.