Skip to content

Commit

Permalink
Merge pull request #5 from kawanamiyuu/injector-factory
Browse files Browse the repository at this point in the history
Injector factory
  • Loading branch information
kawanamiyuu authored Jul 10, 2020
2 parents a4fc9a6 + d40df3e commit 13526f5
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 12 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/build/
/vendor/
/composer.lock
/.phpunit.result.cache
11 changes: 3 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,20 @@ 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,
BarMiddleware::class,
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();

Expand Down
30 changes: 30 additions & 0 deletions src/CachedInjectorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

declare(strict_types=1);

namespace K9u\Framework;

use Ray\Compiler\ScriptInjector;
use Ray\Di\AbstractModule;
use Ray\Di\Bind;
use Ray\Di\InjectorInterface;

class CachedInjectorFactory implements InjectorFactoryInterface
{
private string $cacheDir;

public function __construct(string $cacheDir)
{
$this->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;
}
}
13 changes: 13 additions & 0 deletions src/InjectorFactoryInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace K9u\Framework;

use Ray\Di\AbstractModule;
use Ray\Di\InjectorInterface;

interface InjectorFactoryInterface
{
public function __invoke(AbstractModule $module): InjectorInterface;
}
17 changes: 17 additions & 0 deletions src/OnDemandInjectorFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace K9u\Framework;

use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\Di\InjectorInterface;

class OnDemandInjectorFactory implements InjectorFactoryInterface
{
public function __invoke(AbstractModule $module): InjectorInterface
{
return new Injector($module);
}
}
16 changes: 12 additions & 4 deletions tests/FrameworkModuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 13526f5

Please sign in to comment.