Elgg  Version master
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(string $id, bool $force = false): bool {
25  $split = explode(':', $id);
26 
27  $plugin_id = $split[0] ?? $id;
28  $priority = $split[1] ?? null;
29 
31  if (!$plugin instanceof \ElggPlugin) {
32  throw new UnexpectedValueException(elgg_echo('PluginException:InvalidID', [$plugin_id]));
33  }
34 
35  if ($plugin->isActive()) {
36  return true;
37  }
38 
39  if (isset($priority)) {
40  $plugin->setPriority($priority);
41  }
42 
43  if (!$force) {
44  try {
45  return $plugin->activate();
46  } catch (PluginException $e) {
47  return false;
48  }
49  }
50 
51  $conflicts = $this->getConflicts($plugin_id);
52  foreach ($conflicts as $conflict) {
53  $this->deactivate($conflict, true);
54  }
55 
56  $requires = $this->getRequires($plugin_id);
57  foreach ($requires as $require) {
58  $this->activate($require, true);
59  }
60 
61  try {
62  return $plugin->activate();
63  } catch (PluginException $e) {
64  return false;
65  }
66  }
67 
77  public function deactivate(string $id, bool $force = false): bool {
79  if (!$plugin instanceof \ElggPlugin) {
80  throw new UnexpectedValueException(elgg_echo('PluginException:InvalidID', [$id]));
81  }
82 
83  if (!$plugin->isActive()) {
84  return true;
85  }
86 
87  if (!$force) {
88  try {
89  return $plugin->deactivate();
90  } catch (PluginException $e) {
91  return false;
92  }
93  }
94 
95  $dependents = $this->getDependents($id);
96  foreach ($dependents as $dependent) {
97  $this->deactivate($dependent, true);
98  }
99 
100  try {
101  return $plugin->deactivate();
102  } catch (PluginException $e) {
103  return false;
104  }
105  }
106 
114  protected function getDependents(string $id): array {
115  $dependents = [];
116 
117  $active_plugins = elgg_get_plugins();
118 
119  foreach ($active_plugins as $plugin) {
120  $dependencies = $plugin->getDependencies();
121  if (!array_key_exists($id, $dependencies)) {
122  continue;
123  }
124 
125  if (elgg_extract('must_be_active', $dependencies[$id], true)) {
126  $dependents[] = $plugin->getID();
127  }
128  }
129 
130  return $dependents;
131  }
132 
140  public function getConflicts(string $id): array {
141  $result = [];
142 
143  $plugin = elgg_get_plugin_from_id($id);
144  $conflicts = $plugin->getConflicts();
145 
146  foreach ($conflicts as $plugin_id => $plugin_version) {
147  // @todo need to validate version
149  $result[] = $plugin_id;
150  }
151  }
152 
153  return $result;
154  }
155 
163  public function getRequires(string $id): array {
164  $result = [];
165 
166  $plugin = elgg_get_plugin_from_id($id);
167  foreach ($plugin->getDependencies() as $plugin_id => $config) {
168  if (!elgg_extract('must_be_active', $config, true)) {
169  continue;
170  }
171 
172  if (!elgg_get_plugin_from_id($plugin_id)) {
173  continue;
174  }
175 
176  $result[] = $plugin_id;
177  }
178 
179  return $result;
180  }
181 }
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:55
$plugin
Plugin class containing helper functions for plugin activation/deactivation, dependency checking capa...
Definition: ElggPlugin.php:17
elgg_get_plugin_from_id(string $plugin_id)
Elgg plugins library Contains functions for managing plugins.
Definition: plugins.php:15
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
$plugin_id
Remove all user and plugin settings from the give plugin ID.
Definition: remove.php:8
elgg_extract($key, $array, $default=null, bool $strict=true)
Checks for $array[$key] and returns its value if it exists, else returns $default.
Definition: elgglib.php:254
$config
Advanced site settings, debugging section.
Definition: debugging.php:6
Exception thrown if a value does not match with a set of values.
$id
Generic annotation delete action.
Definition: delete.php:6
$priority