Elgg  Version 4.3
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  unset($conflicts['elgg']);
148  }
149 
150  foreach ($conflicts as $plugin_id => $constraints) {
152  continue;
153  }
154 
156 
157  if ($this->checkConstraints($plugin->getVersion(), $constraints)) {
158  throw new ConflictException("Plugin [{$plugin->getID()}] with version: {$plugin->getVersion()} conflicts with constraint {$constraints}");
159  }
160  }
161  }
162 
169  public function assertActivePluginConflicts() {
170  $active_plugins = elgg_get_plugins('active');
171  foreach ($active_plugins as $plugin) {
172  $conflicts = $plugin->getConflicts();
173  if (!isset($conflicts[$this->plugin->getID()])) {
174  continue;
175  }
176 
177  $constraint = $conflicts[$this->plugin->getID()];
178  if ($this->checkConstraints($this->plugin->getVersion(), $constraint)) {
179  $msg = 'The plugin ' . $this->plugin->getDisplayName() . ' with version ' . $this->plugin->getVersion();
180  $msg .= ' conflicts with constraint '. $constraint . ' defined in ' . $plugin->getID();
181  throw new ConflictException($msg);
182  }
183  }
184  }
185 
194  public function checkConstraints($version, $constraints) {
195  return Semver::satisfies($version, $constraints);
196  }
197 
204  public function assertRequiredPhpVersion() {
205  $requirements = $this->configuration->dependencies();
206  if (!isset($requirements['php'])) {
207  return;
208  }
209 
210  $php_version = phpversion();
211  if (!$this->checkConstraints($php_version, $requirements['php'])) {
212  throw new PhpVersionException("The PHP version ({$php_version}) does not meet the plugin [{$this->plugin->getID()}] requirements of {$requirements['php']}");
213  }
214  }
215 
222  public function assertRequiredPhpExtensions() {
223  $requirements = $this->configuration->dependencies();
224  foreach ($requirements as $name => $constraint) {
225  if (strpos($name, 'ext-') !== 0) {
226  continue;
227  }
228 
229  $extension = substr($name, 4);
230  if (!extension_loaded($extension)) {
231  throw new PhpExtensionException("Plugin [{$this->plugin->getID()}] requires the PHP extensions {$extension}");
232  }
233 
234  $extension_version = phpversion($extension);
235  if (!$this->checkConstraints($extension_version, $constraint)) {
236  throw new PhpExtensionException("The PHP extension version ({$extension_version}) does not meet the plugin [{$this->plugin->getID()}] requirements of {$constraint}");
237  }
238  }
239  }
240 
251  protected function getFriendlyCategory($category) {
252  $cat_raw_string = strtolower("admin:plugins:category:{$category}");
253  if (_elgg_services()->translator->languageKeyExists($cat_raw_string)) {
254  return elgg_echo($cat_raw_string);
255  }
256 
257  return ucwords(str_replace(['-', '_'], ' ', $category));
258  }
259 }
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
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
getConfiguration()
Returns the composer configuration.
Definition: Composer.php:56
getFriendlyCategory($category)
Returns a category&#39;s friendly name.
Definition: Composer.php:251
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
Indicates invalid php version for a plugin.
elgg_is_active_plugin(string $plugin_id)
Returns if a plugin is active for a current site.
Definition: plugins.php:43
__construct(\ElggPlugin $plugin)
Constructor.
Definition: Composer.php:36
assertActivePluginConflicts()
Asserts if there are active plugins that conflict with the current plugin.
Definition: Composer.php:169
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:194
_elgg_services()
Get the global service provider.
Definition: elgglib.php:638
assertRequiredPhpExtensions()
Asserts if the required php extensions matches the actual installed extensions.
Definition: Composer.php:222
assertRequiredPhpVersion()
Asserts if the required php version matches the actual php version.
Definition: Composer.php:204
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