1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace Arp\Container\Adapter; |
6
|
|
|
|
7
|
|
|
use Arp\Container\Adapter\Exception\AdapterException; |
8
|
|
|
use Arp\Container\Adapter\Exception\NotFoundException; |
9
|
|
|
use Psr\Container\ContainerExceptionInterface; |
10
|
|
|
use Psr\Container\ContainerInterface; |
11
|
|
|
use Psr\Container\NotFoundExceptionInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* This class can be used base class for adapters proxying method calls to a PSR Container. |
15
|
|
|
* Extending classes must implement the remaining requirements of ContainerAdapterInterface. |
16
|
|
|
* |
17
|
|
|
* @author Alex Patterson <[email protected]> |
18
|
|
|
* @package Arp\Container\Adapter |
19
|
|
|
*/ |
20
|
|
|
abstract class AbstractPsrAdapter implements ContainerAdapterInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @var ContainerInterface |
24
|
|
|
*/ |
25
|
|
|
protected ContainerInterface $container; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @param ContainerInterface $container |
29
|
|
|
*/ |
30
|
|
|
public function __construct(ContainerInterface $container) |
31
|
|
|
{ |
32
|
|
|
$this->container = $container; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param string $name The name of the service to check. |
37
|
|
|
* |
38
|
|
|
* @return bool |
39
|
|
|
* |
40
|
|
|
* @throws AdapterException If the container raises an error |
41
|
|
|
*/ |
42
|
|
|
public function hasService(string $name): bool |
43
|
|
|
{ |
44
|
|
|
try { |
45
|
|
|
return $this->container->has($name); |
46
|
|
|
} catch (ContainerExceptionInterface $e) { |
47
|
|
|
throw new AdapterException($e->getMessage(), $e->getCode(), $e); |
|
|
|
|
48
|
|
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string $name The name of the service to return |
53
|
|
|
* |
54
|
|
|
* @return mixed |
55
|
|
|
* |
56
|
|
|
* @throws NotFoundException |
57
|
|
|
* @throws AdapterException |
58
|
|
|
*/ |
59
|
|
|
public function getService(string $name) |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
return $this->container->get($name); |
63
|
|
|
} catch (NotFoundExceptionInterface $e) { |
64
|
|
|
throw new NotFoundException($e->getMessage(), $e->getCode(), $e); |
65
|
|
|
} catch (ContainerExceptionInterface $e) { |
66
|
|
|
throw new AdapterException($e->getMessage(), $e->getCode(), $e); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|