-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
## Description Simple light weight container ## Checklist - [x] Updated CHANGELOG files - [x] Updated Documentation - [x] Unit Tests Created - [x] php-cs-fixer
- Loading branch information
1 parent
7757151
commit 4842439
Showing
20 changed files
with
341 additions
and
6 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
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
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,34 @@ | ||
--- | ||
title: Container | ||
--- | ||
|
||
This is a very simple lightweight PSR-11 Container implementation. | ||
|
||
## Installation | ||
|
||
```shell | ||
composer require sonsofphp/container | ||
``` | ||
|
||
## Usage | ||
|
||
```php | ||
<?php | ||
|
||
use SonsOfPHP\Component\Container\Container; | ||
use Psr\Container\ContainerInterface; | ||
|
||
$container = new Container(); | ||
$container->set('service.id.one', function (ContainerInterface $container) { | ||
return new Service(); | ||
}); | ||
$container->set('service.id.two', function (ContainerInterface $container) { | ||
return new Service($container->get('service.id.one')); | ||
}); | ||
|
||
// Services will not be created until they are called, once called, they will | ||
// always return the same instance of the service. That means that in the | ||
// following code, the "service.id.two" is only constructed once. | ||
$service = $container->get('service.id.two'); | ||
$service2 = $container->get('service.id.two'); | ||
``` |
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,4 @@ | ||
/Tests export-ignore | ||
/phpunit.xml.dist export-ignore | ||
/.gitattributes export-ignore | ||
/.gitignore export-ignore |
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,3 @@ | ||
composer.lock | ||
phpunit.xml | ||
vendor/ |
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,60 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Container; | ||
|
||
use Psr\Container\ContainerInterface; | ||
use SonsOfPHP\Component\Container\Exception\ContainerException; | ||
use SonsOfPHP\Component\Container\Exception\NotFoundException; | ||
|
||
/** | ||
* Usage: | ||
* $container = new Container(); | ||
* $container->set('service.id', function (ContainerInterface $container) { | ||
* return new Service($container->get('another.service_id')); | ||
* }); | ||
*/ | ||
class Container implements ContainerInterface | ||
{ | ||
private array $services = []; | ||
private array $cachedServices = []; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function get(string $id) | ||
{ | ||
if (false === $this->has($id)) { | ||
throw new NotFoundException(sprintf('Service "%s" not found.', $id)); | ||
} | ||
|
||
if (!array_key_exists($id, $this->cachedServices)) { | ||
$this->cachedServices[$id] = call_user_func($this->services[$id], $this); | ||
} | ||
|
||
return $this->cachedServices[$id]; | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function has(string $id): bool | ||
{ | ||
return array_key_exists($id, $this->services); | ||
} | ||
|
||
/** | ||
* @param callable $callable | ||
*/ | ||
public function set(string $id, $callable): self | ||
{ | ||
if (!is_callable($callable)) { | ||
throw new ContainerException('MUST pass in a callable'); | ||
} | ||
|
||
$this->services[$id] = $callable; | ||
|
||
return $this; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/SonsOfPHP/Component/Container/Exception/ContainerException.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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Container\Exception; | ||
|
||
use Psr\Container\ContainerExceptionInterface; | ||
|
||
class ContainerException extends \Exception implements ContainerExceptionInterface {} |
9 changes: 9 additions & 0 deletions
9
src/SonsOfPHP/Component/Container/Exception/NotFoundException.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,9 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Container\Exception; | ||
|
||
use Psr\Container\NotFoundExceptionInterface; | ||
|
||
class NotFoundException extends ContainerException implements NotFoundExceptionInterface {} |
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,19 @@ | ||
Copyright 2022 to Present Joshua Estes | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy of | ||
this software and associated documentation files (the "Software"), to deal in | ||
the Software without restriction, including without limitation the rights to | ||
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies | ||
of the Software, and to permit persons to whom the Software is furnished to do | ||
so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. |
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,16 @@ | ||
Sons of PHP - Cookie | ||
==================== | ||
|
||
## Learn More | ||
|
||
* [Documentation][docs] | ||
* [Contributing][contributing] | ||
* [Report Issues][issues] and [Submit Pull Requests][pull-requests] in the [Mother Repository][mother-repo] | ||
* Get Help & Support using [Discussions][discussions] | ||
|
||
[discussions]: https://github.com/orgs/SonsOfPHP/discussions | ||
[mother-repo]: https://github.com/SonsOfPHP/sonsofphp | ||
[contributing]: https://docs.sonsofphp.com/contributing/ | ||
[docs]: https://docs.sonsofphp.com/components/cookie/ | ||
[issues]: https://github.com/SonsOfPHP/sonsofphp/issues?q=is%3Aopen+is%3Aissue+label%3ACookie | ||
[pull-requests]: https://github.com/SonsOfPHP/sonsofphp/pulls?q=is%3Aopen+is%3Apr+label%3ACookie |
100 changes: 100 additions & 0 deletions
100
src/SonsOfPHP/Component/Container/Tests/ContainerTest.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,100 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SonsOfPHP\Component\Container\Tests; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use Psr\Container\ContainerExceptionInterface; | ||
use Psr\Container\ContainerInterface; | ||
use Psr\Container\NotFoundExceptionInterface; | ||
use SonsOfPHP\Component\Container\Container; | ||
|
||
/** | ||
* @coversDefaultClass \SonsOfPHP\Component\Container\Container | ||
* | ||
* @uses \SonsOfPHP\Component\Container\Container | ||
*/ | ||
final class ContainerTest extends TestCase | ||
{ | ||
/** | ||
* @coversNothing | ||
*/ | ||
public function testItHasTheCorrectInterface(): void | ||
{ | ||
$container = new Container(); | ||
|
||
$this->assertInstanceOf(ContainerInterface::class, $container); | ||
} | ||
|
||
/** | ||
* @covers ::has | ||
*/ | ||
public function testhHas(): void | ||
{ | ||
$container = new Container(); | ||
|
||
$this->assertFalse($container->has('service.id')); | ||
$container->set('service.id', function (): void {}); | ||
$this->assertTrue($container->has('service.id')); | ||
} | ||
|
||
/** | ||
* @covers ::get | ||
*/ | ||
public function testGetWhenServiceNotFound(): void | ||
{ | ||
$container = new Container(); | ||
|
||
$this->expectException(NotFoundExceptionInterface::class); | ||
$container->get('nope'); | ||
} | ||
|
||
/** | ||
* @covers ::get | ||
*/ | ||
public function testGetAlwaysReturnSameInstance(): void | ||
{ | ||
$container = new Container(); | ||
$container->set('service.id', fn() => new \stdClass()); | ||
$service = $container->get('service.id'); | ||
$this->assertSame($service, $container->get('service.id')); | ||
} | ||
|
||
/** | ||
* @covers ::get | ||
*/ | ||
public function testGetWillCacheService(): void | ||
{ | ||
$container = new Container(); | ||
$cached = new \ReflectionProperty($container, 'cachedServices'); | ||
|
||
$container->set('service.id', fn() => new \stdClass()); | ||
$service = $container->get('service.id'); | ||
$this->assertCount(1, $cached->getValue($container)); | ||
} | ||
|
||
/** | ||
* @covers ::set | ||
*/ | ||
public function testSet(): void | ||
{ | ||
$container = new Container(); | ||
$services = new \ReflectionProperty($container, 'services'); | ||
$this->assertCount(0, $services->getValue($container)); | ||
|
||
$container->set('service.id', function (): void {}); | ||
$this->assertCount(1, $services->getValue($container)); | ||
} | ||
|
||
/** | ||
* @covers ::set | ||
*/ | ||
public function testSetWhenNotCallable(): void | ||
{ | ||
$container = new Container(); | ||
|
||
$this->expectException(ContainerExceptionInterface::class); | ||
$container->set('service.id', 'this is not callable'); | ||
} | ||
} |
Oops, something went wrong.