1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Fwk |
4
|
|
|
* |
5
|
|
|
* Copyright (c) 2011-2014, 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 Core |
27
|
|
|
* @package Fwk\Core |
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.fwk.pw |
32
|
|
|
*/ |
33
|
|
|
namespace Fwk\Core\Action; |
34
|
|
|
|
35
|
|
|
use Fwk\Core\ActionProxy; |
36
|
|
|
use Fwk\Core\Application; |
37
|
|
|
use Fwk\Core\Context; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* ActionProxy to a Closure or a Callable. |
41
|
|
|
* |
42
|
|
|
* @category ActionProxy |
43
|
|
|
* @package Fwk\Core |
44
|
|
|
* @author Julien Ballestracci <[email protected]> |
45
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
46
|
|
|
* @link http://www.fwk.pw |
47
|
|
|
*/ |
48
|
|
|
class CallableActionProxy implements ActionProxy |
49
|
|
|
{ |
50
|
|
|
const PARAM_CONTEXT_NAME = 'context'; |
51
|
|
|
const PARAM_SERVICES_NAME = 'services'; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* The Closure or Callable |
55
|
|
|
* |
56
|
|
|
* @var mixed |
57
|
|
|
*/ |
58
|
|
|
protected $callable; |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $actionData = array(); |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Constructor |
68
|
|
|
* |
69
|
|
|
* @param mixed $callable Closure or Callable |
70
|
|
|
* |
71
|
|
|
* @throws \InvalidArgumentException if invalid $callable |
72
|
|
|
* @return void |
|
|
|
|
73
|
|
|
*/ |
74
|
34 |
|
public function __construct($callable) |
75
|
|
|
{ |
76
|
34 |
|
if (!is_callable($callable)) { |
77
|
1 |
|
throw new \InvalidArgumentException("The closure is not callable"); |
78
|
|
|
} |
79
|
|
|
|
80
|
34 |
|
$this->callable = $callable; |
81
|
34 |
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Executes the callable and returns the result |
85
|
|
|
* |
86
|
|
|
* @param Application $app The running Application |
87
|
|
|
* @param Context $context Actual context |
88
|
|
|
* |
89
|
|
|
* @return mixed The controller's result |
|
|
|
|
90
|
|
|
*/ |
91
|
19 |
|
public function execute(Application $app, Context $context) |
92
|
|
|
{ |
93
|
19 |
|
if ($this->callable instanceof \Closure) { |
94
|
18 |
|
$refFunc = new \ReflectionFunction($this->callable); |
95
|
|
|
|
96
|
18 |
|
$params = array(); |
97
|
18 |
|
$request = $context->getRequest(); |
98
|
18 |
|
foreach ($refFunc->getParameters() as $param) { |
99
|
13 |
|
if ($param->getName() == self::PARAM_CONTEXT_NAME) { |
100
|
5 |
|
$params[] = $context; |
101
|
13 |
|
} elseif ($param->getName() == self::PARAM_SERVICES_NAME) { |
102
|
3 |
|
$params[] = $app->getServices(); |
103
|
3 |
|
} else { |
104
|
6 |
|
$params[] = $request->get($param->getName(), null); |
105
|
|
|
} |
106
|
18 |
|
} |
107
|
|
|
|
108
|
18 |
|
$result = call_user_func_array($this->callable, $params); |
109
|
18 |
|
} else { |
110
|
1 |
|
$result = call_user_func($this->callable); |
111
|
|
|
} |
112
|
|
|
|
113
|
19 |
|
if (is_array($result)) { |
114
|
|
|
$this->actionData = array_merge($result, $this->actionData); |
115
|
|
|
} |
116
|
|
|
|
117
|
19 |
|
return $result; |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
public function getActionData() |
121
|
|
|
{ |
122
|
3 |
|
return $this->actionData; |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function setActionData(array $data) |
126
|
|
|
{ |
127
|
|
|
$this->actionData = $data; |
128
|
|
|
} |
129
|
|
|
} |
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.