|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Vjik\Yii2\Psr\ContainerProxy; |
|
4
|
|
|
|
|
5
|
|
|
use Psr\Container\ContainerInterface; |
|
6
|
|
|
use yii\base\InvalidConfigException as Yii2InvalidConfigException; |
|
7
|
|
|
use yii\di\Container; |
|
8
|
|
|
use yii\di\Instance; |
|
9
|
|
|
use yii\di\NotInstantiableException as Yii2NotInstantiableException; |
|
10
|
|
|
|
|
11
|
|
|
use function is_string; |
|
12
|
|
|
|
|
13
|
|
|
class ContainerProxy implements ContainerInterface |
|
14
|
|
|
{ |
|
15
|
|
|
private $container; |
|
16
|
|
|
|
|
17
|
7 |
|
public function __construct(Container $container) |
|
18
|
|
|
{ |
|
19
|
7 |
|
$this->container = $container; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Returns an instance by class Instance, class name, interface name or alias. |
|
24
|
|
|
* |
|
25
|
|
|
* @param string|Instance $id the class Instance, the interface or an alias name that was previously registered |
|
26
|
|
|
* @param array $params a list of constructor parameter values. The parameters should be provided in the order |
|
27
|
|
|
* they appear in the constructor declaration. If you want to skip some parameters, you should index the remaining |
|
28
|
|
|
* ones with the integers that represent their positions in the constructor parameter list. |
|
29
|
|
|
* @param array $config a list of name-value pairs that will be used to initialize the object properties. |
|
30
|
|
|
* |
|
31
|
|
|
* @return object An instance of the requested interface. |
|
32
|
|
|
* |
|
33
|
|
|
* @throws InvalidConfigException |
|
34
|
|
|
* @throws NotInstantiableException |
|
35
|
|
|
* @throws NotFoundException |
|
36
|
|
|
*/ |
|
37
|
3 |
|
public function get($id, array $params = [], array $config = []) |
|
38
|
|
|
{ |
|
39
|
3 |
|
if (is_string($id) && !$this->has($id)) { |
|
40
|
1 |
|
throw new NotFoundException("No definition for $id"); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
try { |
|
44
|
2 |
|
return $this->container->get($id, $params, $config); |
|
45
|
1 |
|
} catch (Yii2NotInstantiableException $e) { |
|
46
|
1 |
|
throw new NotInstantiableException($e->getMessage()); |
|
47
|
|
|
} catch (Yii2InvalidConfigException $e) { |
|
48
|
|
|
throw new InvalidConfigException($e->getMessage()); |
|
49
|
|
|
} |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* Returns a value indicating whether the container has the definition of the specified name. |
|
54
|
|
|
* |
|
55
|
|
|
* @param string $id The class name, interface name or alias name. |
|
56
|
|
|
* |
|
57
|
|
|
* @return bool Whether the container is able to provide instance of class specified. |
|
58
|
|
|
*/ |
|
59
|
7 |
|
public function has($id): bool |
|
60
|
|
|
{ |
|
61
|
7 |
|
return $this->container->hasSingleton($id) || |
|
62
|
7 |
|
$this->container->has($id) || |
|
63
|
7 |
|
class_exists($id); |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|