|
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\InvalidCallableDefinition; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* CallableDefinition |
|
39
|
|
|
* |
|
40
|
|
|
* Represents a Definition that require a function (callable) to be executed |
|
41
|
|
|
* to obtain its value. |
|
42
|
|
|
* |
|
43
|
|
|
* @category Definition |
|
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 CallableDefinition extends AbstractDefinition implements Invokable |
|
50
|
|
|
{ |
|
51
|
|
|
/** |
|
52
|
|
|
* The callable function |
|
53
|
|
|
* @var callable |
|
54
|
|
|
*/ |
|
55
|
|
|
protected $callable; |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Constructor |
|
59
|
|
|
* |
|
60
|
|
|
* @param callable $callable The function to be called |
|
61
|
|
|
* @param array<mixed> $arguments List of arguments |
|
62
|
|
|
* |
|
63
|
|
|
* @return void |
|
|
|
|
|
|
64
|
|
|
*/ |
|
65
|
10 |
|
public function __construct($callable, array $arguments = array()) |
|
66
|
|
|
{ |
|
67
|
10 |
|
$this->callable = $callable; |
|
68
|
10 |
|
$this->arguments = $arguments; |
|
69
|
10 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* Calls $this->callable and return its value |
|
73
|
|
|
* |
|
74
|
|
|
* @param Container $container The Di Container |
|
75
|
|
|
* @param null|string $name Name of the definition (if any) |
|
76
|
|
|
* |
|
77
|
|
|
* @return mixed |
|
78
|
|
|
* @throws Exceptions\InvalidCallableDefinition |
|
79
|
|
|
*/ |
|
80
|
7 |
|
public function invoke(Container $container, $name = null) |
|
81
|
|
|
{ |
|
82
|
7 |
|
if (!is_callable($this->callable)) { |
|
83
|
1 |
|
throw new InvalidCallableDefinition($this->callable, $name); |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
6 |
|
$args = array(); |
|
87
|
|
|
try { |
|
88
|
6 |
|
$args = $this->getConstructorArguments($container, $name); |
|
89
|
6 |
|
} catch(Exception $exp) { |
|
90
|
2 |
|
throw new InvalidCallableDefinition($this->callable, $name, $exp); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
4 |
|
return call_user_func_array($this->callable, $args); |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* Returns the callable function |
|
98
|
|
|
* |
|
99
|
|
|
* @return callable |
|
100
|
|
|
*/ |
|
101
|
4 |
|
public function getCallable() |
|
102
|
|
|
{ |
|
103
|
4 |
|
return $this->callable; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Defines the callable function |
|
108
|
|
|
* |
|
109
|
|
|
* @param callable $callable The callable function |
|
110
|
|
|
* |
|
111
|
|
|
* @return void |
|
112
|
|
|
*/ |
|
113
|
6 |
|
public function setCallable($callable) |
|
114
|
|
|
{ |
|
115
|
6 |
|
$this->callable = $callable; |
|
116
|
|
|
} |
|
117
|
|
|
} |
Adding a
@returnannotation 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.