1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fwk |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2011-2012, Julien Ballestracci <[email protected]>. |
6
|
|
|
* All rights reserved. |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
12
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
13
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
14
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
15
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
16
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
17
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
18
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
19
|
|
|
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
20
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN |
21
|
|
|
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
22
|
|
|
* POSSIBILITY OF SUCH DAMAGE. |
23
|
|
|
* |
24
|
|
|
* PHP Version 5.3 |
25
|
|
|
* |
26
|
|
|
* @category DependencyInjection |
27
|
|
|
* @package Fwk\Di |
28
|
|
|
* @author Julien Ballestracci <[email protected]> |
29
|
|
|
* @copyright 2011-2014 Julien Ballestracci <[email protected]> |
30
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
31
|
|
|
* @link http://www.nitronet.org/fwk |
32
|
|
|
*/ |
33
|
|
|
namespace Fwk\Di; |
34
|
|
|
|
35
|
|
|
use Fwk\Di\Exceptions\DefinitionNotFoundException; |
36
|
|
|
use Fwk\Di\Exceptions\InvalidReferenceException; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Reference |
40
|
|
|
* |
41
|
|
|
* Reference another Definition in the Di Container |
42
|
|
|
* |
43
|
|
|
* @category Reference |
44
|
|
|
* @package Fwk\Di |
45
|
|
|
* @author Julien Ballestracci <[email protected]> |
46
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
47
|
|
|
* @link http://www.nitronet.org/fwk |
48
|
|
|
*/ |
49
|
|
|
class Reference implements InvokableInterface |
50
|
|
|
{ |
51
|
|
|
/** |
52
|
|
|
* Container's reference name |
53
|
|
|
* @var string |
54
|
|
|
*/ |
55
|
|
|
protected $name; |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Constructor |
59
|
|
|
* |
60
|
|
|
* @param string $name Container's reference name |
61
|
|
|
* |
62
|
|
|
* @return void |
|
|
|
|
63
|
|
|
*/ |
64
|
14 |
|
public function __construct($name) |
65
|
|
|
{ |
66
|
14 |
|
$this->name = (string)$name; |
67
|
14 |
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Gets the container's reference name |
71
|
|
|
* |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
1 |
|
public function getName() |
75
|
|
|
{ |
76
|
1 |
|
return $this->name; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Defines the container's reference name |
81
|
|
|
* |
82
|
|
|
* @param string $name Container's reference name |
83
|
|
|
* |
84
|
|
|
* @return Reference |
85
|
|
|
*/ |
86
|
4 |
|
public function setName($name) |
87
|
|
|
{ |
88
|
4 |
|
$this->name = $name; |
89
|
|
|
|
90
|
4 |
|
return $this; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Return the value of the referenced Definition |
95
|
|
|
* |
96
|
|
|
* @param Container $container The Di Container |
97
|
|
|
* @param null|string $name Name of the current definition (if any) |
98
|
|
|
* |
99
|
|
|
* @return mixed |
100
|
|
|
*/ |
101
|
10 |
|
public function invoke(Container $container, $name = null) |
102
|
|
|
{ |
103
|
|
|
try { |
104
|
10 |
|
$return = $container->get($this->name); |
105
|
5 |
|
} catch(DefinitionNotFoundException $exp) { |
106
|
5 |
|
throw new InvalidReferenceException($this->name, $name, $exp); |
107
|
|
|
} |
108
|
|
|
|
109
|
5 |
|
return $return; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Convenience method |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
4 |
|
public function __toString() |
118
|
|
|
{ |
119
|
4 |
|
return $this->name; |
120
|
|
|
} |
121
|
|
|
} |
Adding a
@return
annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.