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
|
|
|
use Fwk\Core\Exception; |
39
|
|
|
use Fwk\Di\Container; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* ActionProxy to a PHP file inclusion. |
43
|
|
|
* |
44
|
|
|
* Allows the developper to use $this->getContext() and $this->getServices() |
45
|
|
|
* inside the included file. |
46
|
|
|
* |
47
|
|
|
* @category ActionProxy |
48
|
|
|
* @package Fwk\Core |
49
|
|
|
* @author Julien Ballestracci <[email protected]> |
50
|
|
|
* @license http://www.opensource.org/licenses/bsd-license.php BSD License |
51
|
|
|
* @link http://www.fwk.pw |
52
|
|
|
*/ |
53
|
|
|
class IncludeActionProxy implements ActionProxy |
54
|
|
|
{ |
55
|
|
|
/** |
56
|
|
|
* Path to the to-be-included file |
57
|
|
|
* |
58
|
|
|
* @var string |
59
|
|
|
*/ |
60
|
|
|
protected $file; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Services Container |
64
|
|
|
* |
65
|
|
|
* @var Container |
66
|
|
|
*/ |
67
|
|
|
protected $services; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Actual Application Context |
71
|
|
|
* |
72
|
|
|
* @var Context |
73
|
|
|
*/ |
74
|
|
|
protected $context; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* |
78
|
|
|
* @var array |
79
|
|
|
*/ |
80
|
|
|
protected $actionData = array(); |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Constructor |
84
|
|
|
* |
85
|
|
|
* @param string $file Path to the PHP file to be included |
86
|
|
|
* |
87
|
|
|
* @throws \InvalidArgumentException if $file is empty |
88
|
|
|
*/ |
89
|
7 |
|
public function __construct($file) |
90
|
|
|
{ |
91
|
7 |
|
if (empty($file)) { |
92
|
1 |
|
throw new \InvalidArgumentException( |
93
|
|
|
"You must specify a file to include" |
94
|
1 |
|
); |
95
|
|
|
} |
96
|
|
|
|
97
|
7 |
|
$this->file = $file; |
98
|
7 |
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Includes the PHP file and return the content. |
102
|
|
|
* |
103
|
|
|
* @param Application $app The running Application |
104
|
|
|
* @param Context $context Actual context |
105
|
|
|
* |
106
|
|
|
* @return mixed or void if the file doesn't end with a return statement |
107
|
|
|
* @throws Exception if the file is not readable/does not exist |
108
|
|
|
*/ |
109
|
4 |
|
public function execute(Application $app, Context $context) |
110
|
|
|
{ |
111
|
4 |
|
if (!is_file($this->file) || !is_readable($this->file)) { |
112
|
1 |
|
throw new Exception( |
113
|
1 |
|
'Unable to include file: '. $this->file . ' (not found/readable)' |
114
|
1 |
|
); |
115
|
|
|
} |
116
|
|
|
|
117
|
3 |
|
$this->context = $context; |
118
|
3 |
|
$this->services = $app->getServices(); |
119
|
|
|
|
120
|
3 |
|
return include $this->file; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Return the Services Container |
125
|
|
|
* |
126
|
|
|
* @return Container |
127
|
|
|
*/ |
128
|
1 |
|
public function getServices() |
129
|
|
|
{ |
130
|
1 |
|
return $this->services; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Return the actual context |
135
|
|
|
* |
136
|
|
|
* @return Context |
137
|
|
|
*/ |
138
|
1 |
|
public function getContext() |
139
|
|
|
{ |
140
|
1 |
|
return $this->context; |
141
|
|
|
} |
142
|
|
|
|
143
|
1 |
|
public function getActionData() |
144
|
|
|
{ |
145
|
1 |
|
return $this->actionData; |
146
|
|
|
} |
147
|
|
|
|
148
|
1 |
|
public function setActionData(array $data) |
149
|
|
|
{ |
150
|
1 |
|
$this->actionData = $data; |
151
|
1 |
|
} |
152
|
|
|
|
153
|
1 |
|
public function __get($name) |
|
|
|
|
154
|
|
|
{ |
155
|
1 |
|
return (array_key_exists($name, $this->actionData) ? |
156
|
1 |
|
$this->actionData[$name] : |
157
|
|
|
false |
158
|
1 |
|
); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
public function __isset($name) |
|
|
|
|
162
|
|
|
{ |
163
|
|
|
return array_key_exists($name, $this->actionData); |
164
|
|
|
} |
165
|
|
|
} |
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@return
annotation as described here.