-
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
1 parent
fbdc967
commit 9004392
Showing
4 changed files
with
160 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace BrenoRoosevelt\Psr11; | ||
|
||
use Exception; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class CompositeContainer implements ContainerInterface | ||
{ | ||
/** @var ContainerInterface[] */ | ||
private array $containers; | ||
|
||
public function __construct(ContainerInterface ...$containers) | ||
{ | ||
$this->containers = $containers; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function get(string $id) | ||
{ | ||
foreach ($this->containers as $container) { | ||
if ($container->has($id)) { | ||
return $container->get($id); | ||
} | ||
} | ||
|
||
$message = sprintf('Composite Container Exception: no entry was found for %s', $id); | ||
throw new class($message) extends Exception implements NotFoundExceptionInterface { | ||
}; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function has(string $id): bool | ||
{ | ||
foreach ($this->containers as $container) { | ||
if ($container->has($id)) { | ||
return true; | ||
} | ||
} | ||
|
||
return false; | ||
} | ||
} |
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,39 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace BrenoRoosevelt\Psr11; | ||
|
||
use Exception; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class StaticContainer implements ContainerInterface | ||
{ | ||
private array $services; | ||
|
||
/** | ||
* @param array<string, mixed> $services | ||
*/ | ||
public function __construct(array $services = []) | ||
{ | ||
$this->services = $services; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function get(string $id) | ||
{ | ||
if (! array_key_exists($id, $this->services)) { | ||
$message = sprintf('Static Container Exception: no entry was found for %s', $id); | ||
throw new class($message) extends Exception implements NotFoundExceptionInterface { | ||
}; | ||
} | ||
|
||
return $this->services[$id]; | ||
} | ||
|
||
/** @inheritDoc */ | ||
public function has(string $id): bool | ||
{ | ||
return array_key_exists($id, $this->services); | ||
} | ||
} |
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 | ||
declare(strict_types=1); | ||
|
||
namespace BrenoRoosevelt\Psr11\Tests; | ||
|
||
use BrenoRoosevelt\Psr11\CompositeContainer; | ||
use BrenoRoosevelt\Psr11\StaticContainer; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class CompositeContainerTest extends TestCase | ||
{ | ||
public function testGet(): void | ||
{ | ||
$container = new CompositeContainer( | ||
new StaticContainer(['id1' => 'value1']), | ||
new StaticContainer(['id2' => 'value2']), | ||
); | ||
|
||
$this->assertEquals('value1', $container->get('id1')); | ||
$this->assertEquals('value2', $container->get('id2')); | ||
} | ||
|
||
public function testGetException(): void | ||
{ | ||
$container = new CompositeContainer( | ||
new StaticContainer(['id1' => 'value1']), | ||
new StaticContainer(['id2' => 'value2']), | ||
); | ||
|
||
$this->expectException(NotFoundExceptionInterface::class); | ||
$container->get('invalid'); | ||
} | ||
|
||
public function testHas(): void | ||
{ | ||
$container = new CompositeContainer( | ||
new StaticContainer(['id1' => 'value1']), | ||
new StaticContainer(['id2' => 'value2']), | ||
); | ||
|
||
$this->assertTrue($container->has('id1')); | ||
$this->assertTrue($container->has('id2')); | ||
$this->assertFalse($container->has('id3')); | ||
} | ||
} |
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,30 @@ | ||
<?php | ||
declare(strict_types=1); | ||
|
||
namespace BrenoRoosevelt\Psr11\Tests; | ||
|
||
use BrenoRoosevelt\Psr11\StaticContainer; | ||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class StaticContainerTest extends TestCase | ||
{ | ||
public function testGet(): void | ||
{ | ||
$container = new StaticContainer(['id' => 'value']); | ||
$this->assertEquals('value', $container->get('id')); | ||
} | ||
|
||
public function testGetException(): void | ||
{ | ||
$this->expectException(NotFoundExceptionInterface::class); | ||
(new StaticContainer)->get('invalid'); | ||
} | ||
|
||
public function testHas(): void | ||
{ | ||
$container = new StaticContainer(['id' => 'value']); | ||
$this->assertTrue($container->has('id')); | ||
$this->assertFalse($container->has('invalid')); | ||
} | ||
} |