forked from ProcessMaker/processmaker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHasControllerAddons.php
More file actions
54 lines (48 loc) · 1.66 KB
/
HasControllerAddons.php
File metadata and controls
54 lines (48 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
<?php
namespace ProcessMaker\Traits;
trait HasControllerAddons
{
private static $addons = [];
/**
* Get configured addons for this controller
*
* @param string $method filter to identify the type of addon we are interested on
* @param array $data data that the controller will pass to the addon views
*
* @return array
*/
protected function getPluginAddons($method, array $data)
{
if (!isset(static::$addons)) {
return;
}
$addons = [];
foreach (static::$addons as $addon) {
// The addon must have the requested method and must be associated to the current controller
if ($addon['method'] === $method && $addon['scope'] === get_class($this)) {
if (isset($addon['data']) && is_callable($addon['data'])) {
$data = call_user_func($addon['data'], $data);
}
$addon['content'] = isset($addon['view']) && !isset($addon['content'])
? view($addon['view'], $data)->render() : (isset($addon['content'])
? $addon['content'] : '');
$addon['script'] = isset($addon['script']) && is_string($addon['script']) ? view($addon['script'], $data)->render() : '';
$addons[] = $addon;
}
}
return $addons;
}
/**
* Register a controller addon
*
* @param array $config
*
* @return void
*/
public static function registerAddon(array $config)
{
// Add the controller to which the addon is attached
$config['scope'] = static::class;
static::$addons[] = $config;
}
}