-
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.
- Loading branch information
Showing
6 changed files
with
82 additions
and
2 deletions.
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
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 |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Twent\Framework\DI\Contracts; | ||
|
||
interface Container | ||
{ | ||
public function register(string $className, callable $definition): self; | ||
|
||
/** | ||
* @template TClassName | ||
* @param class-string<TClassName> $className | ||
* @return TClassName | ||
*/ | ||
public function get(string $className): object; | ||
} |
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Twent\Framework\DI; | ||
|
||
use Twent\Framework\DI\Contracts\Container; | ||
|
||
final class GenericContainer implements Container | ||
{ | ||
private array $definitions = []; | ||
|
||
public function register(string $className, callable $definition): Container | ||
{ | ||
$this->definitions[$className] = $definition; | ||
|
||
return $this; | ||
} | ||
|
||
/** | ||
* @inheritDoc | ||
*/ | ||
public function get(string $className): object | ||
{ | ||
$definition = $this->definitions[$className]; | ||
|
||
return $definition(); | ||
} | ||
} |
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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Twent\Framework\Feature\DI; | ||
|
||
final class Foo | ||
{ | ||
} |
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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Tests\Twent\Framework\Feature\DI; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Twent\Framework\DI\GenericContainer; | ||
|
||
final class GenericContainerTest extends TestCase | ||
{ | ||
public function testContainer(): void | ||
{ | ||
$container = new GenericContainer(); | ||
|
||
$container->register(Foo::class, static fn() => new Foo()); | ||
|
||
$foo = $container->get(Foo::class); | ||
|
||
$this->assertInstanceOf(Foo::class, $foo); | ||
} | ||
} |