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\Resolver; |
16
|
|
|
|
17
|
|
|
use Phossa2\Shared\Base\ObjectAbstract; |
18
|
|
|
use Phossa2\Di\Traits\ContainerAwareTrait; |
19
|
|
|
use Phossa2\Config\Interfaces\ConfigInterface; |
20
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareTrait; |
21
|
|
|
use Phossa2\Di\Interfaces\ContainerAwareInterface; |
22
|
|
|
use Phossa2\Shared\Reference\DelegatorAwareInterface; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ObjectResolver |
26
|
|
|
* |
27
|
|
|
* - ObjectResolver is a readonly `ConfigInterface` wrapping around a container |
28
|
|
|
* (it implements `ContainerAwareInterface`) for resolving '#service_id' type of |
29
|
|
|
* objects |
30
|
|
|
* |
31
|
|
|
* - ObjectResolver can be injected into a config delegator, so it implements the |
32
|
|
|
* `DelegatorAwareInterface` |
33
|
|
|
* |
34
|
|
|
* @package Phossa2\Di |
35
|
|
|
* @author Hong Zhang <[email protected]> |
36
|
|
|
* @see ConfigInterface |
37
|
|
|
* @see DelegatorAwareInterface |
38
|
|
|
* @see ContainerAwareInterface |
39
|
|
|
* @version 2.0.0 |
40
|
|
|
* @since 2.0.0 added |
41
|
|
|
*/ |
42
|
|
|
class ObjectResolver extends ObjectAbstract implements ConfigInterface, DelegatorAwareInterface, ContainerAwareInterface |
43
|
|
|
{ |
44
|
|
|
use DelegatorAwareTrait, ContainerAwareTrait; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get '#service_id' from the container |
48
|
|
|
* |
49
|
|
|
* {@inheritDoc} |
50
|
|
|
*/ |
51
|
|
|
public function get(/*# string */ $id, $default = null) |
52
|
|
|
{ |
53
|
|
|
if (static::isServiceId($id)) { |
54
|
|
|
return $this->getContainer()->get(static::getRawId($id)); |
55
|
|
|
} else { |
56
|
|
|
return $default; |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Has '#service_id' in the container ? |
62
|
|
|
* |
63
|
|
|
* {@inheritDoc} |
64
|
|
|
*/ |
65
|
|
|
public function has(/*# string */ $id)/*# : bool */ |
66
|
|
|
{ |
67
|
|
|
if (static::isServiceId($id)) { |
68
|
|
|
return $this->getContainer()->has(static::getRawId($id)); |
69
|
|
|
} |
70
|
|
|
return false; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Convert '#service_id' to 'service_id' |
75
|
|
|
* |
76
|
|
|
* @param string $serviceId |
77
|
|
|
* @return string |
78
|
|
|
* @access protected |
79
|
|
|
*/ |
80
|
|
|
public static function getRawId(/*# string */ $serviceId)/*# : string */ |
81
|
|
|
{ |
82
|
|
|
return substr($serviceId, 1); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* Convert 'service_id' to '#service_id' |
87
|
|
|
* |
88
|
|
|
* @param string $rawId |
89
|
|
|
* @return string |
90
|
|
|
* @access public |
91
|
|
|
* @static |
92
|
|
|
*/ |
93
|
|
|
public static function getServiceId(/*# string */ $rawId)/*# : string */ |
94
|
|
|
{ |
95
|
|
|
return '#' . $rawId; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Is $serviceId something like '#service_id' ? |
100
|
|
|
* |
101
|
|
|
* @param mixed $serviceId |
102
|
|
|
* @return bool |
103
|
|
|
* @access public |
104
|
|
|
* @static |
105
|
|
|
*/ |
106
|
|
|
public static function isServiceId($serviceId)/*# : bool */ |
107
|
|
|
{ |
108
|
|
|
if (is_string($serviceId) && |
109
|
|
|
isset($serviceId[0]) && |
110
|
|
|
'#' == $serviceId[0] |
111
|
|
|
) { |
112
|
|
|
return true; |
113
|
|
|
} |
114
|
|
|
return false; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|