annotate core/modules/update/src/UpdateFetcher.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\update;
Chris@0 4
Chris@0 5 use Drupal\Core\Config\ConfigFactoryInterface;
Chris@0 6 use Drupal\Core\DependencyInjection\DependencySerializationTrait;
Chris@0 7 use GuzzleHttp\ClientInterface;
Chris@0 8 use GuzzleHttp\Exception\RequestException;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Fetches project information from remote locations.
Chris@0 12 */
Chris@0 13 class UpdateFetcher implements UpdateFetcherInterface {
Chris@0 14
Chris@0 15 use DependencySerializationTrait;
Chris@0 16
Chris@0 17 /**
Chris@0 18 * URL to check for updates, if a given project doesn't define its own.
Chris@0 19 */
Chris@0 20 const UPDATE_DEFAULT_URL = 'http://updates.drupal.org/release-history';
Chris@0 21
Chris@0 22 /**
Chris@0 23 * The fetch url configured in the update settings.
Chris@0 24 *
Chris@0 25 * @var string
Chris@0 26 */
Chris@0 27 protected $fetchUrl;
Chris@0 28
Chris@0 29 /**
Chris@0 30 * The update settings
Chris@0 31 *
Chris@0 32 * @var \Drupal\Core\Config\Config
Chris@0 33 */
Chris@0 34 protected $updateSettings;
Chris@0 35
Chris@0 36 /**
Chris@0 37 * The HTTP client to fetch the feed data with.
Chris@0 38 *
Chris@0 39 * @var \GuzzleHttp\ClientInterface
Chris@0 40 */
Chris@0 41 protected $httpClient;
Chris@0 42
Chris@0 43 /**
Chris@0 44 * Constructs a UpdateFetcher.
Chris@0 45 *
Chris@0 46 * @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
Chris@0 47 * The config factory.
Chris@0 48 * @param \GuzzleHttp\ClientInterface $http_client
Chris@0 49 * A Guzzle client object.
Chris@0 50 */
Chris@0 51 public function __construct(ConfigFactoryInterface $config_factory, ClientInterface $http_client) {
Chris@0 52 $this->fetchUrl = $config_factory->get('update.settings')->get('fetch.url');
Chris@0 53 $this->httpClient = $http_client;
Chris@0 54 $this->updateSettings = $config_factory->get('update.settings');
Chris@0 55 }
Chris@0 56
Chris@0 57 /**
Chris@0 58 * {@inheritdoc}
Chris@0 59 */
Chris@0 60 public function fetchProjectData(array $project, $site_key = '') {
Chris@0 61 $url = $this->buildFetchUrl($project, $site_key);
Chris@0 62 $data = '';
Chris@0 63 try {
Chris@0 64 $data = (string) $this->httpClient
Chris@0 65 ->get($url, ['headers' => ['Accept' => 'text/xml']])
Chris@0 66 ->getBody();
Chris@0 67 }
Chris@0 68 catch (RequestException $exception) {
Chris@0 69 watchdog_exception('update', $exception);
Chris@0 70 }
Chris@0 71 return $data;
Chris@0 72 }
Chris@0 73
Chris@0 74 /**
Chris@0 75 * {@inheritdoc}
Chris@0 76 */
Chris@0 77 public function buildFetchUrl(array $project, $site_key = '') {
Chris@0 78 $name = $project['name'];
Chris@0 79 $url = $this->getFetchBaseUrl($project);
Chris@0 80 $url .= '/' . $name . '/' . \Drupal::CORE_COMPATIBILITY;
Chris@0 81
Chris@0 82 // Only append usage information if we have a site key and the project is
Chris@0 83 // enabled. We do not want to record usage statistics for disabled projects.
Chris@0 84 if (!empty($site_key) && (strpos($project['project_type'], 'disabled') === FALSE)) {
Chris@0 85 // Append the site key.
Chris@0 86 $url .= (strpos($url, '?') !== FALSE) ? '&' : '?';
Chris@0 87 $url .= 'site_key=';
Chris@0 88 $url .= rawurlencode($site_key);
Chris@0 89
Chris@0 90 // Append the version.
Chris@0 91 if (!empty($project['info']['version'])) {
Chris@0 92 $url .= '&version=';
Chris@0 93 $url .= rawurlencode($project['info']['version']);
Chris@0 94 }
Chris@0 95
Chris@0 96 // Append the list of modules or themes enabled.
Chris@0 97 $list = array_keys($project['includes']);
Chris@0 98 $url .= '&list=';
Chris@0 99 $url .= rawurlencode(implode(',', $list));
Chris@0 100 }
Chris@0 101 return $url;
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * {@inheritdoc}
Chris@0 106 */
Chris@0 107 public function getFetchBaseUrl($project) {
Chris@0 108 if (isset($project['info']['project status url'])) {
Chris@0 109 $url = $project['info']['project status url'];
Chris@0 110 }
Chris@0 111 else {
Chris@0 112 $url = $this->fetchUrl;
Chris@0 113 if (empty($url)) {
Chris@0 114 $url = static::UPDATE_DEFAULT_URL;
Chris@0 115 }
Chris@0 116 }
Chris@0 117 return $url;
Chris@0 118 }
Chris@0 119
Chris@0 120 }