Skip to content

Commit 458b160

Browse files
committed
Implement NullContainer
1 parent 2f5ef09 commit 458b160

File tree

4 files changed

+91
-0
lines changed

4 files changed

+91
-0
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/vendor/
2+
composer.lock

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "technically-php/null-container",
3+
"description": "Null (always empty) PSR-11 container implementation",
4+
"type": "library",
5+
"require": {
6+
"php": ">=7.0",
7+
"psr/container": "^1.0"
8+
},
9+
"license": "MIT",
10+
"authors": [
11+
{
12+
"name": "Ivan Voskoboinyk",
13+
"email": "ivan@voskoboinyk.com"
14+
}
15+
],
16+
"minimum-stability": "stable",
17+
"autoload": {
18+
"psr-4": {
19+
"Technically\\NullContainer\\": "src"
20+
}
21+
}
22+
}

src/Exceptions/ServiceNotFound.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Technically\NullContainer\Exceptions;
4+
5+
use Psr\Container\NotFoundExceptionInterface;
6+
use RuntimeException;
7+
8+
final class ServiceNotFound extends RuntimeException implements NotFoundExceptionInterface
9+
{
10+
/**
11+
* @var string
12+
*/
13+
private $serviceName;
14+
15+
/**
16+
* @param string $serviceName
17+
*/
18+
public function __construct(string $serviceName)
19+
{
20+
$this->serviceName = $serviceName;
21+
22+
parent::__construct("Could not resolve service (`{$serviceName}`).");
23+
}
24+
25+
/**
26+
* @return string
27+
*/
28+
public function getServiceName(): string
29+
{
30+
return $this->serviceName;
31+
}
32+
}

src/NullContainer.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace Technically\NullContainer;
4+
5+
use Psr\Container\ContainerInterface;
6+
use Technically\NullContainer\Exceptions\ServiceNotFound;
7+
8+
final class NullContainer implements ContainerInterface
9+
{
10+
/**
11+
* Get an entry of the container by its identifier.
12+
*
13+
* Always throws `ServiceNotFound` exception.
14+
*
15+
* @param string $id Identifier of the entry to look for.
16+
* @throws ServiceNotFound Always
17+
*/
18+
public function get($id)
19+
{
20+
throw new ServiceNotFound($id);
21+
}
22+
23+
/**
24+
* Check if the container can return an entry for the given identifier
25+
*
26+
* Always returns false.
27+
*
28+
* @param string $id
29+
* @return false
30+
*/
31+
public function has($id)
32+
{
33+
return false;
34+
}
35+
}

0 commit comments

Comments
 (0)