SocialApiImplementerInstaller.php in Social API 8.2
File
src/Utility/SocialApiImplementerInstaller.php
View source
<?php
namespace Drupal\social_api\Utility;
class SocialApiImplementerInstaller {
public static function checkLibrary($machine_name, $name, $library, $min_version, $max_version) {
$sdk_found = FALSE;
$requirements = [];
$json = self::readPackages();
if ($json) {
foreach ($json as $package) {
if ($package['name'] == $library) {
$sdk_found = TRUE;
if ($package['version_normalized'] < $min_version || $package['version_normalized'] > $max_version) {
$requirements[$machine_name] = [
'description' => t("@name could not be installed because an incompatible version of @library was detected. Please read the installation instructions.", [
'@name' => $name,
'@library' => $library,
]),
'severity' => REQUIREMENT_ERROR,
];
}
break;
}
}
if (!$sdk_found) {
$requirements[$machine_name] = [
'description' => t("@name could not be installed because @library was not found. @name must be installed using Composer. Please read the installation instructions.", [
'@name' => $name,
'@library' => $library,
]),
'severity' => REQUIREMENT_ERROR,
];
}
}
else {
$requirements[$machine_name] = [
'description' => t('@name could not be installed: installed.json could not be read.', [
'@name' => $name,
]),
'severity' => REQUIREMENT_ERROR,
];
}
return $requirements;
}
protected static function readPackages() {
$base_dir = is_dir(DRUPAL_ROOT . '/vendor') ? DRUPAL_ROOT : dirname(DRUPAL_ROOT);
$file_uri = $base_dir . '/vendor/composer/installed.json';
if (file_exists($file_uri)) {
$filedata = file_get_contents($file_uri);
if ($filedata) {
$json = json_decode($filedata, TRUE);
if ($json !== NULL) {
return $json;
}
}
}
return FALSE;
}
}