Add support for warnings/infos keys to report more complex information to diverse Composer versions
parent
ebf4cbdc69
commit
7e3626362e
|
@ -448,11 +448,12 @@ class HttpDownloader
|
||||||
* @internal
|
* @internal
|
||||||
*
|
*
|
||||||
* @param string $url
|
* @param string $url
|
||||||
* @param array{warning?: string, info?: string, warning-versions?: string, info-versions?: string} $data
|
* @param array{warning?: string, info?: string, warning-versions?: string, info-versions?: string, warnings?: array<array{versions: string, message: string}>, infos?: array<array{versions: string, message: string}>} $data
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
public static function outputWarnings(IOInterface $io, $url, $data)
|
public static function outputWarnings(IOInterface $io, $url, $data)
|
||||||
{
|
{
|
||||||
|
// legacy warning/info keys
|
||||||
foreach (array('warning', 'info') as $type) {
|
foreach (array('warning', 'info') as $type) {
|
||||||
if (empty($data[$type])) {
|
if (empty($data[$type])) {
|
||||||
continue;
|
continue;
|
||||||
|
@ -469,6 +470,25 @@ class HttpDownloader
|
||||||
|
|
||||||
$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$data[$type].'</'.$type.'>');
|
$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$data[$type].'</'.$type.'>');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// modern Composer 2.2+ format with support for multiple warning/info messages
|
||||||
|
foreach (array('warnings', 'infos') as $key) {
|
||||||
|
if (empty($data[$key])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$versionParser = new VersionParser();
|
||||||
|
foreach ($data[$key] as $spec) {
|
||||||
|
$type = substr($key, 0, -1);
|
||||||
|
$constraint = $versionParser->parseConstraints($spec['versions']);
|
||||||
|
$composer = new Constraint('==', $versionParser->normalize(Composer::getVersion()));
|
||||||
|
if (!$constraint->matches($composer)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
$io->writeError('<'.$type.'>'.ucfirst($type).' from '.Url::sanitize($url).': '.$spec['message'].'</'.$type.'>');
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -12,6 +12,7 @@
|
||||||
|
|
||||||
namespace Composer\Test\Util;
|
namespace Composer\Test\Util;
|
||||||
|
|
||||||
|
use Composer\IO\BufferIO;
|
||||||
use Composer\Util\HttpDownloader;
|
use Composer\Util\HttpDownloader;
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
|
@ -51,4 +52,32 @@ class HttpDownloaderTest extends TestCase
|
||||||
$this->assertNotEquals(200, $e->getCode());
|
$this->assertNotEquals(200, $e->getCode());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function testOutputWarnings()
|
||||||
|
{
|
||||||
|
$io = new BufferIO();
|
||||||
|
HttpDownloader::outputWarnings($io, '$URL', array());
|
||||||
|
$this->assertSame('', $io->getOutput());
|
||||||
|
HttpDownloader::outputWarnings($io, '$URL', array(
|
||||||
|
'warning' => 'old warning msg',
|
||||||
|
'warning-versions' => '>=2.0',
|
||||||
|
'info' => 'old info msg',
|
||||||
|
'info-versions' => '>=2.0',
|
||||||
|
'warnings' => array(
|
||||||
|
array('message' => 'should not appear', 'versions' => '<2.2'),
|
||||||
|
array('message' => 'visible warning', 'versions' => '>=2.2-dev'),
|
||||||
|
),
|
||||||
|
'infos' => array(
|
||||||
|
array('message' => 'should not appear', 'versions' => '<2.2'),
|
||||||
|
array('message' => 'visible info', 'versions' => '>=2.2-dev'),
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
|
// the <info> tag are consumed by the OutputFormatter, but not <warning> as that is not a default output format
|
||||||
|
$this->assertSame('<warning>Warning from $URL: old warning msg</warning>
|
||||||
|
Info from $URL: old info msg
|
||||||
|
<warning>Warning from $URL: visible warning</warning>
|
||||||
|
Info from $URL: visible info
|
||||||
|
', $io->getOutput());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue