Elgg  Version master
Composer.php
Go to the documentation of this file.
1 <?php
2 namespace Elgg\Plugin;
3 
10 
17 class Composer {
18 
22  protected $configuration;
23 
31  public function __construct(protected \ElggPlugin $plugin) {
32  try {
33  // need to suppress warning because of deprecated notices that get converted to warnings during phpunit
34  $reader = @new \Eloquent\Composer\Configuration\ConfigurationReader;
35  $this->configuration = $reader->read($this->plugin->getPath() . 'composer.json');
36  } catch (\Exception $e) {
37  throw new ComposerException(elgg_echo('ElggPlugin:NoPluginComposer', [
38  $this->plugin->getID(),
39  $this->plugin->guid,
40  ]));
41  }
42  }
43 
49  public function getConfiguration() {
50  return $this->configuration;
51  }
52 
59  public function assertPluginId() {
60  if ($this->configuration->projectName() !== $this->plugin->getID()) {
61  throw new IdMismatchException(elgg_echo('ElggPlugin:IdMismatch', [$this->configuration->projectName()]));
62  }
63  }
64 
70  public function getLicense() {
71  $license = $this->configuration->license();
72  if (!empty($license)) {
73  $license = implode(', ', $license);
74  }
75 
76  return (string) $license;
77  }
78 
84  public function getCategories() {
85  $cats = $this->configuration->keywords() ?: [];
86 
87  $result = [];
88  foreach ($cats as $cat) {
89  $result[strtolower($cat)] = $this->getFriendlyCategory($cat);
90  }
91 
92  // plugins often set Elgg in their keywords, we do not need that keyword
93  unset($result['elgg']);
94  unset($result['plugin']);
95 
96  // add vendor to categories
97  $vendor = strtolower((string) $this->configuration->vendorName());
98  if (!isset($result[$vendor])) {
99  $result[$vendor] = $this->getFriendlyCategory($vendor);
100  }
101 
102  return $result;
103  }
104 
110  public function getConflicts() {
111  $conflicts = $this->configuration->conflict();
112 
113  $result = [];
114  foreach ($conflicts as $name => $version) {
115  list(,$projectname) = explode('/', $name);
116  if (!empty($projectname)) {
117  $result[$projectname] = $version;
118  }
119  }
120 
121  return $result;
122  }
123 
130  public function assertConflicts() {
131  $conflicts = $this->getConflicts();
132  if (empty($conflicts)) {
133  return;
134  }
135 
136  if (isset($conflicts['elgg'])) {
137  if ($this->checkConstraints(elgg_get_release(), $conflicts['elgg'])) {
138  throw new ConflictException('Elgg version: ' . elgg_get_release() . ' conflicts with constraint '. $conflicts['elgg']);
139  }
140 
141  unset($conflicts['elgg']);
142  }
143 
144  foreach ($conflicts as $plugin_id => $constraints) {
146  continue;
147  }
148 
150 
151  if ($this->checkConstraints($plugin->getVersion(), $constraints)) {
152  throw new ConflictException("Plugin [{$plugin->getID()}] with version: {$plugin->getVersion()} conflicts with constraint {$constraints}");
153  }
154  }
155  }
156 
163  public function assertActivePluginConflicts() {
164  $active_plugins = elgg_get_plugins('active');
165  foreach ($active_plugins as $plugin) {
166  $conflicts = $plugin->getConflicts();
167  if (!isset($conflicts[$this->plugin->getID()])) {
168  continue;
169  }
170 
171  $constraint = $conflicts[$this->plugin->getID()];
172  if ($this->checkConstraints($this->plugin->getVersion(), $constraint)) {
173  $msg = 'The plugin ' . $this->plugin->getDisplayName() . ' with version ' . $this->plugin->getVersion();
174  $msg .= ' conflicts with constraint '. $constraint . ' defined in ' . $plugin->getID();
175  throw new ConflictException($msg);
176  }
177  }
178  }
179 
188  public function checkConstraints($version, $constraints) {
189  try {
190  return Semver::satisfies($version, $constraints);
191  } catch (\UnexpectedValueException $e) {
192  // something is wrong with the version number
193  elgg_log($e, 'ERROR');
194  }
195 
196  return false;
197  }
198 
205  public function assertRequiredPhpVersion() {
206  $requirements = $this->configuration->dependencies();
207  if (!isset($requirements['php'])) {
208  return;
209  }
210 
211  $php_version = phpversion();
212  if (!$this->checkConstraints($php_version, $requirements['php'])) {
213  throw new PhpVersionException("The PHP version ({$php_version}) does not meet the plugin [{$this->plugin->getID()}] requirements of {$requirements['php']}");
214  }
215  }
216 
223  public function assertRequiredPhpExtensions() {
224  $requirements = $this->configuration->dependencies();
225  foreach ($requirements as $name => $constraint) {
226  if (!str_starts_with($name, 'ext-')) {
227  continue;
228  }
229 
230  $extension = substr($name, 4);
231  if (!extension_loaded($extension)) {
232  throw new PhpExtensionException("Plugin [{$this->plugin->getID()}] requires the PHP extensions {$extension}");
233  }
234 
235  $extension_version = phpversion($extension);
236  if (!$this->checkConstraints($extension_version, $constraint)) {
237  throw new PhpExtensionException("The PHP extension version ({$extension_version}) does not meet the plugin [{$this->plugin->getID()}] requirements of {$constraint}");
238  }
239  }
240  }
241 
252  protected function getFriendlyCategory($category) {
253  $cat_raw_string = strtolower("admin:plugins:category:{$category}");
254  if (_elgg_services()->translator->languageKeyExists($cat_raw_string)) {
255  return elgg_echo($cat_raw_string);
256  }
257 
258  return ucwords(str_replace(['-', '_'], ' ', $category));
259  }
260 }
elgg_get_release()
Get the current Elgg release.
elgg_get_plugins(string $status= 'active')
Returns an ordered list of plugins.
Definition: plugins.php:55
$plugin
assertConflicts()
Asserts if there are conflicts.
Definition: Composer.php:130
Plugin class containing helper functions for plugin activation/deactivation, dependency checking capa...
Definition: ElggPlugin.php:17
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:49
getFriendlyCategory($category)
Returns a category&#39;s friendly name.
Definition: Composer.php:252
$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
__construct(protected\ElggPlugin $plugin)
Constructor.
Definition: Composer.php:31
elgg_log($message, $level=\Psr\Log\LogLevel::NOTICE)
Log a message.
Definition: elgglib.php:86
assertActivePluginConflicts()
Asserts if there are active plugins that conflict with the current plugin.
Definition: Composer.php:163
Holds plugin composer.json related functions.
Definition: Composer.php:17
getCategories()
Returns an array with categories.
Definition: Composer.php:84
assertPluginId()
Asserts if plugin id matches project name.
Definition: Composer.php:59
checkConstraints($version, $constraints)
Determine if given version satisfies given constraints.
Definition: Composer.php:188
_elgg_services()
Get the global service provider.
Definition: elgglib.php:351
assertRequiredPhpExtensions()
Asserts if the required php extensions matches the actual installed extensions.
Definition: Composer.php:223
assertRequiredPhpVersion()
Asserts if the required php version matches the actual php version.
Definition: Composer.php:205
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:110
$extension
Definition: default.php:25
getLicense()
Returns the license.
Definition: Composer.php:70