1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-09 00:22:53 +00:00

Automatically generate classmaps for all PSR-0 packages to speed things up, fixes #541, fixes #127

This commit is contained in:
Jordi Boggiano 2012-06-19 23:15:42 +02:00
parent 289d23b5cc
commit 48c46ce3b6
4 changed files with 111 additions and 47 deletions

View file

@ -33,7 +33,8 @@ class AutoloadGeneratorTest extends TestCase
$this->fs = new Filesystem;
$that = $this;
$this->workingDir = realpath(sys_get_temp_dir());
$this->workingDir = realpath(sys_get_temp_dir()).DIRECTORY_SEPARATOR.'cmptest';
$this->fs->ensureDirectoryExists($this->workingDir);
$this->vendorDir = $this->workingDir.DIRECTORY_SEPARATOR.'composer-test-autoload';
$this->ensureDirectoryExistsAndClear($this->vendorDir);
@ -63,18 +64,14 @@ class AutoloadGeneratorTest extends TestCase
protected function tearDown()
{
if ($this->vendorDir === $this->workingDir) {
if (is_dir($this->workingDir.'/composer')) {
$this->fs->removeDirectory($this->workingDir.'/composer');
}
} elseif (is_dir($this->vendorDir)) {
chdir($this->dir);
if (is_dir($this->workingDir)) {
$this->fs->removeDirectory($this->workingDir);
}
if (is_dir($this->vendorDir)) {
$this->fs->removeDirectory($this->vendorDir);
}
if (is_dir($this->workingDir.'/composersrc')) {
$this->fs->removeDirectory($this->workingDir.'/composersrc');
}
chdir($this->dir);
}
public function testMainPackageAutoloading()
@ -89,9 +86,9 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue(array()));
if (!is_dir($this->vendorDir.'/composer')) {
mkdir($this->vendorDir.'/composer');
}
$this->fs->ensureDirectoryExists($this->workingDir.'/composer');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->fs->ensureDirectoryExists($this->workingDir.'/lib');
$this->createClassFile($this->workingDir);
@ -114,9 +111,9 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue(array()));
if (!is_dir($this->vendorDir.'/composer')) {
mkdir($this->vendorDir.'/composer', 0777, true);
}
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/src/Main');
file_put_contents($this->vendorDir.'/src/Main/Foo.php', '<?php namespace Main; class Foo {}');
$this->createClassFile($this->vendorDir);
@ -138,7 +135,10 @@ class AutoloadGeneratorTest extends TestCase
->will($this->returnValue(array()));
$this->vendorDir .= '/subdir';
mkdir($this->vendorDir.'/composer', 0777, true);
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->workingDir.'/src');
$this->createClassFile($this->workingDir);
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_3');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
@ -157,6 +157,8 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue(array()));
$this->fs->ensureDirectoryExists($this->vendorDir.'/a');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'TargetDir');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_target_dir.php', $this->vendorDir.'/autoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_target_dir.php', $this->vendorDir.'/composer/autoload_realTargetDir.php');
@ -177,7 +179,11 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue($packages));
mkdir($this->vendorDir.'/composer', 0777, true);
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_5');
$this->assertAutoloadFiles('vendors', $this->vendorDir.'/composer');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated, even if empty.");
@ -197,10 +203,10 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue($packages));
@mkdir($this->vendorDir.'/composer', 0777, true);
mkdir($this->vendorDir.'/a/a/src', 0777, true);
mkdir($this->vendorDir.'/b/b/src', 0777, true);
mkdir($this->vendorDir.'/b/b/lib', 0777, true);
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/lib');
file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
file_put_contents($this->vendorDir.'/b/b/src/b.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/b/b/lib/c.php', '<?php class ClassMapBaz {}');
@ -234,10 +240,10 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue($packages));
@mkdir($this->vendorDir.'/composer', 0777, true);
mkdir($this->vendorDir.'/a/a/src', 0777, true);
mkdir($this->vendorDir.'/b/b', 0777, true);
mkdir($this->vendorDir.'/c/c/foo', 0777, true);
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
$this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo');
file_put_contents($this->vendorDir.'/a/a/src/a.php', '<?php class ClassMapFoo {}');
file_put_contents($this->vendorDir.'/b/b/test.php', '<?php class ClassMapBar {}');
file_put_contents($this->vendorDir.'/c/c/foo/test.php', '<?php class ClassMapBaz {}');
@ -269,8 +275,8 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue($packages));
mkdir($this->vendorDir.'/a/a', 0777, true);
mkdir($this->vendorDir.'/b/b', 0777, true);
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b');
file_put_contents($this->vendorDir.'/a/a/test.php', '<?php function testFilesAutoloadGeneration1() {}');
file_put_contents($this->vendorDir.'/b/b/test2.php', '<?php function testFilesAutoloadGeneration2() {}');
@ -289,7 +295,7 @@ class AutoloadGeneratorTest extends TestCase
public function testOverrideVendorsAutoloading()
{
$package = new MemoryPackage('a', '1.0', '1.0');
$package->setAutoload(array('psr-0' => array('A\\B' => '/home/deveuser/local-packages/a-a/lib')));
$package->setAutoload(array('psr-0' => array('A\\B' => $this->workingDir.'/lib')));
$packages = array();
$packages[] = $a = new MemoryPackage('a/a', '1.0', '1.0');
@ -301,9 +307,48 @@ class AutoloadGeneratorTest extends TestCase
->method('getPackages')
->will($this->returnValue($packages));
mkdir($this->vendorDir.'/composer', 0777, true);
$this->fs->ensureDirectoryExists($this->workingDir.'/lib/A/B');
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/src');
$this->fs->ensureDirectoryExists($this->vendorDir.'/a/a/lib/A/B');
$this->fs->ensureDirectoryExists($this->vendorDir.'/b/b/src');
file_put_contents($this->workingDir.'/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
file_put_contents($this->vendorDir.'/a/a/lib/A/B/C.php', '<?php namespace A\\B; class C {}');
$workDir = strtr($this->workingDir, '\\', '/');
$expectedNamespace = <<<EOF
<?php
// autoload_namespaces.php generated by Composer
\$vendorDir = dirname(__DIR__);
\$baseDir = dirname(\$vendorDir);
return array(
'B\\\\Sub\\\\Name' => \$vendorDir . '/b/b/src/',
'A\\\\B' => array('$workDir/lib', \$vendorDir . '/a/a/lib/'),
'A' => \$vendorDir . '/a/a/src/',
);
EOF;
$expectedClassmap = <<<EOF
<?php
// autoload_classmap.php generated by Composer
\$vendorDir = dirname(__DIR__);
\$baseDir = dirname(\$vendorDir);
return array(
'A\\\\B\\\\C' => \$baseDir . '/lib/A/B/C.php',
);
EOF;
$this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_9');
$this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/composer');
$this->assertEquals($expectedNamespace, file_get_contents($this->vendorDir.'/composer/autoload_namespaces.php'));
$this->assertEquals($expectedClassmap, file_get_contents($this->vendorDir.'/composer/autoload_classmap.php'));
}
public function testIncludePathFileGeneration()
@ -328,7 +373,7 @@ class AutoloadGeneratorTest extends TestCase
->method("getPackages")
->will($this->returnValue($packages));
mkdir($this->vendorDir."/composer", 0777, true);
$this->fs->ensureDirectoryExists($this->vendorDir.'/composer');
$this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_10');