Merge remote-tracking branch 'hason/classloader'

main
Jordi Boggiano 12 years ago
commit 5f30206c69

@ -58,7 +58,7 @@ EOF;
$filesystem = new Filesystem(); $filesystem = new Filesystem();
$vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/'); $vendorPath = strtr(realpath($installationManager->getVendorPath()), '\\', '/');
$relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath); $relVendorPath = $filesystem->findShortestPath(getcwd(), $vendorPath, true);
$vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true); $vendorDirCode = $filesystem->findShortestPathCode(realpath($targetDir), $vendorPath, true);
$appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true); $appBaseDir = $filesystem->findShortestPathCode($vendorPath, getcwd(), true);
@ -85,11 +85,7 @@ EOF;
$path = strtr($path, '\\', '/'); $path = strtr($path, '\\', '/');
$baseDir = ''; $baseDir = '';
if (!$filesystem->isAbsolutePath($path)) { if (!$filesystem->isAbsolutePath($path)) {
// vendor dir == working dir if (strpos($path, $relVendorPath) === 0) {
if (preg_match('{^(\./?)?$}', $relVendorPath)) {
$path = '/'.$path;
$baseDir = '$vendorDir . ';
} elseif (strpos($path, $relVendorPath) === 0) {
// path starts with vendor dir // path starts with vendor dir
$path = substr($path, strlen($relVendorPath)); $path = substr($path, strlen($relVendorPath));
$baseDir = '$vendorDir . '; $baseDir = '$vendorDir . ';
@ -129,7 +125,7 @@ EOF;
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap'])); $autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
foreach ($autoloads['classmap'] as $dir) { foreach ($autoloads['classmap'] as $dir) {
foreach (ClassMapGenerator::createMap($dir) as $class => $path) { foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
$path = '/'.$filesystem->findShortestPath(getcwd(), $path); $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
$classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n"; $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
} }
} }

@ -58,9 +58,10 @@ class Filesystem
* *
* @param string $from * @param string $from
* @param string $to * @param string $to
* @param Boolean $directories if true, the source/target are considered to be directories
* @return string * @return string
*/ */
public function findShortestPath($from, $to) public function findShortestPath($from, $to, $directories = false)
{ {
if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) { if (!$this->isAbsolutePath($from) || !$this->isAbsolutePath($to)) {
throw new \InvalidArgumentException('from and to must be absolute paths'); throw new \InvalidArgumentException('from and to must be absolute paths');
@ -69,6 +70,10 @@ class Filesystem
$from = lcfirst(rtrim(strtr($from, '\\', '/'), '/')); $from = lcfirst(rtrim(strtr($from, '\\', '/'), '/'));
$to = lcfirst(rtrim(strtr($to, '\\', '/'), '/')); $to = lcfirst(rtrim(strtr($to, '\\', '/'), '/'));
if ($directories) {
$from .= '/dummy_file';
}
if (dirname($from) === dirname($to)) { if (dirname($from) === dirname($to)) {
return './'.basename($to); return './'.basename($to);
} }
@ -143,4 +148,4 @@ class Filesystem
{ {
return new ProcessExecutor; return new ProcessExecutor;
} }
} }

@ -66,21 +66,34 @@ class AutoloadGeneratorTest extends TestCase
} elseif (is_dir($this->vendorDir)) { } elseif (is_dir($this->vendorDir)) {
$this->fs->removeDirectory($this->vendorDir); $this->fs->removeDirectory($this->vendorDir);
} }
if (is_dir($this->workingDir.'/.composersrc')) {
$this->fs->removeDirectory($this->workingDir.'/.composersrc');
}
chdir($this->dir); chdir($this->dir);
} }
public function testMainPackageAutoloading() public function testMainPackageAutoloading()
{ {
$package = new MemoryPackage('a', '1.0', '1.0'); $package = new MemoryPackage('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); $package->setAutoload(array(
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
'classmap' => array('.composersrc/'),
));
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('getPackages') ->method('getPackages')
->will($this->returnValue(array())); ->will($this->returnValue(array()));
mkdir($this->vendorDir.'/.composer'); if (!is_dir($this->vendorDir.'/.composer')) {
mkdir($this->vendorDir.'/.composer');
}
$this->createClassFile($this->workingDir);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('main', $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main', $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('classmap', $this->vendorDir.'/.composer', 'classmap');
} }
public function testVendorDirSameAsWorkingDir() public function testVendorDirSameAsWorkingDir()
@ -88,7 +101,10 @@ class AutoloadGeneratorTest extends TestCase
$this->vendorDir = $this->workingDir; $this->vendorDir = $this->workingDir;
$package = new MemoryPackage('a', '1.0', '1.0'); $package = new MemoryPackage('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); $package->setAutoload(array(
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
'classmap' => array('.composersrc/'),
));
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('getPackages') ->method('getPackages')
@ -98,14 +114,20 @@ class AutoloadGeneratorTest extends TestCase
mkdir($this->vendorDir.'/.composer', 0777, true); mkdir($this->vendorDir.'/.composer', 0777, true);
} }
$this->createClassFile($this->vendorDir);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main3', $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('classmap3', $this->vendorDir.'/.composer', 'classmap');
} }
public function testMainPackageAutoloadingAlternativeVendorDir() public function testMainPackageAutoloadingAlternativeVendorDir()
{ {
$package = new MemoryPackage('a', '1.0', '1.0'); $package = new MemoryPackage('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('Main' => 'src/', 'Lala' => 'src/'))); $package->setAutoload(array(
'psr-0' => array('Main' => 'src/', 'Lala' => 'src/'),
'classmap' => array('.composersrc/'),
));
$this->repository->expects($this->once()) $this->repository->expects($this->once())
->method('getPackages') ->method('getPackages')
@ -113,8 +135,10 @@ class AutoloadGeneratorTest extends TestCase
$this->vendorDir .= '/subdir'; $this->vendorDir .= '/subdir';
mkdir($this->vendorDir.'/.composer', 0777, true); mkdir($this->vendorDir.'/.composer', 0777, true);
$this->createClassFile($this->workingDir);
$this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer'); $this->generator->dump($this->repository, $package, $this->im, $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('main2', $this->vendorDir.'/.composer');
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/.composer', 'classmap');
} }
public function testVendorsAutoloading() public function testVendorsAutoloading()
@ -169,6 +193,7 @@ class AutoloadGeneratorTest extends TestCase
), ),
include ($this->vendorDir.'/.composer/autoload_classmap.php') include ($this->vendorDir.'/.composer/autoload_classmap.php')
); );
$this->assertAutoloadFiles('classmap4', $this->vendorDir.'/.composer', 'classmap');
} }
public function testOverrideVendorsAutoloading() public function testOverrideVendorsAutoloading()
@ -191,8 +216,17 @@ class AutoloadGeneratorTest extends TestCase
$this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer'); $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/.composer');
} }
private function assertAutoloadFiles($name, $dir) private function createClassFile($basedir)
{
if (!is_dir($basedir.'/.composersrc')) {
mkdir($basedir.'/.composersrc', 0777, true);
}
file_put_contents($basedir.'/.composersrc/foo.php', '<?php class ClassMapFoo {}');
}
private function assertAutoloadFiles($name, $dir, $type = 'namespaces')
{ {
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_namespaces.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_'.$name.'.php', $dir.'/autoload_'.$type.'.php');
} }
} }

@ -25,7 +25,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
public function getTestCreateMapTests() public function getTestCreateMapTests()
{ {
return array( $data = array(
array(__DIR__.'/Fixtures/Namespaced', array( array(__DIR__.'/Fixtures/Namespaced', array(
'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.php', 'Namespaced\\Bar' => realpath(__DIR__).'/Fixtures/Namespaced/Bar.php',
'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php', 'Namespaced\\Foo' => realpath(__DIR__).'/Fixtures/Namespaced/Foo.php',
@ -44,6 +44,7 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
array(__DIR__.'/Fixtures/classmap', array( array(__DIR__.'/Fixtures/classmap', array(
'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php', 'Foo\\Bar\\A' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php', 'Foo\\Bar\\B' => realpath(__DIR__).'/Fixtures/classmap/sameNsMultipleClasses.php',
'A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', 'Alpha\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', 'Alpha\\B' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
'Beta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php', 'Beta\\A' => realpath(__DIR__).'/Fixtures/classmap/multipleNs.php',
@ -53,6 +54,19 @@ class ClassMapGeneratorTest extends \PHPUnit_Framework_TestCase
'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php', 'ClassMap\\SomeClass' => realpath(__DIR__).'/Fixtures/classmap/SomeClass.php',
)), )),
); );
if (version_compare(PHP_VERSION, '5.4', '>=')) {
$data[] = array(__DIR__.'/Fixtures/php5.4', array(
'TFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
'CFoo' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\TBar' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\IBar' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\TFooBar' => __DIR__.'/Fixtures/php5.4/traits.php',
'Foo\\CBar' => __DIR__.'/Fixtures/php5.4/traits.php',
));
}
return $data;
} }
public function testCreateMapFinderSupport() public function testCreateMapFinderSupport()

@ -0,0 +1,10 @@
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'ClassMapFoo' => $baseDir . '/.composersrc/foo.php',
);

@ -0,0 +1,10 @@
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname(dirname($vendorDir));
return array(
'ClassMapFoo' => $baseDir . '/.composersrc/foo.php',
);

@ -0,0 +1,10 @@
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = $vendorDir;
return array(
'ClassMapFoo' => $baseDir . '/.composersrc/foo.php',
);

@ -0,0 +1,12 @@
<?php
// autoload_classmap.php generated by Composer
$vendorDir = dirname(__DIR__);
$baseDir = dirname($vendorDir);
return array(
'ClassMapBaz' => $baseDir . '/composer-test-autoload/b/b/lib/c.php',
'ClassMapFoo' => $baseDir . '/composer-test-autoload/a/a/src/a.php',
'ClassMapBar' => $baseDir . '/composer-test-autoload/b/b/src/b.php',
);

@ -1,4 +1,7 @@
<?php <?php
namespace {
class A {}
}
namespace Alpha { namespace Alpha {
class A {} class A {}

@ -0,0 +1,28 @@
<?php
namespace {
trait TFoo {
}
class CFoo {
use TFoo;
}
}
namespace Foo {
trait TBar {
}
interface IBar {
}
trait TFooBar {
}
class CBar implements IBar {
use TBar, TFooBar;
}
}

@ -58,10 +58,10 @@ class FilesystemTest extends TestCase
/** /**
* @dataProvider providePathCouples * @dataProvider providePathCouples
*/ */
public function testFindShortestPath($a, $b, $expected) public function testFindShortestPath($a, $b, $expected, $directory = false)
{ {
$fs = new Filesystem; $fs = new Filesystem;
$this->assertEquals($expected, $fs->findShortestPath($a, $b)); $this->assertEquals($expected, $fs->findShortestPath($a, $b, $directory));
} }
public function providePathCouples() public function providePathCouples()
@ -69,8 +69,13 @@ class FilesystemTest extends TestCase
return array( return array(
array('/foo/bar', '/foo/bar', "./bar"), array('/foo/bar', '/foo/bar', "./bar"),
array('/foo/bar', '/foo/baz', "./baz"), array('/foo/bar', '/foo/baz', "./baz"),
array('/foo/bar/', '/foo/baz', "./baz"),
array('/foo/bar', '/foo/bar', "./", true),
array('/foo/bar', '/foo/baz', "../baz", true),
array('/foo/bar/', '/foo/baz', "../baz", true),
array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('/foo/bin/run', '/foo/vendor/acme/bin/run', "../vendor/acme/bin/run"),
array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"), array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run"),
array('/foo/bin/run', '/bar/bin/run', "/bar/bin/run", true),
array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:/bin/run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"), array('c:\\bin\\run', 'c:/vendor/acme/bin/run', "../vendor/acme/bin/run"),
array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"), array('c:/bin/run', 'd:/vendor/acme/bin/run', "d:/vendor/acme/bin/run"),
@ -79,6 +84,7 @@ class FilesystemTest extends TestCase
array('/tmp/test', '/tmp', "./"), array('/tmp/test', '/tmp', "./"),
array('C:/Temp/test/sub', 'C:\Temp', "../"), array('C:/Temp/test/sub', 'C:\Temp', "../"),
array('/tmp/test/sub', '/tmp', "../"), array('/tmp/test/sub', '/tmp', "../"),
array('/tmp/test/sub', '/tmp', "../../", true),
array('/tmp', '/tmp/test', "test"), array('/tmp', '/tmp/test', "test"),
array('C:/Temp', 'C:\Temp\test', "test"), array('C:/Temp', 'C:\Temp\test', "test"),
array('C:/Temp', 'c:\Temp\test', "test"), array('C:/Temp', 'c:\Temp\test', "test"),

Loading…
Cancel
Save