1
0
Fork 0

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

pull/9897/head
Jordi Boggiano 2021-05-20 10:17:30 +02:00 committed by GitHub
parent c4ed9f5566
commit 9ba042ded8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 27 additions and 1 deletions

View File

@ -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'; 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 * @var VersionParser
*/ */
@ -526,6 +531,10 @@ class PlatformRepository extends ArrayRepository
$package->setExtra(array('config.platform' => true)); $package->setExtra(array('config.platform' => true));
parent::addPackage($package); parent::addPackage($package);
if ($package->getName() === 'php') {
self::$lastSeenPlatformPhp = implode('.', array_slice(explode('.', $package->getVersion()), 0, 3));
}
return $package; return $package;
} }
@ -620,4 +629,18 @@ class PlatformRepository extends ArrayRepository
return $cache[$name] = (bool) preg_match(PlatformRepository::PLATFORM_PACKAGE_REGEX, $name); 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;
}
} }

View File

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