UpdateFetcher.php in Drupal 10
File
core/modules/update/src/UpdateFetcher.php
View source
<?php
namespace Drupal\update;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\DependencyInjection\DependencySerializationTrait;
use Drupal\Core\Site\Settings;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\TransferException;
class UpdateFetcher implements UpdateFetcherInterface {
use DependencySerializationTrait;
const UPDATE_DEFAULT_URL = 'https://updates.drupal.org/release-history';
protected $fetchUrl;
protected $updateSettings;
protected $httpClient;
protected $withHttpFallback;
public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client, Settings $settings) {
$this->fetchUrl = $config_factory
->get('update.settings')
->get('fetch.url');
$this->httpClient = $http_client;
$this->updateSettings = $config_factory
->get('update.settings');
$this->withHttpFallback = $settings
->get('update_fetch_with_http_fallback', FALSE);
}
public function fetchProjectData(array $project, $site_key = '') {
$url = $this
->buildFetchUrl($project, $site_key);
return $this
->doRequest($url, [
'headers' => [
'Accept' => 'text/xml',
],
], $this->withHttpFallback);
}
protected function doRequest(string $url, array $options, bool $with_http_fallback) : string {
$data = '';
try {
$data = (string) $this->httpClient
->get($url, [
'headers' => [
'Accept' => 'text/xml',
],
])
->getBody();
} catch (TransferException $exception) {
watchdog_exception('update', $exception);
if ($with_http_fallback && strpos($url, "http://") === FALSE) {
$url = str_replace('https://', 'http://', $url);
return $this
->doRequest($url, $options, FALSE);
}
}
return $data;
}
public function buildFetchUrl(array $project, $site_key = '') {
$name = $project['name'];
$url = $this
->getFetchBaseUrl($project);
$url .= '/' . $name . '/current';
if (!empty($site_key) && strpos($project['project_type'], 'disabled') === FALSE) {
$url .= strpos($url, '?') !== FALSE ? '&' : '?';
$url .= 'site_key=';
$url .= rawurlencode($site_key);
if (!empty($project['info']['version'])) {
$url .= '&version=';
$url .= rawurlencode($project['info']['version']);
}
$list = array_keys($project['includes']);
$url .= '&list=';
$url .= rawurlencode(implode(',', $list));
}
return $url;
}
public function getFetchBaseUrl($project) {
if (isset($project['info']['project status url'])) {
$url = $project['info']['project status url'];
}
else {
$url = $this->fetchUrl;
if (empty($url)) {
$url = static::UPDATE_DEFAULT_URL;
}
}
return $url;
}
}