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\Shared\Reference\DelegatorInterface; |
25
|
|
|
use Phossa2\Config\Interfaces\WritableInterface; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Delegator |
29
|
|
|
* |
30
|
|
|
* A writable and array accessable container delegator |
31
|
|
|
* |
32
|
|
|
* @package Phossa2\Di |
33
|
|
|
* @author Hong Zhang <[email protected]> |
34
|
|
|
* @see ObjectAbstract |
35
|
|
|
* @see ContainerInterface |
36
|
|
|
* @see DelegatorInterface |
37
|
|
|
* @see WritableInterface |
38
|
|
|
* @see \ArrayAccess |
39
|
|
|
* @version 2.0.0 |
40
|
|
|
* @since 2.0.0 added |
41
|
|
|
*/ |
42
|
|
|
class Delegator extends ObjectAbstract implements ContainerInterface, DelegatorInterface, \ArrayAccess, WritableInterface |
43
|
|
|
{ |
44
|
|
|
use DelegatorTrait, ArrayAccessTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* {@inheritDoc} |
48
|
|
|
*/ |
49
|
|
|
public function get($id) |
50
|
|
|
{ |
51
|
|
|
if ($this->hasInLookup($id)) { |
52
|
|
|
return $this->getFromLookup($id); |
53
|
|
|
} else { |
54
|
|
|
throw new NotFoundException( |
55
|
|
|
Message::get(Message::DI_SERVICE_NOTFOUND, $id), |
56
|
|
|
Message::DI_SERVICE_NOTFOUND |
57
|
|
|
); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* {@inheritDoc} |
63
|
|
|
*/ |
64
|
|
|
public function has($id) |
65
|
|
|
{ |
66
|
|
|
return $this->hasInLookup($id); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* {@inheritDoc} |
71
|
|
|
*/ |
72
|
|
|
public function set(/*# string */ $id, $value) |
73
|
|
|
{ |
74
|
|
|
if ($this->isWritable()) { |
75
|
|
|
$this->writable->set($id, $value); |
76
|
|
|
return $this; |
77
|
|
|
} else { |
78
|
|
|
throw new RuntimeException( |
79
|
|
|
Message::get(Message::DI_CONTAINER_READONLY, $id), |
80
|
|
|
Message::DI_CONTAINER_READONLY |
81
|
|
|
); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritDoc} |
87
|
|
|
*/ |
88
|
|
|
protected function isValidRegistry($registry)/*# : bool */ |
89
|
|
|
{ |
90
|
|
|
return $registry instanceof ContainerInterface; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* {@inheritDoc} |
95
|
|
|
*/ |
96
|
|
|
protected function hasInRegistry( |
97
|
|
|
$registry, |
98
|
|
|
/*# string */ $key |
99
|
|
|
)/*# : bool */ { |
100
|
|
|
/* @var $registry ContainerInterface */ |
101
|
|
|
return $registry->has($key); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritDoc} |
106
|
|
|
*/ |
107
|
|
|
protected function getFromRegistry( |
108
|
|
|
$registry, |
109
|
|
|
/*# string */ $key |
110
|
|
|
) { |
111
|
|
|
/* @var $registry ContainerInterface */ |
112
|
|
|
return $registry->get($key); |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
|