Permalink
Browse files

Automatically generate classmaps for all PSR-0 packages to speed thin…

…gs up, fixes #541, fixes #127
  • Loading branch information...
1 parent 289d23b commit 48c46ce3b6e773c65ee904a745e1a8af6292f147 @Seldaek Seldaek committed Jun 19, 2012
@@ -116,13 +116,30 @@ public static function autoload(\$class)
}
// flatten array
+ $classMap = array();
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
+ foreach ($autoloads['psr-0'] as $namespace => $paths) {
+ foreach ($paths as $dir) {
+ $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
+ foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
+ if (0 === strpos($class, $namespace)) {
+ $path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
+ if (!isset($classMap[$class])) {
+ $classMap[$class] = '$baseDir . '.var_export($path, true).",\n";
+ }
+ }
+ }
+ }
+ }
foreach ($autoloads['classmap'] as $dir) {
foreach (ClassMapGenerator::createMap($dir) as $class => $path) {
$path = '/'.$filesystem->findShortestPath(getcwd(), $path, true);
- $classmapFile .= ' '.var_export($class, true).' => $baseDir . '.var_export($path, true).",\n";
+ $classMap[$class] = '$baseDir . '.var_export($path, true).",\n";
}
}
+ foreach ($classMap as $class => $code) {
+ $classmapFile .= ' '.var_export($class, true).' => '.$code;
+ }
$classmapFile .= ");\n";
$filesCode = "";
@@ -279,6 +296,20 @@ protected function getPathCode(Filesystem $filesystem, $relVendorPath, $vendorPa
return $baseDir.var_export($path, true);
}
+ protected function getPath(Filesystem $filesystem, $relVendorPath, $vendorPath, $path)
+ {
+ $path = strtr($path, '\\', '/');
+ if (!$filesystem->isAbsolutePath($path)) {
+ if (strpos($path, $relVendorPath) === 0) {
+ // path starts with vendor dir
+ return $vendorPath . substr($path, strlen($relVendorPath));
+ }
+ return getcwd().'/'.$path;
+ }
+
+ return $path;
+ }
+
protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
{
return <<<AUTOLOAD
@@ -383,4 +414,3 @@ public static function getLoader()
}
}
-
@@ -33,7 +33,8 @@ protected function setUp()
$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 @@ protected function setUp()
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)) {
- $this->fs->removeDirectory($this->vendorDir);
+ chdir($this->dir);
+
+ if (is_dir($this->workingDir)) {
+ $this->fs->removeDirectory($this->workingDir);
}
- if (is_dir($this->workingDir.'/composersrc')) {
- $this->fs->removeDirectory($this->workingDir.'/composersrc');
+ if (is_dir($this->vendorDir)) {
+ $this->fs->removeDirectory($this->vendorDir);
}
-
- chdir($this->dir);
}
public function testMainPackageAutoloading()
@@ -89,9 +86,9 @@ public function testMainPackageAutoloading()
->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 @@ public function testVendorDirSameAsWorkingDir()
->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 @@ public function testMainPackageAutoloadingAlternativeVendorDir()
->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 @@ public function testMainPackageAutoloadingWithTargetDir()
->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 @@ public function testVendorsAutoloading()
->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 @@ public function testVendorsClassMapAutoloading()
->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 @@ public function testClassMapAutoloadingEmptyDirAndExactFile()
->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 @@ public function testFilesAutoloadGeneration()
->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 @@ public function testFilesAutoloadGeneration()
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 @@ public function testOverrideVendorsAutoloading()
->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 @@ public function testIncludePathFileGeneration()
->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');
@@ -6,5 +6,6 @@
$baseDir = $vendorDir;
return array(
+ 'Main\\Foo' => $baseDir . '/src/Main/Foo.php',
'ClassMapFoo' => $baseDir . '/composersrc/foo.php',
);
@@ -1,12 +0,0 @@
-<?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('/home/deveuser/local-packages/a-a/lib', $vendorDir . '/a/a/lib/'),
- 'A' => $vendorDir . '/a/a/src/',
-);

0 comments on commit 48c46ce

Please sign in to comment.