1
0
Fork 0

Refactor to avoid global static state

pull/2855/head
Jordi Boggiano 2014-03-29 17:46:55 +01:00
parent c3ab09750d
commit 781c97fa9b
3 changed files with 24 additions and 26 deletions

View File

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

View File

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

View File

@ -109,10 +109,18 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
$finder = new Finder(); $finder = new Finder();
$finder->files()->in(__DIR__ . '/Fixtures/Ambiguous'); $finder->files()->in(__DIR__ . '/Fixtures/Ambiguous');
ClassMapGenerator::createMap($finder); $io = $this->getMockBuilder('Composer\IO\ConsoleIO')
->disableOriginalConstructor()
->getMock();
$this->assertEquals(1, count(ClassMapGenerator::$ambiguousReferences)); $a = realpath(__DIR__.'/Fixtures/Ambiguous/A.php');
$this->assertEquals('A', ClassMapGenerator::$ambiguousReferences[0]['class']); $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);
} }
/** /**