1
|
|
|
<?php |
2
|
|
|
namespace SamBurns\Pimple3ContainerInterop; |
3
|
|
|
|
4
|
|
|
use Interop\Container\ContainerInterface; |
5
|
|
|
use Pimple\Container as PimpleContainer; |
6
|
|
|
use Pimple\ServiceProviderInterface; |
7
|
|
|
use SamBurns\Pimple3ContainerInterop\Exception\ContainerException; |
8
|
|
|
use SamBurns\Pimple3ContainerInterop\Exception\NotFoundException; |
9
|
|
|
use Interop\Container\Exception\ContainerException as ContainerExceptionInterface; |
10
|
|
|
use Interop\Container\Exception\NotFoundException as NotFoundExceptionInterface; |
11
|
|
|
|
12
|
|
|
class ServiceContainer implements ContainerInterface, \ArrayAccess |
13
|
|
|
{ |
14
|
|
|
/** @var PimpleContainer */ |
15
|
|
|
private $pimpleContainer; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @param PimpleContainer|null $pimpleContainer |
19
|
|
|
*/ |
20
|
|
|
public function __construct(PimpleContainer $pimpleContainer = null) |
21
|
|
|
{ |
22
|
|
|
$this->pimpleContainer = $pimpleContainer ?: new PimpleContainer(); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param ServiceProviderInterface $serviceProvider |
27
|
|
|
* @return ServiceContainer |
28
|
|
|
*/ |
29
|
|
|
public static function constructConfiguredWith(ServiceProviderInterface $serviceProvider) |
30
|
|
|
{ |
31
|
|
|
$container = new static(); |
32
|
|
|
$container->addConfig($serviceProvider); |
33
|
|
|
return $container; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param ServiceProviderInterface $pimpleServiceProvider |
38
|
|
|
*/ |
39
|
|
|
public function addConfig(ServiceProviderInterface $pimpleServiceProvider) |
40
|
|
|
{ |
41
|
|
|
$this->pimpleContainer->register($pimpleServiceProvider); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @throws NotFoundExceptionInterface |
46
|
|
|
* @throws ContainerExceptionInterface |
47
|
|
|
* |
48
|
|
|
* @param string $serviceId |
49
|
|
|
* @return mixed |
50
|
|
|
*/ |
51
|
|
|
public function get($serviceId) |
52
|
|
|
{ |
53
|
|
|
if (!$this->has($serviceId)) { |
54
|
|
|
throw NotFoundException::constructWithServiceId($serviceId); |
55
|
|
|
} |
56
|
|
|
try { |
57
|
|
|
return $this->pimpleContainer[$serviceId]; |
58
|
|
|
} catch (\Exception $exception) { |
59
|
|
|
throw new ContainerException('Pimple container exception occurred', 0, $exception); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $serviceId |
65
|
|
|
* @return bool |
66
|
|
|
*/ |
67
|
|
|
public function has($serviceId) |
68
|
|
|
{ |
69
|
|
|
return isset($this->pimpleContainer[$serviceId]); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
public function offsetGet($serviceId) |
73
|
|
|
{ |
74
|
|
|
$this->get($serviceId); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
public function offsetExists($serviceId) |
78
|
|
|
{ |
79
|
|
|
return $this->has($serviceId); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
public function offsetUnset($serviceId) |
83
|
|
|
{ |
84
|
|
|
$this->pimpleContainer->offsetUnset($serviceId); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
public function offsetSet($serviceId, $value) |
88
|
|
|
{ |
89
|
|
|
$this->pimpleContainer->offsetSet($serviceId, $value); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|