drupalorg-1.0.x-dev/src/Traits/UpdateXML.php
src/Traits/UpdateXML.php
<?php
namespace Drupal\drupalorg\Traits;
use Drupal\Component\Serialization\Json;
/**
* Helper function to obtain project information via updates.drupal.org.
*/
trait UpdateXML {
/**
* Get project information from a project from updates.drupal.org.
*
* @param string $project
* The drupal.org project name.
*
* @return array
* The response object.
*
* @see https://www.drupal.org/drupalorg/docs/apis/update-status-xml
*/
protected static function getProjectInformation(string $project): array {
$cache = \Drupal::cache();
$cache_key = 'drupalorg:update_xml:' . $project;
if ($result = $cache->get($cache_key)) {
return $result->data;
}
$result = [];
$url = 'https://updates.drupal.org/release-history/' . $project . '/current';
try {
$response = \Drupal::httpClient()->request('GET', $url);
if ($response && $response->getStatusCode() == 200) {
$body = $response->getBody()->getContents();
if (!str_contains($body, 'No release history available')) {
$xml = \simplexml_load_string($body);
$result = Json::decode(Json::encode($xml));
}
}
}
catch (\Throwable $e) {
\Drupal::logger('drupalorg')
->warning('Could not fetch releases for @project.', [
'@project' => $project,
]);
}
$cache->set($cache_key, $result);
return $result;
}
}
