From e7be672788c909e0ef31aa3d314143e5af68b68d Mon Sep 17 00:00:00 2001 From: kawanamiyuu Date: Fri, 10 Jul 2020 21:34:40 +0900 Subject: [PATCH 1/3] add Injector Factory #4 --- src/CachedInjectorFactory.php | 30 ++++++++++++++++++++++++++++++ src/InjectorFactoryInterface.php | 13 +++++++++++++ src/OnDemandInjectorFactory.php | 17 +++++++++++++++++ 3 files changed, 60 insertions(+) create mode 100644 src/CachedInjectorFactory.php create mode 100644 src/InjectorFactoryInterface.php create mode 100644 src/OnDemandInjectorFactory.php 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 @@ + Date: Fri, 10 Jul 2020 21:36:24 +0900 Subject: [PATCH 2/3] test compile --- .gitignore | 1 + tests/FrameworkModuleTest.php | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 4 deletions(-) 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/tests/FrameworkModuleTest.php b/tests/FrameworkModuleTest.php index ee50cb1..649a481 100644 --- a/tests/FrameworkModuleTest.php +++ b/tests/FrameworkModuleTest.php @@ -5,19 +5,27 @@ namespace K9u\Framework; use PHPUnit\Framework\TestCase; -use Ray\Di\Injector; +use Ray\Compiler\DiCompiler; class FrameworkModuleTest extends TestCase { + public function setUp(): void + { + array_map('unlink', glob(__DIR__ . '/../build/tests/*')); + } + public function testCompile(): void { - $injector = new Injector(new FrameworkModule([ + $module = new FrameworkModule([ FakeMiddleware::class, FakeRequestHandler::class - ])); + ]); - $instance = $injector->getInstance(ApplicationInterface::class); + $compiler = new DiCompiler($module, __DIR__ . '/../build/tests'); + $instance = $compiler->getInstance(ApplicationInterface::class); $this->assertInstanceOf(ApplicationInterface::class, $instance); + +// $compiler->dumpGraph(); } } From d40df3e65e733f63050738bd369805d9ebb4b17c Mon Sep 17 00:00:00 2001 From: kawanamiyuu Date: Fri, 10 Jul 2020 21:43:46 +0900 Subject: [PATCH 3/3] update README --- README.md | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) 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();