Skip to content

Commit

Permalink
[Container] New Component (#182)
Browse files Browse the repository at this point in the history
## Description

Simple light weight container

## Checklist
- [x] Updated CHANGELOG files
- [x] Updated Documentation
- [x] Unit Tests Created
- [x] php-cs-fixer
  • Loading branch information
JoshuaEstes authored Dec 6, 2023
1 parent 7757151 commit 4842439
Show file tree
Hide file tree
Showing 20 changed files with 341 additions and 6 deletions.
1 change: 1 addition & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ docs/ @JoshuaEstes
# Each component/contract needs a Team
/src/SonsOfPHP/**/Cache @JoshuaEstes
/src/SonsOfPHP/**/Clock @JoshuaEstes
/src/SonsOfPHP/**/Container @JoshuaEstes
/src/SonsOfPHP/**/Cookie @JoshuaEstes
/src/SonsOfPHP/**/Common @JoshuaEstes
/src/SonsOfPHP/**/Cqrs @JoshuaEstes
Expand Down
4 changes: 4 additions & 0 deletions .github/labeler.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ Clock:
- docs/components/clock/*
- src/SonsOfPHP/**/Clock/*

Container:
- docs/components/container/*
- src/SonsOfPHP/**/Container/*

Cookie:
- docs/components/cookie/*
- src/SonsOfPHP/**/Cookie/*
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ To get the diff between two versions, go to https://github.com/SonsOfPHP/sonsofp
* [PR #170](https://github.com/SonsOfPHP/sonsofphp/pull/170) [Link] New Component (PSR-13)
* [PR #173](https://github.com/SonsOfPHP/sonsofphp/pull/173) [Money] Twig Bridge
* [PR #181](https://github.com/SonsOfPHP/sonsofphp/pull/181) [Cookie] New Component and Contract
* [PR #182](https://github.com/SonsOfPHP/sonsofphp/pull/182) [Container] New Component (PSR-11)

## [0.3.8]

Expand Down
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ test-cache: phpunit
test-clock: PHPUNIT_TESTSUITE=clock
test-clock: phpunit

test-container: PHPUNIT_TESTSUITE=container
test-container: phpunit

test-cookie: PHPUNIT_TESTSUITE=cookie
test-cookie: phpunit

Expand Down Expand Up @@ -105,6 +108,9 @@ coverage-cache: coverage
coverage-clock: PHPUNIT_TESTSUITE=clock
coverage-clock: coverage

coverage-container: PHPUNIT_TESTSUITE=container
coverage-container: coverage

coverage-cookie: PHPUNIT_TESTSUITE=cookie
coverage-cookie: coverage

Expand Down
4 changes: 4 additions & 0 deletions bard.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
"path": "src/SonsOfPHP/Component/Clock",
"repository": "git@github.com:SonsOfPHP/clock.git"
},
{
"path": "src/SonsOfPHP/Component/Container",
"repository": "git@github.com:SonsOfPHP/container.git"
},
{
"path": "src/SonsOfPHP/Component/Cookie",
"repository": "git@github.com:SonsOfPHP/cookie.git"
Expand Down
8 changes: 6 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,8 @@
"ext-intl": "*",
"doctrine/collections": "^2",
"doctrine/orm": "^2",
"sonsofphp/cookie-contract": "0.3.x-dev"
"sonsofphp/cookie-contract": "0.3.x-dev",
"psr/container": "^1.0 || ^2.0"
},
"replace": {
"sonsofphp/bard": "self.version",
Expand Down Expand Up @@ -114,13 +115,15 @@
"sonsofphp/pager-doctrine-dbal": "self.version",
"sonsofphp/pager-doctrine-orm": "self.version",
"sonsofphp/cookie": "self.version",
"sonsofphp/cookie-contract": "self.version"
"sonsofphp/cookie-contract": "self.version",
"sonsofphp/container": "self.version"
},
"autoload": {
"psr-4": {
"SonsOfPHP\\Bard\\": "src/SonsOfPHP/Bard/src",
"SonsOfPHP\\Component\\Cache\\": "src/SonsOfPHP/Component/Cache",
"SonsOfPHP\\Component\\Clock\\": "src/SonsOfPHP/Component/Clock",
"SonsOfPHP\\Component\\Container\\": "src/SonsOfPHP/Component/Container",
"SonsOfPHP\\Component\\Cookie\\": "src/SonsOfPHP/Component/Cookie",
"SonsOfPHP\\Component\\Cqrs\\": "src/SonsOfPHP/Component/Cqrs",
"SonsOfPHP\\Bundle\\Cqrs\\": "src/SonsOfPHP/Bundle/Cqrs",
Expand Down Expand Up @@ -158,6 +161,7 @@
"src/SonsOfPHP/Bard/Tests",
"src/SonsOfPHP/Component/Cache/Tests",
"src/SonsOfPHP/Component/Clock/Tests",
"src/SonsOfPHP/Component/Container/Tests",
"src/SonsOfPHP/Component/Cookie/Tests",
"src/SonsOfPHP/Component/Cqrs/Tests",
"src/SonsOfPHP/Bundle/Cqrs/Tests",
Expand Down
34 changes: 34 additions & 0 deletions docs/components/container/index.md
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');
```
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ nav:
- components/index.md
- Cache: components/cache/index.md
- Clock: components/clock/index.md
- Container: components/container/index.md
- Cookie: components/cookie/index.md
- CQRS:
- components/cqrs/index.md
Expand Down
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@
<directory>src/SonsOfPHP/Component/Clock/Tests</directory>
</testsuite>

<testsuite name="container">
<directory>src/SonsOfPHP/Component/Container/Tests</directory>
</testsuite>

<testsuite name="cookie">
<directory>src/SonsOfPHP/Component/Cookie/Tests</directory>
</testsuite>
Expand Down
4 changes: 4 additions & 0 deletions src/SonsOfPHP/Component/Container/.gitattributes
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
3 changes: 3 additions & 0 deletions src/SonsOfPHP/Component/Container/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
composer.lock
phpunit.xml
vendor/
60 changes: 60 additions & 0 deletions src/SonsOfPHP/Component/Container/Container.php
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;
}
}
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 {}
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 {}
19 changes: 19 additions & 0 deletions src/SonsOfPHP/Component/Container/LICENSE
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.
16 changes: 16 additions & 0 deletions src/SonsOfPHP/Component/Container/README.md
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 src/SonsOfPHP/Component/Container/Tests/ContainerTest.php
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');
}
}
Loading

0 comments on commit 4842439

Please sign in to comment.