1 | <?php |
||
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) |
||
76 | |||
77 | public function offsetExists($serviceId) |
||
81 | |||
82 | public function offsetUnset($serviceId) |
||
86 | |||
87 | public function offsetSet($serviceId, $value) |
||
91 | |||
92 | public function __call($name, $arguments) |
||
93 | { |
||
94 | call_user_func_array([$this->pimpleContainer, $name], $arguments); |
||
95 | } |
||
96 | } |
||
97 |