1
|
|
|
<?php namespace Myth\Bay; |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Class Bay |
5
|
|
|
* |
6
|
|
|
* @package Myth\Bay |
7
|
|
|
*/ |
8
|
|
|
class Bay { |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Instance of our customized class loader. |
12
|
|
|
* |
13
|
|
|
* @var LibraryFinderInterface|null |
14
|
|
|
*/ |
15
|
|
|
protected $finder = null; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Instance of our cache access library. |
19
|
|
|
* |
20
|
|
|
* @var CacheInterface|null |
21
|
|
|
*/ |
22
|
|
|
protected $cache = null; |
23
|
|
|
|
24
|
|
|
//-------------------------------------------------------------------- |
25
|
|
|
|
26
|
|
|
public function __construct(LibraryFinderInterface $finder = null, CacheInterface $cache = null) |
27
|
|
|
{ |
28
|
|
|
if (is_object($finder)) |
29
|
|
|
{ |
30
|
|
|
$this->finder = $finder; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
if (is_object($cache)) |
34
|
|
|
{ |
35
|
|
|
$this->cache = $cache; |
36
|
|
|
} |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
//-------------------------------------------------------------------- |
40
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* The primary method used. Will attempt to locate the library, run |
44
|
|
|
* the requested method, and return the rendered HTML. |
45
|
|
|
* |
46
|
|
|
* @param string $library |
47
|
|
|
* @param string|array $params |
48
|
|
|
* @param string $cache_name |
49
|
|
|
* @param int $cache_ttl // Time in _minutes_ |
50
|
|
|
* |
51
|
|
|
* @return null|string |
52
|
|
|
*/ |
53
|
|
|
public function display($library, $params=null, $cache_name=null, $cache_ttl=0) |
54
|
|
|
{ |
55
|
|
|
list($class, $method) = $this->determineClass($library); |
56
|
|
|
|
57
|
|
|
// Is it cached? |
58
|
|
|
$cache_name = ! empty($cache_name) ? $cache_name : $class . $method . md5(serialize($params)); |
59
|
|
|
|
60
|
|
|
if (! empty($this->cache) && $output = $this->cache->get($cache_name)) |
61
|
|
|
{ |
62
|
|
|
return $output; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
// Not cached - so grab it... |
66
|
|
|
$instance = new $class(); |
67
|
|
|
|
68
|
|
|
if (!method_exists($instance, $method)) |
69
|
|
|
{ |
70
|
|
|
throw new \InvalidArgumentException("{$class}::{$method} is not a valid method."); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$params_array = $this->prepareParams($params); |
74
|
|
|
$ref_method = new \ReflectionMethod($instance, $method); |
75
|
|
|
$num_of_params = $ref_method->getNumberOfParameters(); |
76
|
|
|
$ref_params = $ref_method->getParameters(); |
77
|
|
|
|
78
|
|
|
if ($num_of_params === 0) |
79
|
|
|
{ |
80
|
|
|
if ($params_array !== null) |
81
|
|
|
{ |
82
|
|
|
throw new \InvalidArgumentException("{$class}::{$method} has no params."); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
$output = $instance->{$method}(); |
86
|
|
|
} |
87
|
|
|
elseif ( |
88
|
|
|
($num_of_params === 1) |
89
|
|
|
&& ( |
90
|
|
|
(! array_key_exists($ref_params[0]->name, $params_array)) |
91
|
|
|
|| ( |
92
|
|
|
array_key_exists($ref_params[0]->name, $params_array) |
93
|
|
|
&& count($params_array) !== 1 |
94
|
|
|
) |
95
|
|
|
) |
96
|
|
|
) |
97
|
|
|
{ |
98
|
|
|
$output = $instance->{$method}($params_array); |
99
|
|
|
} |
100
|
|
|
else |
101
|
|
|
{ |
102
|
|
|
$fire_args = []; |
103
|
|
|
$method_params = []; |
104
|
|
|
|
105
|
|
|
foreach($ref_params as $arg) |
106
|
|
|
{ |
107
|
|
|
$method_params[$arg->name] = true; |
108
|
|
|
if (array_key_exists($arg->name, $params_array)) |
109
|
|
|
{ |
110
|
|
|
$fire_args[$arg->name] = $params_array[$arg->name]; |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
foreach ($params_array as $key => $val) |
|
|
|
|
115
|
|
|
{ |
116
|
|
|
if (! isset($method_params[$key])) |
117
|
|
|
{ |
118
|
|
|
throw new \InvalidArgumentException("{$key} is not a valid param name."); |
119
|
|
|
} |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
$output = call_user_func_array([$instance, $method], $fire_args); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
// Can we cache it? |
126
|
|
|
if (! empty($this->cache) && $cache_ttl !== 0) |
127
|
|
|
{ |
128
|
|
|
$this->cache->set($cache_name, $output, $cache_ttl); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return $output; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
//-------------------------------------------------------------------- |
135
|
|
|
|
136
|
|
|
//-------------------------------------------------------------------- |
137
|
|
|
// Utility Methods |
138
|
|
|
//-------------------------------------------------------------------- |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Attempts to locate and load the class passed in the library |
142
|
|
|
* portion of the display() method. |
143
|
|
|
* |
144
|
|
|
* First, we will try to autoload the file. If it cannot be found there, |
145
|
|
|
* we will try to run a framework-specific loader, if the user provided |
146
|
|
|
* one in __construct(); |
147
|
|
|
* |
148
|
|
|
* @param $library |
149
|
|
|
* @return array |
150
|
|
|
*/ |
151
|
|
|
public function determineClass($library) |
152
|
|
|
{ |
153
|
|
|
$found = false; |
154
|
|
|
|
155
|
|
|
// We don't want to actually call static methods |
156
|
|
|
// by default, so convert any double colons. |
157
|
|
|
$library = str_replace('::', ':', $library); |
158
|
|
|
|
159
|
|
|
list($class, $method) = explode(':', $library); |
160
|
|
|
|
161
|
|
|
if (empty($class)) |
162
|
|
|
{ |
163
|
|
|
throw new \InvalidArgumentException('No class provided to Bay::display().'); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (!class_exists($class, true)) |
167
|
|
|
{ |
168
|
|
|
// Try the Finder to see if it can find it... |
169
|
|
|
if (!is_null($this->finder)) |
170
|
|
|
{ |
171
|
|
|
if ($this->finder->find($class)) |
172
|
|
|
{ |
173
|
|
|
$found = true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
else |
178
|
|
|
{ |
179
|
|
|
$found = true; |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if (!$found) |
183
|
|
|
{ |
184
|
|
|
throw new \InvalidArgumentException('Unable to locate class '.$class.', provided to Bay::display().'); |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (empty($method)) |
188
|
|
|
{ |
189
|
|
|
$method = 'index'; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
return [ $class, $method ]; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
//-------------------------------------------------------------------- |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Parses the params attribute. If an array, returns untouched. |
199
|
|
|
* If a string, it should be in the format "key1=value key2=value". |
200
|
|
|
* It will be split and returned as an array. |
201
|
|
|
* |
202
|
|
|
* @param $params |
203
|
|
|
* @return array|null |
204
|
|
|
*/ |
205
|
|
|
public function prepareParams($params) |
206
|
|
|
{ |
207
|
|
|
if (!is_string($params) && !is_array($params)) |
208
|
|
|
{ |
209
|
|
|
return null; |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
if (is_string($params)) |
213
|
|
|
{ |
214
|
|
|
if (empty($params)) |
215
|
|
|
{ |
216
|
|
|
return null; |
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
$new_params = [ ]; |
220
|
|
|
|
221
|
|
|
$separator = ' '; |
222
|
|
|
if (strpos($params, ',') !== false) |
223
|
|
|
{ |
224
|
|
|
$separator = ','; |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
$params = explode($separator, $params); |
228
|
|
|
unset($separator); |
229
|
|
|
|
230
|
|
|
foreach ($params as $p) |
231
|
|
|
{ |
232
|
|
|
list($key, $val) = explode('=', $p); |
233
|
|
|
|
234
|
|
|
$new_params[ trim($key) ] = trim($val, ', '); |
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
$params = $new_params; |
238
|
|
|
unset($new_params); |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
if (is_array($params) && !count($params)) |
242
|
|
|
{ |
243
|
|
|
return null; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
return $params; |
247
|
|
|
} |
248
|
|
|
|
249
|
|
|
//-------------------------------------------------------------------- |
250
|
|
|
|
251
|
|
|
} |
252
|
|
|
|
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.