Merge remote-tracking branch 'johnikx/ambiguous-reference-warning'

main
Jordi Boggiano 10 years ago
commit c3ab09750d

@ -15,6 +15,7 @@ namespace Composer\Autoload;
use Composer\Config;
use Composer\EventDispatcher\EventDispatcher;
use Composer\Installer\InstallationManager;
use Composer\IO\IOInterface;
use Composer\Package\AliasPackage;
use Composer\Package\PackageInterface;
use Composer\Repository\InstalledRepositoryInterface;
@ -32,11 +33,17 @@ class AutoloadGenerator
*/
private $eventDispatcher;
/**
* @var IOInterface
*/
private $io;
private $devMode = false;
public function __construct(EventDispatcher $eventDispatcher)
public function __construct(EventDispatcher $eventDispatcher, IOInterface $io=null)
{
$this->eventDispatcher = $eventDispatcher;
$this->io = $io;
}
public function setDevMode($devMode = true)
@ -197,6 +204,12 @@ EOF;
}
}
if ($this->io && count(ClassMapGenerator::$ambiguousReferences) > 0) {
foreach (ClassMapGenerator::$ambiguousReferences as $ambiguousReference) {
$this->io->write('<info>Warning: Ambiguous class "'.$ambiguousReference['class'].'" resolution; defined in "'.$ambiguousReference[0].'" and in "'.$ambiguousReference[1].'" files.</info>');
}
}
ksort($classMap);
foreach ($classMap as $class => $code) {
$classmapFile .= ' '.var_export($class, true).' => '.$code;

@ -21,6 +21,12 @@ use Symfony\Component\Finder\Finder;
*/
class ClassMapGenerator
{
/**
* @var array
*/
public static $ambiguousReferences = array();
/**
* Generate a class map file
*
@ -79,6 +85,14 @@ class ClassMapGenerator
$classes = self::findClasses($filePath);
foreach ($classes as $class) {
if (array_key_exists($class, $map)) {
self::$ambiguousReferences[] = array(
'class' => $class,
'0' => $map[$class],
'1' => $filePath
);
}
$map[$class] = $filePath;
}
}

@ -253,7 +253,7 @@ class Factory
$composer->setEventDispatcher($dispatcher);
// initialize autoload generator
$generator = new AutoloadGenerator($dispatcher);
$generator = new AutoloadGenerator($dispatcher, $io);
$composer->setAutoloadGenerator($generator);
// add installers to the manager

@ -12,6 +12,7 @@
namespace Composer\Test\Autoload;
use Composer\Autoload\ClassMapGenerator;
use Symfony\Component\Finder\Finder;
class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
{
@ -78,11 +79,9 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
public function testCreateMapFinderSupport()
{
if (!class_exists('Symfony\\Component\\Finder\\Finder')) {
$this->markTestSkipped('Finder component is not available');
}
$this->checkIfFinderIsAvailable();
$finder = new \Symfony\Component\Finder\Finder();
$finder = new Finder();
$finder->files()->in(__DIR__ . '/Fixtures/beta/NamespaceCollision');
$this->assertEqualsNormalized(array(
@ -103,6 +102,18 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
$find->invoke(null, __DIR__.'/no-file');
}
public function testAmbiguousReference()
{
$this->checkIfFinderIsAvailable();
$finder = new Finder();
$finder->files()->in(__DIR__ . '/Fixtures/Ambiguous');
ClassMapGenerator::createMap($finder);
$this->assertEquals(1, count(ClassMapGenerator::$ambiguousReferences));
$this->assertEquals('A', ClassMapGenerator::$ambiguousReferences[0]['class']);
}
/**
* @expectedException \RuntimeException
@ -123,4 +134,11 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
}
$this->assertEquals($expected, $actual, $message);
}
private function checkIfFinderIsAvailable()
{
if (!class_exists('Symfony\\Component\\Finder\\Finder')) {
$this->markTestSkipped('Finder component is not available');
}
}
}

Loading…
Cancel
Save