Elgg  Version 5.1
Composer.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Plugin;
3 
10 
17 class Composer {
18 
22  protected $plugin;
23 
27  protected $configuration;
28 
36  public function __construct(\ElggPlugin $plugin) {
37  $this->plugin = $plugin;
38 
39  try {
40  // need to suppress warning because of deprecated notices that get converted to warnings during phpunit
41  $reader = @new \Eloquent\Composer\Configuration\ConfigurationReader;
42  $this->configuration = $reader->read($this->plugin->getPath() . 'composer.json');
43  } catch (\Exception $e) {
44  throw new ComposerException(elgg_echo('ElggPlugin:NoPluginComposer', [
45  $this->plugin->getID(),
46  $this->plugin->guid,
47  ]));
48  }
49  }
50 
56  public function getConfiguration() {
57  return $this->configuration;
58  }
59 
66  public function assertPluginId() {
67  if ($this->configuration->projectName() !== $this->plugin->getID()) {
68  throw new IdMismatchException(elgg_echo('ElggPlugin:IdMismatch', [$this->configuration->projectName()]));
69  }
70  }
71 
77  public function getLicense() {
78  $license = $this->configuration->license();
79  if (!empty($license)) {
80  $license = implode(', ', $license);
81  }
82 
83  return (string) $license;
84  }
85 
91  public function getCategories() {
92  $cats = $this->configuration->keywords() ?: [];
93 
94  $result = [];
95  foreach ($cats as $cat) {
96  $result[strtolower($cat)] = $this->getFriendlyCategory($cat);
97  }
98 
99  // plugins often set Elgg in their keywords, we do not need that keyword
100  unset($result['elgg']);
101  unset($result['plugin']);
102 
103  // add vendor to categories
104  $vendor = strtolower((string) $this->configuration->vendorName());
105  if (!isset($result[$vendor])) {
106  $result[$vendor] = $this->getFriendlyCategory($vendor);
107  }
108 
109  return $result;
110  }
111 
117  public function getConflicts() {
118  $conflicts = $this->configuration->conflict();
119 
120  $result = [];
121  foreach ($conflicts as $name => $version) {
122  list(,$projectname) = explode('/', $name);
123  if (!empty($projectname)) {
124  $result[$projectname] = $version;
125  }
126  }
127 
128  return $result;
129  }
130 
137  public function assertConflicts() {
138  $conflicts = $this->getConflicts();
139  if (empty($conflicts)) {
140  return;
141  }
142 
143  if (isset($conflicts['elgg'])) {
144  if ($this->checkConstraints(elgg_get_release(), $conflicts['elgg'])) {
145  throw new ConflictException('Elgg version: ' . elgg_get_release() . ' conflicts with constraint '. $conflicts['elgg']);
146  }
147 
148  unset($conflicts['elgg']);
149  }
150 
151  foreach ($conflicts as $plugin_id => $constraints) {
153  continue;
154  }
155 
157 
158  if ($this->checkConstraints($plugin->getVersion(), $constraints)) {
159  throw new ConflictException("Plugin [{$plugin->getID()}] with version: {$plugin->getVersion()} conflicts with constraint {$constraints}");
160  }
161  }
162  }
163 
170  public function assertActivePluginConflicts() {
171  $active_plugins = elgg_get_plugins('active');
172  foreach ($active_plugins as $plugin) {
173  $conflicts = $plugin->getConflicts();
174  if (!isset($conflicts[$this->plugin->getID()])) {
175  continue;
176  }
177 
178  $constraint = $conflicts[$this->plugin->getID()];
179  if ($this->checkConstraints($this->plugin->getVersion(), $constraint)) {
180  $msg = 'The plugin ' . $this->plugin->getDisplayName() . ' with version ' . $this->plugin->getVersion();
181  $msg .= ' conflicts with constraint '. $constraint . ' defined in ' . $plugin->getID();
182  throw new ConflictException($msg);
183  }
184  }
185  }
186 
195  public function checkConstraints($version, $constraints) {
196  try {
197  return Semver::satisfies($version, $constraints);
198  } catch (\UnexpectedValueException $e) {
199  // something is wrong with the version number
200  elgg_log($e, 'ERROR');
201  }
202 
203  return false;
204  }
205 
212  public function assertRequiredPhpVersion() {
213  $requirements = $this->configuration->dependencies();
214  if (!isset($requirements['php'])) {
215  return;
216  }
217 
218  $php_version = phpversion();
219  if (!$this->checkConstraints($php_version, $requirements['php'])) {
220  throw new PhpVersionException("The PHP version ({$php_version}) does not meet the plugin [{$this->plugin->getID()}] requirements of {$requirements['php']}");
221  }
222  }
223 
230  public function assertRequiredPhpExtensions() {
231  $requirements = $this->configuration->dependencies();
232  foreach ($requirements as $name => $constraint) {
233  if (!str_starts_with($name, 'ext-')) {
234  continue;
235  }
236 
237  $extension = substr($name, 4);
238  if (!extension_loaded($extension)) {
239  throw new PhpExtensionException("Plugin [{$this->plugin->getID()}] requires the PHP extensions {$extension}");
240  }
241 
242  $extension_version = phpversion($extension);
243  if (!$this->checkConstraints($extension_version, $constraint)) {
244  throw new PhpExtensionException("The PHP extension version ({$extension_version}) does not meet the plugin [{$this->plugin->getID()}] requirements of {$constraint}");
245  }
246  }
247  }
248 
259  protected function getFriendlyCategory($category) {
260  $cat_raw_string = strtolower("admin:plugins:category:{$category}");
261  if (_elgg_services()->translator->languageKeyExists($cat_raw_string)) {
262  return elgg_echo($cat_raw_string);
263  }
264 
265  return ucwords(str_replace(['-', '_'], ' ', $category));
266  }
267 }
elgg_get_release()
Get the current Elgg release.
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:55
assertConflicts()
Asserts if there are conflicts.
Definition: Composer.php:137
Plugin class containing helper functions for plugin activation/deactivation, dependency checking capa...
Definition: ElggPlugin.php:16
if(!$user||!$user->canDelete()) $name
Definition: delete.php:22
elgg_get_plugin_from_id(string $plugin_id)
Elgg plugins library Contains functions for managing plugins.
Definition: plugins.php:15
Indicates plugin php extension requirement issues.
$version
elgg_echo(string $message_key, array $args=[], string $language= '')
Elgg language module Functions to manage language and translations.
Definition: languages.php:17
getConfiguration()
Returns the composer configuration.
Definition: Composer.php:56
getFriendlyCategory($category)
Returns a category&#39;s friendly name.
Definition: Composer.php:259
$plugin_id
Remove all user and plugin settings from the give plugin ID.
Definition: remove.php:8
Indicates invalid php version for a plugin.
Exception thrown if a value does not match with a set of values.
elgg_is_active_plugin(string $plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:43
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
__construct(\ElggPlugin $plugin)
Constructor.
Definition: Composer.php:36
assertActivePluginConflicts()
Asserts if there are active plugins that conflict with the current plugin.
Definition: Composer.php:170
Holds plugin composer.json related functions.
Definition: Composer.php:17
getCategories()
Returns an array with categories.
Definition: Composer.php:91
assertPluginId()
Asserts if plugin id matches project name.
Definition: Composer.php:66
checkConstraints($version, $constraints)
Determine if given version satisfies given constraints.
Definition: Composer.php:195
_elgg_services()
Get the global service provider.
Definition: elgglib.php:346
assertRequiredPhpExtensions()
Asserts if the required php extensions matches the actual installed extensions.
Definition: Composer.php:230
assertRequiredPhpVersion()
Asserts if the required php version matches the actual php version.
Definition: Composer.php:212
Indicates a mismatch between the plugin ID in the composer.json and the plugin directory.
Indicates a conflict with the plugin.
Indicates something wrong with the plugin composer.json.
getConflicts()
Returns an array of projectnames with their conflicting version.
Definition: Composer.php:117
$extension
Definition: default.php:25
getLicense()
Returns the license.
Definition: Composer.php:77