1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Phossa Project |
4
|
|
|
* |
5
|
|
|
* PHP version 5.4 |
6
|
|
|
* |
7
|
|
|
* @category Library |
8
|
|
|
* @package Phossa2\Di |
9
|
|
|
* @copyright Copyright (c) 2016 phossa.com |
10
|
|
|
* @license http://mit-license.org/ MIT License |
11
|
|
|
* @link http://www.phossa.com/ |
12
|
|
|
*/ |
13
|
|
|
/*# declare(strict_types=1); */ |
14
|
|
|
|
15
|
|
|
namespace Phossa2\Di; |
16
|
|
|
|
17
|
|
|
use Phossa2\Di\Message\Message; |
18
|
|
|
use Phossa2\Di\Traits\ArrayAccessTrait; |
19
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
20
|
|
|
use Interop\Container\ContainerInterface; |
21
|
|
|
use Phossa2\Di\Exception\RuntimeException; |
22
|
|
|
use Phossa2\Di\Exception\NotFoundException; |
23
|
|
|
use Phossa2\Shared\Reference\DelegatorTrait; |
24
|
|
|
use Phossa2\Di\Interfaces\DelegatorInterface; |
25
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
26
|
|
|
use Phossa2\Config\Traits\DelegatorWritableTrait; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Delegator |
30
|
|
|
* |
31
|
|
|
* A writable and array accessable container delegator |
32
|
|
|
* |
33
|
|
|
* @package Phossa2\Di |
34
|
|
|
* @author Hong Zhang <[email protected]> |
35
|
|
|
* @see ObjectAbstract |
36
|
|
|
* @see ContainerInterface |
37
|
|
|
* @see DelegatorInterface |
38
|
|
|
* @see WritableInterface |
39
|
|
|
* @see \ArrayAccess |
40
|
|
|
* @version 2.0.0 |
41
|
|
|
* @since 2.0.0 added |
42
|
|
|
*/ |
43
|
|
|
class Delegator extends ObjectAbstract implements DelegatorInterface, \ArrayAccess |
|
|
|
|
44
|
|
|
{ |
45
|
|
|
use DelegatorTrait, ArrayAccessTrait, DelegatorWritableTrait; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* {@inheritDoc} |
49
|
|
|
*/ |
50
|
|
|
public function get($id) |
51
|
|
|
{ |
52
|
|
|
if ($this->hasInLookup($id)) { |
53
|
|
|
return $this->getFromLookup($id); |
54
|
|
|
} else { |
55
|
|
|
throw new NotFoundException( |
56
|
|
|
Message::get(Message::DI_SERVICE_NOTFOUND, $id), |
57
|
|
|
Message::DI_SERVICE_NOTFOUND |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function has($id) |
66
|
|
|
{ |
67
|
|
|
return $this->hasInLookup($id); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* {@inheritDoc} |
72
|
|
|
*/ |
73
|
|
|
public function set(/*# string */ $id, $value) |
74
|
|
|
{ |
75
|
|
|
if ($this->isWritable()) { |
76
|
|
|
$this->writable->set($id, $value); |
77
|
|
|
return $this; |
78
|
|
|
} else { |
79
|
|
|
throw new RuntimeException( |
80
|
|
|
Message::get(Message::DI_CONTAINER_READONLY, $id), |
81
|
|
|
Message::DI_CONTAINER_READONLY |
82
|
|
|
); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* {@inheritDoc} |
88
|
|
|
*/ |
89
|
|
|
public function addContainer(ContainerInterface $container) |
90
|
|
|
{ |
91
|
|
|
return $this->addRegistry($container); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* {@inheritDoc} |
96
|
|
|
*/ |
97
|
|
|
protected function isValidRegistry($registry)/*# : bool */ |
98
|
|
|
{ |
99
|
|
|
return $registry instanceof ContainerInterface; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* {@inheritDoc} |
104
|
|
|
*/ |
105
|
|
|
protected function hasInRegistry( |
106
|
|
|
$registry, |
107
|
|
|
/*# string */ $key |
108
|
|
|
)/*# : bool */ { |
109
|
|
|
/* @var $registry ContainerInterface */ |
110
|
|
|
return $registry->has($key); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* {@inheritDoc} |
115
|
|
|
*/ |
116
|
|
|
protected function getFromRegistry( |
117
|
|
|
$registry, |
118
|
|
|
/*# string */ $key |
119
|
|
|
) { |
120
|
|
|
/* @var $registry ContainerInterface */ |
121
|
|
|
return $registry->get($key); |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|