diff --git a/.gitignore b/.gitignore index c84ab0c..0b348e1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ +/build/ /vendor/ /composer.lock /.phpunit.result.cache diff --git a/README.md b/README.md index 3d4794e..d107acb 100644 --- a/README.md +++ b/README.md @@ -7,11 +7,9 @@ use Acme\BarMiddleware; use Acme\BuzRequestHandler; use Acme\FooMiddleware; use K9u\Framework\ApplicationInterface; +use K9u\Framework\CachedInjectorFactory; use K9u\Framework\FrameworkModule; use Laminas\Diactoros\ServerRequestFactory; -use Ray\Compiler\ScriptInjector; -use Ray\Di\Bind; -use Ray\Di\InjectorInterface; $module = new FrameworkModule([ FooMiddleware::class, @@ -19,13 +17,10 @@ $module = new FrameworkModule([ BuzRequestHandler::class, ]); -$injector = new ScriptInjector('/path/to/cache', function () use (&$injector, $module) { - (new Bind($module->getContainer(), InjectorInterface::class))->toInstance($injector); - return $module; -}); +$injector = (new CachedInjectorFactory('/path/to/cache'))($module); $app = $injector->getInstance(ApplicationInterface::class); -assert($app instanceof ApplicationInterface); +/* @var ApplicationInterface $app */ $request = ServerRequestFactory::fromGlobals(); diff --git a/src/CachedInjectorFactory.php b/src/CachedInjectorFactory.php new file mode 100644 index 0000000..ff2b5d3 --- /dev/null +++ b/src/CachedInjectorFactory.php @@ -0,0 +1,30 @@ +cacheDir = $cacheDir; + } + + public function __invoke(AbstractModule $module): InjectorInterface + { + $injector = new ScriptInjector($this->cacheDir, function () use (&$injector, $module) { + (new Bind($module->getContainer(), InjectorInterface::class))->toInstance($injector); + return $module; + }); + + return $injector; + } +} diff --git a/src/InjectorFactoryInterface.php b/src/InjectorFactoryInterface.php new file mode 100644 index 0000000..ade8a99 --- /dev/null +++ b/src/InjectorFactoryInterface.php @@ -0,0 +1,13 @@ +getInstance(ApplicationInterface::class); + $compiler = new DiCompiler($module, __DIR__ . '/../build/tests'); + $instance = $compiler->getInstance(ApplicationInterface::class); $this->assertInstanceOf(ApplicationInterface::class, $instance); + +// $compiler->dumpGraph(); } }