Add Platform-PHP version to user-agent header to improve targetted-php stats (#9873)

main
Jordi Boggiano 3 years ago committed by GitHub
parent c4ed9f5566
commit 9ba042ded8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -33,6 +33,11 @@ class PlatformRepository extends ArrayRepository
{
const PLATFORM_PACKAGE_REGEX = '{^(?:php(?:-64bit|-ipv6|-zts|-debug)?|hhvm|(?:ext|lib)-[a-z0-9](?:[_.-]?[a-z0-9]+)*|composer-(?:plugin|runtime)-api)$}iD';
/**
* @var ?string
*/
private static $lastSeenPlatformPhp = null;
/**
* @var VersionParser
*/
@ -526,6 +531,10 @@ class PlatformRepository extends ArrayRepository
$package->setExtra(array('config.platform' => true));
parent::addPackage($package);
if ($package->getName() === 'php') {
self::$lastSeenPlatformPhp = implode('.', array_slice(explode('.', $package->getVersion()), 0, 3));
}
return $package;
}
@ -620,4 +629,18 @@ class PlatformRepository extends ArrayRepository
return $cache[$name] = (bool) preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name);
}
/**
* Returns the last seen config.platform.php version if defined
*
* This is a best effort attempt for internal purposes, retrieve the real
* packages from a PlatformRepository instance if you need a version guaranteed to
* be correct.
*
* @internal
*/
public static function getPlatformPhpVersion()
{
return self::$lastSeenPlatformPhp;
}
}

@ -15,6 +15,7 @@ namespace Composer\Util;
use Composer\Composer;
use Composer\CaBundle\CaBundle;
use Composer\Downloader\TransportException;
use Composer\Repository\PlatformRepository;
use Composer\Util\Http\ProxyManager;
use Psr\Log\LoggerInterface;
@ -112,13 +113,15 @@ final class StreamContextFactory
}
if (!isset($options['http']['header']) || false === stripos(implode('', $options['http']['header']), 'user-agent')) {
$platformPhpVersion = PlatformRepository::getPlatformPhpVersion();
$options['http']['header'][] = sprintf(
'User-Agent: Composer/%s (%s; %s; %s; %s%s)',
'User-Agent: Composer/%s (%s; %s; %s; %s%s%s)',
Composer::getVersion(),
function_exists('php_uname') ? php_uname('s') : 'Unknown',
function_exists('php_uname') ? php_uname('r') : 'Unknown',
$phpVersion,
$httpVersion,
$platformPhpVersion ? '; Platform-PHP '.$platformPhpVersion : '',
getenv('CI') ? '; CI' : ''
);
}

Loading…
Cancel
Save