From da94e4b6199b7511704633f31e2e7bf8410be3d7 Mon Sep 17 00:00:00 2001 From: Ahammar Yassine Date: Wed, 31 Oct 2018 16:32:02 +0000 Subject: [PATCH] Skip all network-based checks (#7641) * Skip all network-based checks Change the warnings in diagnose to a friendly messages when allow_url_fopen is disabled. Issue: #7622 --- src/Composer/Command/DiagnoseCommand.php | 65 ++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 4 deletions(-) diff --git a/src/Composer/Command/DiagnoseCommand.php b/src/Composer/Command/DiagnoseCommand.php index 11d1e1aa0..893bd5d52 100644 --- a/src/Composer/Command/DiagnoseCommand.php +++ b/src/Composer/Command/DiagnoseCommand.php @@ -119,8 +119,9 @@ EOT $io->write('Checking github.com rate limit: ', false); try { $rate = $this->getGithubRateLimit('github.com'); - $this->outputResult(true); - if (10 > $rate['remaining']) { + if (!is_array($rate)) { + $this->outputResult($rate); + } elseif (10 > $rate['remaining']) { $io->writeError('WARNING'); $io->writeError(sprintf( 'Github has a rate limit on their API. ' @@ -131,6 +132,8 @@ EOT $rate['remaining'], $rate['limit'] )); + } else { + $this->outputResult(true); } } catch (\Exception $e) { if ($e instanceof TransportException && $e->getCode() === 401) { @@ -207,6 +210,11 @@ EOT private function checkHttp($proto, Config $config) { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + $disableTls = false; $result = array(); if ($proto === 'https' && $config->get('disable-tls') === true) { @@ -238,6 +246,11 @@ EOT private function checkHttpProxy() { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + $protocol = extension_loaded('openssl') ? 'https' : 'http'; try { $json = json_decode($this->rfs->getContents('packagist.org', $protocol . '://repo.packagist.org/packages.json', false), true); @@ -265,6 +278,11 @@ EOT */ private function checkHttpProxyFullUriRequestParam() { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + $url = 'http://repo.packagist.org/packages.json'; try { $this->rfs->getContents('packagist.org', $url, false); @@ -290,6 +308,11 @@ EOT */ private function checkHttpsProxyFullUriRequestParam() { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + if (!extension_loaded('openssl')) { return 'You need the openssl extension installed for this check'; } @@ -312,6 +335,11 @@ EOT private function checkGithubOauth($domain, $token) { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); try { $url = $domain === 'github.com' ? 'https://api.'.$domain.'/' : 'https://'.$domain.'/api/v3/'; @@ -332,10 +360,15 @@ EOT * @param string $domain * @param string $token * @throws TransportException - * @return array + * @return array|string */ private function getGithubRateLimit($domain, $token = null) { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + if ($token) { $this->getIO()->setAuthentication($domain, $token, 'x-oauth-basic'); } @@ -390,6 +423,11 @@ EOT private function checkVersion($config) { + $result = $this->checkConnectivity(); + if ($result !== true) { + return $result; + } + $versionsUtil = new Versions($config, $this->rfs); $latest = $versionsUtil->getLatest(); @@ -413,6 +451,7 @@ EOT } $hadError = false; + $hadWarning = false; if ($result instanceof \Exception) { $result = '['.get_class($result).'] '.$result->getMessage().''; } @@ -427,6 +466,8 @@ EOT foreach ($result as $message) { if (false !== strpos($message, '')) { $hadError = true; + } elseif (false !== strpos($message, '')) { + $hadWarning = true; } } } @@ -434,7 +475,7 @@ EOT if ($hadError) { $io->write('FAIL'); $this->exitCode = 2; - } else { + } elseif ($hadWarning) { $io->write('WARNING'); $this->exitCode = 1; } @@ -668,4 +709,20 @@ EOT return !$warnings && !$errors ? true : $output; } + + + /** + * Check if allow_url_fopen is ON + * + * @return bool|string + */ + private function checkConnectivity() + { + if (!ini_get('allow_url_fopen')) { + $result = 'Skipped because allow_url_fopen is missing.'; + return $result; + } + + return true; + } }