Refactor to avoid global static state

main
Jordi Boggiano 10 years ago
parent c3ab09750d
commit 781c97fa9b

@ -40,7 +40,7 @@ class AutoloadGenerator
private $devMode = false;
public function __construct(EventDispatcher $eventDispatcher, IOInterface $io=null)
public function __construct(EventDispatcher $eventDispatcher, IOInterface $io = null)
{
$this->eventDispatcher = $eventDispatcher;
$this->io = $io;
@ -184,7 +184,7 @@ EOF;
preg_quote($dir),
($psrType === 'psr-0' && strpos($namespace, '_') === false) ? preg_quote(strtr($namespace, '\\', '/')) : ''
);
foreach (ClassMapGenerator::createMap($dir, $whitelist) as $class => $path) {
foreach (ClassMapGenerator::createMap($dir, $whitelist, $this->io) as $class => $path) {
if ('' === $namespace || 0 === strpos($class, $namespace)) {
if (!isset($classMap[$class])) {
$path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
@ -198,18 +198,12 @@ EOF;
}
foreach ($autoloads['classmap'] as $dir) {
foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
foreach (ClassMapGenerator::createMap($dir, null, $this->io) as $class => $path) {
$path = $this->getPathCode($filesystem, $basePath, $vendorPath, $path);
$classMap[$class] = $path.",\n";
}
}
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;

@ -12,21 +12,18 @@
*/
namespace Composer\Autoload;
use Symfony\Component\Finder\Finder;
use Composer\IO\IOInterface;
/**
* ClassMapGenerator
*
* @author Gyula Sallai <salla016@gmail.com>
* @author Jordi Boggiano <j.boggiano@seld.be>
*/
class ClassMapGenerator
{
/**
* @var array
*/
public static $ambiguousReferences = array();
/**
* Generate a class map file
*
@ -54,7 +51,7 @@ class ClassMapGenerator
*
* @throws \RuntimeException When the path is neither an existing file nor directory
*/
public static function createMap($path, $whitelist = null)
public static function createMap($path, $whitelist = null, IOInterface $io = null)
{
if (is_string($path)) {
if (is_file($path)) {
@ -85,15 +82,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
if (!isset($map[$class])) {
$map[$class] = $filePath;
} elseif ($io) {
$io->write(
'<warning>Warning: Ambiguous class resolution, "'.$class.'"'.
' was found in both "'.$map[$class].'" and "'.$filePath.'", the first will be used.</warning>'
);
}
$map[$class] = $filePath;
}
}

@ -109,10 +109,18 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
$finder = new Finder();
$finder->files()->in(__DIR__ . '/Fixtures/Ambiguous');
ClassMapGenerator::createMap($finder);
$io = $this->getMockBuilder('Composer\IO\ConsoleIO')
->disableOriginalConstructor()
->getMock();
$this->assertEquals(1, count(ClassMapGenerator::$ambiguousReferences));
$this->assertEquals('A', ClassMapGenerator::$ambiguousReferences[0]['class']);
$a = realpath(__DIR__.'/Fixtures/Ambiguous/A.php');
$b = realpath(__DIR__.'/Fixtures/Ambiguous/other/A.php');
$io->expects($this->once())
->method('write')
->with('<warning>Warning: Ambiguous class resolution, "A" was found in both "'.$a.'" and "'.$b.'", the first will be used.</warning>');
ClassMapGenerator::createMap($finder, null, $io);
}
/**

Loading…
Cancel
Save