-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Injector::implementations() to select implementations for a given…
… type
- Loading branch information
Showing
5 changed files
with
115 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php namespace inject; | ||
|
||
use lang\Generic; | ||
|
||
/** @test inject.unittest.ImplementationsTest */ | ||
#[Generic(self: 'T')] | ||
class Implementations { | ||
private $inject, $bindings; | ||
|
||
/** | ||
* Creates a new instance | ||
* | ||
* @param inject.Injector $inject | ||
* @param [:inject.Binding] $bindings | ||
*/ | ||
public function __construct(Injector $inject, array $bindings) { | ||
$this->inject= $inject; | ||
$this->bindings= $bindings; | ||
} | ||
|
||
/** | ||
* Returns the default implementation | ||
* | ||
* @return T | ||
*/ | ||
#[Generic(return: 'T')] | ||
public function default() { | ||
return current($this->bindings)->resolve($this->inject); | ||
} | ||
|
||
/** | ||
* Returns the implementation for a given name | ||
* | ||
* @param string $name | ||
* @return T | ||
* @throws inject.ProvisionException if there is no such implementation | ||
*/ | ||
#[Generic(return: 'T')] | ||
public function named($name) { | ||
if ($binding= $this->bindings[$name] ?? null) { | ||
return $binding->resolve($this->inject); | ||
} | ||
|
||
throw new ProvisionException('No implementation named "'.$name.'"'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
src/test/php/inject/unittest/ImplementationsTest.class.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php namespace inject\unittest; | ||
|
||
use inject\unittest\fixture\{URI, Endpoint}; | ||
use inject\{Injector, ProvisionException}; | ||
use test\{Assert, Before, Expect, Test, Values}; | ||
|
||
class ImplementationsTest { | ||
private $uris; | ||
|
||
#[Before] | ||
private function uris() { | ||
$this->uris= [ | ||
'dev' => new URI('http://localhost'), | ||
'prod' => new URI('https://example.com'), | ||
]; | ||
} | ||
|
||
/** @return inject.Injector */ | ||
private function fixture() { | ||
$fixture= new Injector(); | ||
foreach ($this->uris as $name => $uri) { | ||
$fixture->bind(URI::class, $uri, $name); | ||
} | ||
return $fixture; | ||
} | ||
|
||
#[Test, Values(['dev', 'prod'])] | ||
public function implementations_named($name) { | ||
Assert::equals($this->uris[$name], $this->fixture()->implementations(URI::class)->named($name)); | ||
} | ||
|
||
#[Test] | ||
public function default_implementation() { | ||
Assert::equals($this->uris['dev'], $this->fixture()->implementations(URI::class)->default()); | ||
} | ||
|
||
#[Test, Expect(ProvisionException::class)] | ||
public function no_implementations() { | ||
$this->fixture()->implementations(Endpoint::class); | ||
} | ||
|
||
#[Test, Expect(ProvisionException::class)] | ||
public function unknown_implementation() { | ||
$this->fixture()->implementations(URI::class)->named('stage'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,10 @@ | ||
<?php namespace inject\unittest\fixture; | ||
|
||
class URI { | ||
private $backing; | ||
|
||
/** @param string|inject.unittest.fixture.Creation $arg */ | ||
public function __construct($arg) { /* ... */ } | ||
public function __construct($arg) { | ||
$this->backing= $arg instanceof Creation ? $arg->create() : (string)$arg; | ||
} | ||
} |