Elgg  Version 3.0
PluginsHelper.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Elgg\Cli;
4 
8 trait PluginsHelper {
9 
20  public function activate($id, $force = false) {
21 
23  if (!$plugin) {
24  throw new \InvalidParameterException("No plugin with '$id'' found");
25  }
26 
27  if (!$plugin->getManifest()) {
28  throw new \InvalidParameterException("Plugin manifest for '$id' is invalid");
29  }
30 
31  if (!$force) {
32  return $plugin->activate();
33  }
34 
35  $conflicts = $this->getConflicts($id);
36  foreach ($conflicts as $conflict) {
37  $this->deactivate($conflict, true);
38  }
39 
40  $requires = $this->getRequires($id);
41  foreach ($requires as $require) {
42  $this->activate($require, true);
43  }
44 
45  return $plugin->activate();
46  }
47 
58  public function deactivate($id, $force = false) {
60  if (!$plugin) {
61  throw new \InvalidParameterException("No plugin with '$id'' found");
62  }
63 
64  if (!$plugin->getManifest()) {
65  throw new \InvalidParameterException("Plugin manifest for '$id' is invalid");
66  }
67 
68  if (!$force) {
69  return $plugin->deactivate();
70  }
71 
72  $dependents = $this->getDependents($id);
73  foreach ($dependents as $dependent) {
74  $this->deactivate($dependent);
75  }
76 
77  return $plugin->deactivate();
78  }
79 
87  public function getDependents($id) {
88  $result = [];
89 
90  $active_plugins = elgg_get_plugins();
91 
92  foreach ($active_plugins as $plugin) {
93  $manifest = $plugin->getManifest();
94  if (!$manifest) {
95  continue;
96  }
97 
98  $requires = $manifest->getRequires();
99 
100  foreach ($requires as $required) {
101  if ($required['type'] == 'plugin' && $required['name'] == $id) {
102  // there are active dependents
103  $result[] = $manifest->getPluginID();
104  }
105  }
106  }
107 
108  return $result;
109  }
110 
118  public function getConflicts($id) {
119  $result = [];
120 
121  $plugin = elgg_get_plugin_from_id($id);
122 
123  $conflicts = $plugin->getManifest()->getConflicts();
124 
125  foreach ($conflicts as $conflict) {
126  if ($conflict['type'] === 'plugin') {
127  $name = $conflict['name'];
129  $result[] = $name;
130  }
131  }
132  }
133 
134  return $result;
135  }
136 
144  public function getRequires($id) {
145  $result = [];
146 
147  $plugin = elgg_get_plugin_from_id($id);
148 
149  $requires = $plugin->getManifest()->getRequires();
150 
151  foreach ($requires as $require) {
152  if ($require['type'] === 'plugin') {
153  $name = $require['name'];
155  $result[] = $name;
156  }
157  }
158  }
159 
160  return $result;
161  }
162 }
$plugin
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
$id
River item delete action.
Definition: delete.php:6
elgg_get_plugins($status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:76
$required
Definition: label.php:12
elgg_get_plugin_from_id($plugin_id)
Returns an object with the path $path.
Definition: plugins.php:28