Skip to content

Commit

Permalink
Merge pull request #102 from NaokiTsuchiya/feat/override-instance
Browse files Browse the repository at this point in the history
Add getOverrideInstance method
  • Loading branch information
koriym authored Mar 17, 2023
2 parents 92ff536 + c22a77d commit 0197688
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 1 deletion.
19 changes: 18 additions & 1 deletion src/CachedInjectorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,25 @@ public static function getInstance(string $injectorId, string $scriptDir, callab
* @param callable(): AbstractModule $modules
* @param array<class-string> $savedSingletons
*/
private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons): InjectorInterface
public static function getOverrideInstance(
string $scriptDir,
callable $modules,
AbstractModule $overrideModule,
array $savedSingletons = []
): InjectorInterface {
return self::getInjector($modules, $scriptDir, $savedSingletons, $overrideModule);
}

/**
* @param callable(): AbstractModule $modules
* @param array<class-string> $savedSingletons
*/
private static function getInjector(callable $modules, string $scriptDir, array $savedSingletons, ?AbstractModule $module = null): InjectorInterface
{
if ($module !== null) {
$modules = new OverrideLazyModule($modules, $module);
}

$injector = InjectorFactory::getInstance($modules, $scriptDir);
foreach ($savedSingletons as $singleton) {
$injector->getInstance($singleton);
Expand Down
13 changes: 13 additions & 0 deletions src/ContextInjector.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Ray\Compiler;

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

use function get_class;
Expand All @@ -27,4 +28,16 @@ public static function getInstance(AbstractInjectorContext $injectorContext): In
$injectorContext->getSavedSingleton()
);
}

public static function getOverrideInstance(
AbstractInjectorContext $injectorContext,
AbstractModule $overrideModule
): InjectorInterface {
return CachedInjectorFactory::getOverrideInstance(
$injectorContext->tmpDir,
$injectorContext,
$overrideModule,
$injectorContext->getSavedSingleton()
);
}
}
31 changes: 31 additions & 0 deletions src/OverrideLazyModule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace Ray\Compiler;

use Ray\Di\AbstractModule;

class OverrideLazyModule implements LazyModuleInterface
{
/** @var callable(): AbstractModule */
private $modules;

/** @var AbstractModule */
private $overrideModule;

/** @param callable(): AbstractModule $modules */
public function __construct(callable $modules, AbstractModule $overrideModule)
{
$this->modules = $modules;
$this->overrideModule = $overrideModule;
}

public function __invoke(): AbstractModule
{
$module = ($this->modules)();
$module->override($this->overrideModule);

return $module;
}
}
16 changes: 16 additions & 0 deletions tests/ContextInjectorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use Ray\Compiler\Deep\FakeDemand;
use Ray\Compiler\Deep\FakeInjectorContext;
use Ray\Compiler\Deep\FakeScriptInjectorContext;
use Ray\Di\AbstractModule;
use Ray\Di\Injector;
use Ray\Di\InjectorInterface;

Expand Down Expand Up @@ -64,4 +65,19 @@ public function testContainerIsResetWhenTheInjectorIsRetrieved(AbstractInjectorC
$demand = $injector->getInstance(FakeDemand::class);
$this->assertInstanceOf(FakeDemand::class, $demand);
}

public function testGetOverrideInstance(): void
{
$overrideModule = new class extends AbstractModule {
protected function configure()
{
$this->bind(FakeRobotInterface::class)->to(FakeDevRobot::class);
}
};

$injector = ContextInjector::getOverrideInstance(new FakeTestContext(__DIR__ . '/tmp/base'), $overrideModule);

$this->assertInstanceOf(Injector::class, $injector);
$this->assertInstanceOf(FakeDevRobot::class, $injector->getInstance(FakeRobotInterface::class));
}
}

0 comments on commit 0197688

Please sign in to comment.