comparison core/modules/update/src/UpdateFetcher.php @ 0:c75dbcec494b

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