Skip to content
Browse files

Merge branch 'classmapgen'

  • Loading branch information...
2 parents 289d23b + 2edb26d commit fb62e2b52fefa8c5d90c9d745612d4a0bfbc9d7f @Seldaek Seldaek committed
View
44 src/Composer/Autoload/AutoloadGenerator.php
@@ -25,7 +25,7 @@
*/
class AutoloadGenerator
{
- public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $suffix = '')
+ public function dump(Config $config, RepositoryInterface $localRepo, PackageInterface $mainPackage, InstallationManager $installationManager, $targetDir, $scanPsr0Packages = false, $suffix = '')
{
$filesystem = new Filesystem();
$filesystem->ensureDirectoryExists($config->get('vendor-dir'));
@@ -116,13 +116,38 @@ public static function autoload(\$class)
}
// flatten array
+ $classMap = array();
$autoloads['classmap'] = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($autoloads['classmap']));
+
+ if ($scanPsr0Packages) {
+ foreach ($autoloads['psr-0'] as $namespace => $paths) {
+ foreach ($paths as $dir) {
+ $dir = $this->getPath($filesystem, $relVendorPath, $vendorPath, $dir);
+ $whitelist = sprintf(
+ '{%s/%s.+(?<!(?<!/)Test\.php)$}',
+ preg_quote(rtrim($dir, '/')),
+ strpos($namespace, '_') === false ? preg_quote(strtr($namespace, '\\', '/')) : ''
+ );
+ foreach (ClassMapGenerator::createMap($dir, $whitelist) 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 +304,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 strtr(getcwd(), '\\', '/').'/'.$path;
+ }
+
+ return $path;
+ }
+
protected function getAutoloadFile($vendorPathToTargetDirCode, $suffix)
{
return <<<AUTOLOAD
@@ -383,4 +422,3 @@ public static function getLoader()
}
}
-
View
7 src/Composer/Autoload/ClassMapGenerator.php
@@ -41,10 +41,11 @@ public static function dump($dirs, $file)
* Iterate over all files in the given directory searching for classes
*
* @param Iterator|string $dir The directory to search in or an iterator
+ * @param string $whitelist Regex that matches against the file path
*
* @return array A class map array
*/
- public static function createMap($dir)
+ public static function createMap($dir, $whitelist = null)
{
if (is_string($dir)) {
if (is_file($dir)) {
@@ -67,6 +68,10 @@ public static function createMap($dir)
continue;
}
+ if ($whitelist && !preg_match($whitelist, strtr($path, '\\', '/'))) {
+ continue;
+ }
+
$classes = self::findClasses($path);
foreach ($classes as $class) {
View
6 src/Composer/Command/DumpAutoloadCommand.php
@@ -13,6 +13,7 @@
namespace Composer\Command;
use Symfony\Component\Console\Input\InputInterface;
+use Symfony\Component\Console\Input\InputOption;
use Composer\Repository\CompositeRepository;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Autoload\AutoloadGenerator;
@@ -27,6 +28,9 @@ protected function configure()
$this
->setName('dump-autoload')
->setDescription('dumps the autoloader')
+ ->setDefinition(array(
+ new InputOption('optimize', 'o', InputOption::VALUE_NONE, 'Optimizes PSR0 packages to be loaded with classmaps too, good for production.'),
+ ))
->setHelp(<<<EOT
<info>php composer.phar dump-autoload</info>
EOT
@@ -45,6 +49,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
$config = $composer->getConfig();
$generator = new AutoloadGenerator();
- $generator->dump($config, $localRepos, $package, $installationManager, 'composer');
+ $generator->dump($config, $localRepos, $package, $installationManager, 'composer', $input->getOption('optimize'));
}
}
View
135 tests/Composer/Test/Autoload/AutoloadGeneratorTest.php
@@ -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,13 +86,13 @@ 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);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_1');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_1');
$this->assertAutoloadFiles('main', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap', $this->vendorDir.'/composer', 'classmap');
}
@@ -114,13 +111,13 @@ 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);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_2');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', true, '_2');
$this->assertAutoloadFiles('main3', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap3', $this->vendorDir.'/composer', 'classmap');
}
@@ -138,9 +135,12 @@ 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->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_3');
$this->assertAutoloadFiles('main2', $this->vendorDir.'/composer');
$this->assertAutoloadFiles('classmap2', $this->vendorDir.'/composer', 'classmap');
}
@@ -157,7 +157,9 @@ public function testMainPackageAutoloadingWithTargetDir()
->method('getPackages')
->will($this->returnValue(array()));
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'TargetDir');
+ $this->fs->ensureDirectoryExists($this->vendorDir.'/a');
+
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '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,8 +179,12 @@ public function testVendorsAutoloading()
->method('getPackages')
->will($this->returnValue($packages));
- mkdir($this->vendorDir.'/composer', 0777, true);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_5');
+ $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', false, '_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,15 +203,15 @@ 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 {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_6');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_6');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals(
array(
@@ -234,15 +240,15 @@ 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 {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_7');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, '_7');
$this->assertTrue(file_exists($this->vendorDir.'/composer/autoload_classmap.php'), "ClassMap file needs to be generated.");
$this->assertEquals(
array(
@@ -269,12 +275,12 @@ 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() {}');
- $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', 'FilesAutoload');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', false, 'FilesAutoload');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php');
$this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions.php', $this->vendorDir.'/composer/autoload_realFilesAutoload.php');
@@ -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->generator->dump($this->config, $this->repository, $package, $this->im, 'composer', '_9');
- $this->assertAutoloadFiles('override_vendors', $this->vendorDir.'/composer');
+ $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', true, '_9');
+ $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,9 +373,9 @@ 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');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_10');
$this->assertFileEquals(__DIR__.'/Fixtures/include_paths.php', $this->vendorDir.'/composer/include_paths.php');
$this->assertEquals(
@@ -359,7 +404,7 @@ public function testIncludePathsArePrependedInAutoloadFile()
mkdir($this->vendorDir."/composer", 0777, true);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_11');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_11');
$oldIncludePath = get_include_path();
@@ -390,7 +435,7 @@ public function testIncludePathFileWithoutPathsIsSkipped()
mkdir($this->vendorDir."/composer", 0777, true);
- $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", '_12');
+ $this->generator->dump($this->config, $this->repository, $package, $this->im, "composer", false, '_12');
$this->assertFalse(file_exists($this->vendorDir."/composer/include_paths.php"));
}
View
1 tests/Composer/Test/Autoload/Fixtures/autoload_classmap3.php
@@ -6,5 +6,6 @@
$baseDir = $vendorDir;
return array(
+ 'Main\\Foo' => $baseDir . '/src/Main/Foo.php',
'ClassMapFoo' => $baseDir . '/composersrc/foo.php',
);
View
12 tests/Composer/Test/Autoload/Fixtures/autoload_override_vendors.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 fb62e2b

Please sign in to comment.
Something went wrong with that request. Please try again.