Elgg  Version 4.3
PluginsHelper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Traits\Cli;
4 
7 
13 trait PluginsHelper {
14 
24  public function activate($id, $force = false) {
25 
27  if (!$plugin) {
28  throw new InvalidParameterException(elgg_echo('PluginException:InvalidID', [$id]));
29  }
30 
31  if (!$force) {
32  try {
33  return $plugin->activate();
34  } catch (PluginException $e) {
35  return false;
36  }
37  }
38 
39  $conflicts = $this->getConflicts($id);
40  foreach ($conflicts as $conflict) {
41  $this->deactivate($conflict, true);
42  }
43 
44  $requires = $this->getRequires($id);
45  foreach ($requires as $require) {
46  $this->activate($require, true);
47  }
48 
49  try {
50  return $plugin->activate();
51  } catch (PluginException $e) {
52  return false;
53  }
54  }
55 
65  public function deactivate($id, $force = false) {
67  if (!$plugin) {
68  throw new InvalidParameterException(elgg_echo('PluginException:InvalidID', [$id]));
69  }
70 
71  if (!$force) {
72  try {
73  return $plugin->deactivate();
74  } catch (PluginException $e) {
75  return false;
76  }
77  }
78 
79  $dependents = $this->getDependents($id);
80  foreach ($dependents as $dependent) {
81  $this->deactivate($dependent, true);
82  }
83 
84  try {
85  return $plugin->deactivate();
86  } catch (PluginException $e) {
87  return false;
88  }
89  }
90 
98  protected function getDependents($id) {
99  $dependents = [];
100 
101  $active_plugins = elgg_get_plugins();
102 
103  foreach ($active_plugins as $plugin) {
104  $dependencies = $plugin->getDependencies();
105  if (!array_key_exists($id, $dependencies)) {
106  continue;
107  }
108 
109  if (elgg_extract('must_be_active', $dependencies[$id], true)) {
110  $dependents[] = $plugin->getID();
111  }
112  }
113 
114  return $dependents;
115  }
116 
124  public function getConflicts($id) {
125  $result = [];
126 
127  $plugin = elgg_get_plugin_from_id($id);
128  $conflicts = $plugin->getConflicts();
129 
130  foreach ($conflicts as $plugin_id => $plugin_version) {
131  // @todo need to validate version
133  $result[] = $plugin_id;
134  }
135  }
136 
137  return $result;
138  }
139 
147  public function getRequires($id) {
148  $result = [];
149 
150  $plugin = elgg_get_plugin_from_id($id);
151  foreach ($plugin->getDependencies() as $plugin_id => $config) {
152  if (!elgg_extract('must_be_active', $config, true)) {
153  continue;
154  }
155 
156  if (!elgg_get_plugin_from_id($plugin_id)) {
157  continue;
158  }
159 
160  $result[] = $plugin_id;
161  }
162 
163  return $result;
164  }
165 }
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:55
$plugin
elgg_get_plugin_from_id(string $plugin_id)
Elgg plugins library Contains functions for managing plugins.
Definition: plugins.php:15
elgg_echo($message_key, array $args=[], $language="")
Elgg language module Functions to manage language and translations.
Definition: languages.php:18
$plugin_id
Remove all user and plugin settings from the give plugin ID.
Definition: remove.php:8
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
elgg_extract($key, $array, $default=null, $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:547
$id
Generic annotation delete action.
Definition: delete.php:6