1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Core\Http\Middleware; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Http\Request; |
7
|
|
|
|
8
|
|
|
class ShortcodeMiddleware |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* Intercept a response to replace shortcodes |
12
|
|
|
* |
13
|
|
|
* @param \Illuminate\Http\Request $request |
14
|
|
|
* @param \Closure $next |
15
|
|
|
* @return mixed |
16
|
|
|
*/ |
17
|
|
|
public function handle($request, Closure $next) |
18
|
|
|
{ |
19
|
|
|
$response = $next($request); |
20
|
|
|
|
21
|
|
|
// Get defined shortcodes from the config file |
22
|
|
|
$shotcodesDefinitions = config('asgard.core.shortcodes', []); |
23
|
|
|
|
24
|
|
|
foreach ($shotcodesDefinitions as $shortcodeName => $options) { |
25
|
|
|
|
26
|
|
|
// Search for shortcodes in the response's content |
27
|
|
|
if (preg_match("/\[$shortcodeName.*\]/", $response->getContent(), $shortcodes)) { |
28
|
|
|
foreach ($shortcodes as $shortcode) { |
29
|
|
|
|
30
|
|
|
// Search for parameters |
31
|
|
|
preg_match('/\w+=".*"/', $shortcode, $rawParameters); |
32
|
|
|
|
33
|
|
|
if (! empty($rawParameters)) { |
34
|
|
|
$parameters = new Request(); |
35
|
|
|
|
36
|
|
|
// Parse every parameters and put them in a Request object |
37
|
|
|
foreach ($rawParameters as $rawParameter) { |
38
|
|
|
list($name, $value) = explode('=', $rawParameter); |
39
|
|
|
|
40
|
|
|
$parameters->query->set($name, trim($value, '"')); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
if (array_key_exists('callback', $options)) { |
44
|
|
|
// Call a function to interpret parameters and get the results as an array |
45
|
|
|
$results = call_user_func($options['callback'], $parameters); |
46
|
|
|
} |
47
|
|
|
} else { |
48
|
|
|
if (array_key_exists('callback', $options)) { |
49
|
|
|
// Call a function to interpret parameters and get the results as an array |
50
|
|
|
$results = call_user_func($options['callback']); |
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
if (isset($options['view'])) { |
55
|
|
|
$view = view($options['view'], compact('results'))->render(); |
|
|
|
|
56
|
|
|
} else { |
57
|
|
|
// If there is no view configured, it assumes that the callback himself is rendering the view |
58
|
|
|
$view = $results; |
|
|
|
|
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// Replace the shortcode by the corresponfing view and datas |
62
|
|
|
$response->setContent(preg_replace("/\[$shortcodeName.*\]/", $view, $response->getContent())); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $response; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
} |
71
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: