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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
|
<?php
/**
* Factory Class for the plugin engine
* @author Dennis Reil, <dennis.reil@offis.de>
* @template P of StudIPPlugin
*/
class PluginEngine
{
/**
* This function maps an incoming request to a tuple
* (pluginclassname, unconsumed rest).
* @param string $dispatch_to
* @return array the above mentioned tuple
*/
public static function routeRequest($dispatch_to)
{
$dispatch_to = ltrim($dispatch_to, '/');
if (mb_strlen($dispatch_to) === 0) {
throw new PluginNotFoundException(_('Es wurde kein Plugin gewählt.'));
}
$pos = mb_strpos($dispatch_to, '/');
return $pos === false
? [$dispatch_to, '']
: [mb_substr($dispatch_to, 0, $pos), mb_substr($dispatch_to, $pos + 1)];
}
/**
* Load the default set of plugins. This currently loads plugins of
* type Homepage, Standard (if a course is selected), Administration
* (if user has admin status) and System. The exact type of plugins
* loaded here may change in the future.
*/
public static function loadPlugins()
{
$plugin_manager = PluginManager::getInstance();
$plugin_info = $plugin_manager->getPluginInfos();
$context_id = Context::getId();
uasort($plugin_info, fn($a, $b) =>
$b['core'] - $a['core'] ?: $a['position'] - $b['position']
);
foreach ($plugin_info as $id => $info) {
// load system plugins
if (in_array(SystemPlugin::class, $info['type'])) {
$plugin_manager->getPluginById($id);
}
// load homepage plugins
if (in_array(HomepagePlugin::class, $info['type'])) {
$plugin_manager->getPluginById($id);
}
// load course plugins
if (in_array(StudipModule::class, $info['type'])) {
if ($context_id) {
if ($plugin_manager->isPluginActivated($id, $context_id)) {
$navigation = Navigation::getItem('/course');
$module = $plugin_manager->getPluginById($id);
if ($module) {
/** @var StudipModule $module */
$tabs = $module->getTabNavigation($context_id);
if ($navigation && $tabs) {
$navigation->addToolNavigation($id, $tabs);
}
}
}
}
}
// load admin plugins
if (in_array(AdministrationPlugin::class, $info['type'])) {
if ($GLOBALS['perm']->have_perm('admin')) {
$plugin_manager->getPluginById($id);
}
}
}
}
/**
* Get instance of the plugin specified by plugin class name.
*
* @param class-string<P> $class class name of plugin
* @return P
*/
public static function getPlugin ($class)
{
return PluginManager::getInstance()->getPlugin($class);
}
/**
* Get instances of all plugins of the specified type. A type of NULL
* returns all enabled plugins. The optional context parameter can be
* used to get only plugins that are activated in the given context.
*
* @param class-string<P>|null $type plugin type or null (all types)
* @param string $context context range id (optional)
* @return P[]|StudIPPlugin[] all plugins of the specified type
*/
public static function getPlugins ($type, $context = null)
{
return PluginManager::getInstance()->getPlugins($type, $context);
}
/**
* Sends a message to all activated plugins of a type and returns an array of
* the return values.
*
* @param string $type plugin type or null (all types)
* @param string $method the method name that should be send to all plugins
* @param mixed a variable number of arguments
*
* @return array an array containing the return values
*/
public static function sendMessage($type, $method /* ... */)
{
$args = func_get_args();
array_splice($args, 1, 0, [null]);
return call_user_func_array([__CLASS__, 'sendMessageWithContext'], $args);
}
/**
* Sends a message to all activated plugins of a type enabled in a context and
* returns an array of the return values.
*
* @param string $type plugin type or null (all types)
* @param string $context context range id (may be null)
* @param string $method the method name that should be send to all plugins
* @param mixed a variable number of arguments
*
* @return array an array containing the return values
*/
public static function sendMessageWithContext($type, $context, $method /* ... */)
{
$args = func_get_args();
$args = array_slice($args, 3);
$results = [];
foreach (self::getPlugins($type, $context) as $plugin) {
$results[] = call_user_func_array([$plugin, $method], $args);
}
return $results;
}
/**
* Generates a URL which can be shown in user interfaces
* @param StudIPPlugin|string $plugin - the plugin to which should be linked
* @param array $params - an array with name value pairs
* @param string $cmd - command to execute by clicking the link
* @param bool $ignore_registered_params do not add registered params
* @return string a link to the current plugin with the additional $params
*/
public static function getURL($plugin, $params = [], $cmd = 'show', $ignore_registered_params = false)
{
if (is_null($plugin)) {
throw new InvalidArgumentException(_('Es wurde kein Plugin gewählt.'));
} else if (is_object($plugin)) {
$plugin = mb_strtolower(get_class($plugin)) . '/' . $cmd;
} else if (mb_strpos($plugin, '/') === false) {
$plugin = $plugin . '/' . $cmd;
}
return URLHelper::getURL('plugins.php/' . $plugin, $params, $ignore_registered_params);
}
/**
* Generates a link (entity encoded URL) which can be shown in user interfaces
* @param StudIPPlugin|string $plugin - the plugin to which should be linked
* @param array $params - an array with name value pairs
* @param string $cmd - command to execute by clicking the link
* @param bool $ignore_registered_params do not add registeredparams
* @return string a link to the current plugin with the additional $params
*/
public static function getLink($plugin, $params = [], $cmd = 'show', $ignore_registered_params = false)
{
return htmlReady(self::getURL($plugin, $params, $cmd, $ignore_registered_params));
}
}
|