1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Fundic\Decorator; |
6
|
|
|
|
7
|
|
|
use Fundic\ContainerInterface; |
8
|
|
|
use Fundic\DataStructure\Result\Exception; |
9
|
|
|
use Fundic\DataStructure\Result\Just; |
10
|
|
|
use Fundic\DataStructure\Result\NotFound; |
11
|
|
|
use Fundic\Decorator\Exception\ContainerException; |
12
|
|
|
use Fundic\Decorator\Exception\NotFoundException; |
13
|
|
|
use Fundic\Factory\ValueFactory; |
14
|
|
|
use Psr\Container\ContainerExceptionInterface; |
15
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
16
|
|
|
|
17
|
|
|
final class ExceptionContainer implements ContainerInterface |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var ContainerInterface |
21
|
|
|
*/ |
22
|
|
|
private $container; |
23
|
|
|
|
24
|
|
|
public function __construct(ContainerInterface $container) |
25
|
|
|
{ |
26
|
|
|
$this->container = $container; |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public static function create(ContainerInterface $container): self |
30
|
|
|
{ |
31
|
|
|
return new self($container); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Finds an entry of the container by its identifier and returns it. |
36
|
|
|
* |
37
|
|
|
* @param string $id Identifier of the entry to look for. |
38
|
|
|
* |
39
|
|
|
* @throws NotFoundExceptionInterface No entry was found for **this** identifier. |
40
|
|
|
* @throws ContainerExceptionInterface Error while retrieving the entry. |
41
|
|
|
* |
42
|
|
|
* @return mixed Entry. |
43
|
|
|
*/ |
44
|
|
|
public function get($id) |
45
|
|
|
{ |
46
|
|
|
$result = $this->container->get($id); |
47
|
|
|
|
48
|
|
|
switch ($result) { |
49
|
|
|
case ($result instanceof Just): |
50
|
|
|
/** @var Just $result */ |
51
|
|
|
return $result->get(); |
52
|
|
|
case ($result instanceof NotFound): |
53
|
|
|
throw NotFoundException::forKey($id); |
54
|
|
|
case ($result instanceof Exception): |
55
|
|
|
/** @var Exception $result */ |
56
|
|
|
throw ContainerException::forKeyWithInner($id, $result->inner()); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* {@inheritdoc} |
62
|
|
|
*/ |
63
|
|
|
public function has($id) : bool |
64
|
|
|
{ |
65
|
|
|
return $this->container->has($id); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
public function add(string $id, ValueFactory $factory): ContainerInterface |
69
|
|
|
{ |
70
|
|
|
return new self($this->container->add($id, $factory)); |
71
|
|
|
} |
72
|
|
|
} |
73
|
|
|
|