From 4fa1cb2e9c3e4cf3fe8242b99551e76c00d3277d Mon Sep 17 00:00:00 2001 From: voroks Date: Sat, 15 Aug 2015 16:12:16 +0800 Subject: [PATCH 1/5] Removing file autoload_files.php was added (#4254) --- src/Composer/Autoload/AutoloadGenerator.php | 7 +- .../Test/Autoload/AutoloadGeneratorTest.php | 65 +++++++++++++++++++ ...oad_files_functions_with_removed_extra.php | 10 +++ ...load_real_functions_with_removed_extra.php | 50 ++++++++++++++ 4 files changed, 130 insertions(+), 2 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_files_functions_with_removed_extra.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_removed_extra.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 4a55a53c0..b89c23113 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -255,9 +255,12 @@ EOF; if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { file_put_contents($targetDir.'/include_paths.php', $includePathFile); } + $includeFilesFilePath = $targetDir.'/autoload_files.php'; if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { - file_put_contents($targetDir.'/autoload_files.php', $includeFilesFile); - } + file_put_contents($includeFilesFilePath, $includeFilesFile); + } else if (file_exists($includeFilesFilePath) === true) { + unlink($includeFilesFilePath); + } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFile, $targetDirLoader, (bool) $includeFilesFile, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $classMapAuthoritative)); diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index dbb7d2fad..bf63af717 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -585,6 +585,71 @@ class AutoloadGeneratorTest extends TestCase $this->assertTrue(function_exists('testFilesAutoloadGenerationRoot')); } + public function testFilesAutoloadGenerationRemoveExtraEntitiesFromAutoloadFiles() + { + $autoloadPackage = new Package('a', '1.0', '1.0'); + $autoloadPackage->setAutoload(array('files' => array('root.php'))); + + $notAutoloadPackage = new Package('a', '1.0', '1.0'); + + $autoloadPackages = array(); + $autoloadPackages[] = $a = new Package('a/a', '1.0', '1.0'); + $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0'); + $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0'); + $a->setAutoload(array('files' => array('test.php'))); + $b->setAutoload(array('files' => array('test2.php'))); + $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php'))); + $c->setTargetDir('foo/bar'); + + $notAutoloadPackages = array(); + $notAutoloadPackages[] = $a = new Package('a/a', '1.0', '1.0'); + $notAutoloadPackages[] = $b = new Package('b/b', '1.0', '1.0'); + $notAutoloadPackages[] = $c = new Package('c/c', '1.0', '1.0'); + + $this->repository->expects($this->at(0)) + ->method('getCanonicalPackages') + ->will($this->returnValue($autoloadPackages)); + + $this->repository->expects($this->at(1)) + ->method('getCanonicalPackages') + ->will($this->returnValue($notAutoloadPackages)); + + $this->repository->expects($this->at(2)) + ->method('getCanonicalPackages') + ->will($this->returnValue($notAutoloadPackages)); + + $this->fs->ensureDirectoryExists($this->vendorDir.'/a/a'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/b/b'); + $this->fs->ensureDirectoryExists($this->vendorDir.'/c/c/foo/bar'); + file_put_contents($this->vendorDir.'/a/a/test.php', 'vendorDir.'/b/b/test2.php', 'vendorDir.'/c/c/foo/bar/test3.php', 'vendorDir.'/c/c/foo/bar/test4.php', 'workingDir.'/root.php', 'generator->dump($this->config, $this->repository, $autoloadPackage, $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_real.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php'); + + $this->generator->dump($this->config, $this->repository, $autoloadPackage, $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_real.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php'); + + $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_real.php'); + $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php'); + + include $this->vendorDir . '/autoload.php'; + $this->assertFalse(function_exists('testFilesAutoloadGeneration1')); + $this->assertFalse(function_exists('testFilesAutoloadGeneration2')); + $this->assertFalse(function_exists('testFilesAutoloadGeneration3')); + $this->assertFalse(function_exists('testFilesAutoloadGeneration4')); + $this->assertFalse(function_exists('testFilesAutoloadGenerationRoot')); + } + public function testFilesAutoloadOrderByDependencies() { $package = new Package('a', '1.0', '1.0'); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions_with_removed_extra.php b/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions_with_removed_extra.php new file mode 100644 index 000000000..8d124df37 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions_with_removed_extra.php @@ -0,0 +1,10 @@ + $path) { + $loader->set($namespace, $path); + } + + $map = require __DIR__ . '/autoload_psr4.php'; + foreach ($map as $namespace => $path) { + $loader->setPsr4($namespace, $path); + } + + $classMap = require __DIR__ . '/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + + $loader->register(true); + + return $loader; + } +} + +function composerRequireFilesAutoload($file) +{ + require $file; +} From 85de450fe9d0069b9f89234db658ad030d5ca91e Mon Sep 17 00:00:00 2001 From: voroks Date: Sat, 15 Aug 2015 16:41:35 +0800 Subject: [PATCH 2/5] Removed some extra assertions. --- tests/Composer/Test/Autoload/AutoloadGeneratorTest.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index bf63af717..08f9cc09d 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -641,13 +641,6 @@ class AutoloadGeneratorTest extends TestCase $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php'); - - include $this->vendorDir . '/autoload.php'; - $this->assertFalse(function_exists('testFilesAutoloadGeneration1')); - $this->assertFalse(function_exists('testFilesAutoloadGeneration2')); - $this->assertFalse(function_exists('testFilesAutoloadGeneration3')); - $this->assertFalse(function_exists('testFilesAutoloadGeneration4')); - $this->assertFalse(function_exists('testFilesAutoloadGenerationRoot')); } public function testFilesAutoloadOrderByDependencies() From cca42ff0be37f9eff7380953549174dca59cd040 Mon Sep 17 00:00:00 2001 From: voroks Date: Tue, 18 Aug 2015 15:29:22 +0800 Subject: [PATCH 3/5] Fixed indents: we should use spaces instead tabulation. --- src/Composer/Autoload/AutoloadGenerator.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index b89c23113..86f954aba 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -255,12 +255,12 @@ EOF; if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { file_put_contents($targetDir.'/include_paths.php', $includePathFile); } - $includeFilesFilePath = $targetDir.'/autoload_files.php'; + $includeFilesFilePath = $targetDir.'/autoload_files.php'; if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { file_put_contents($includeFilesFilePath, $includeFilesFile); } else if (file_exists($includeFilesFilePath) === true) { - unlink($includeFilesFilePath); - } + unlink($includeFilesFilePath); + } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFile, $targetDirLoader, (bool) $includeFilesFile, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $classMapAuthoritative)); From 457bc20c34e4e1ac2ba8868184822495b5300a1c Mon Sep 17 00:00:00 2001 From: voroks Date: Tue, 18 Aug 2015 16:10:59 +0800 Subject: [PATCH 4/5] Explicit comparison was removed. --- src/Composer/Autoload/AutoloadGenerator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 86f954aba..90d17bcf3 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -258,7 +258,7 @@ EOF; $includeFilesFilePath = $targetDir.'/autoload_files.php'; if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { file_put_contents($includeFilesFilePath, $includeFilesFile); - } else if (file_exists($includeFilesFilePath) === true) { + } else if (file_exists($includeFilesFilePath)) { unlink($includeFilesFilePath); } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); From 154092ba1a9b05caa0c450dd947be286c4897191 Mon Sep 17 00:00:00 2001 From: voroks Date: Wed, 19 Aug 2015 15:08:27 +0800 Subject: [PATCH 5/5] Added removing file 'include_path.php' (if it is empty). --- src/Composer/Autoload/AutoloadGenerator.php | 13 ++-- .../Test/Autoload/AutoloadGeneratorTest.php | 13 +++- ...load_real_functions_with_include_paths.php | 59 +++++++++++++++++++ ...moved_include_paths_and_autolad_files.php} | 0 .../Fixtures/include_paths_functions.php | 15 +++++ ...ude_paths_functions_with_removed_extra.php | 11 ++++ 6 files changed, 103 insertions(+), 8 deletions(-) create mode 100644 tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_include_paths.php rename tests/Composer/Test/Autoload/Fixtures/{autoload_real_functions_with_removed_extra.php => autoload_real_functions_with_removed_include_paths_and_autolad_files.php} (100%) create mode 100644 tests/Composer/Test/Autoload/Fixtures/include_paths_functions.php create mode 100644 tests/Composer/Test/Autoload/Fixtures/include_paths_functions_with_removed_extra.php diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 90d17bcf3..7227166a8 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -252,17 +252,20 @@ EOF; file_put_contents($targetDir.'/autoload_namespaces.php', $namespacesFile); file_put_contents($targetDir.'/autoload_psr4.php', $psr4File); file_put_contents($targetDir.'/autoload_classmap.php', $classmapFile); - if ($includePathFile = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { - file_put_contents($targetDir.'/include_paths.php', $includePathFile); + $includePathFilePath = $targetDir.'/include_paths.php'; + if ($includePathFileContents = $this->getIncludePathsFile($packageMap, $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { + file_put_contents($includePathFilePath, $includePathFileContents); + } else if (file_exists($includePathFilePath)){ + unlink($includePathFilePath); } $includeFilesFilePath = $targetDir.'/autoload_files.php'; - if ($includeFilesFile = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { - file_put_contents($includeFilesFilePath, $includeFilesFile); + if ($includeFilesFileContents = $this->getIncludeFilesFile($autoloads['files'], $filesystem, $basePath, $vendorPath, $vendorPathCode52, $appBaseDirCode)) { + file_put_contents($includeFilesFilePath, $includeFilesFileContents); } else if (file_exists($includeFilesFilePath)) { unlink($includeFilesFilePath); } file_put_contents($vendorPath.'/autoload.php', $this->getAutoloadFile($vendorPathToTargetDirCode, $suffix)); - file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFile, $targetDirLoader, (bool) $includeFilesFile, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $classMapAuthoritative)); + file_put_contents($targetDir.'/autoload_real.php', $this->getAutoloadRealFile(true, (bool) $includePathFileContents, $targetDirLoader, (bool) $includeFilesFileContents, $vendorPathCode, $appBaseDirCode, $suffix, $useGlobalIncludePath, $prependAutoloader, $classMapAuthoritative)); $this->safeCopy(__DIR__.'/ClassLoader.php', $targetDir.'/ClassLoader.php'); $this->safeCopy(__DIR__.'/../../../LICENSE', $targetDir.'/LICENSE'); diff --git a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php index 08f9cc09d..007f16363 100644 --- a/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php +++ b/tests/Composer/Test/Autoload/AutoloadGeneratorTest.php @@ -589,6 +589,7 @@ class AutoloadGeneratorTest extends TestCase { $autoloadPackage = new Package('a', '1.0', '1.0'); $autoloadPackage->setAutoload(array('files' => array('root.php'))); + $autoloadPackage->setIncludePaths(array('/lib', '/src')); $notAutoloadPackage = new Package('a', '1.0', '1.0'); @@ -597,8 +598,11 @@ class AutoloadGeneratorTest extends TestCase $autoloadPackages[] = $b = new Package('b/b', '1.0', '1.0'); $autoloadPackages[] = $c = new Package('c/c', '1.0', '1.0'); $a->setAutoload(array('files' => array('test.php'))); + $a->setIncludePaths(array('lib1', 'src1')); $b->setAutoload(array('files' => array('test2.php'))); + $b->setIncludePaths(array('lib2')); $c->setAutoload(array('files' => array('test3.php', 'foo/bar/test4.php'))); + $c->setIncludePaths(array('lib3')); $c->setTargetDir('foo/bar'); $notAutoloadPackages = array(); @@ -629,18 +633,21 @@ class AutoloadGeneratorTest extends TestCase $this->generator->dump($this->config, $this->repository, $autoloadPackage, $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_real.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions.php', $this->vendorDir.'/composer/autoload_files.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions.php', $this->vendorDir.'/composer/include_paths.php'); $this->generator->dump($this->config, $this->repository, $autoloadPackage, $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_real.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_include_paths.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_files_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_files.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/include_paths_functions_with_removed_extra.php', $this->vendorDir.'/composer/include_paths.php'); $this->generator->dump($this->config, $this->repository, $notAutoloadPackage, $this->im, 'composer', false, 'FilesAutoload'); $this->assertFileEquals(__DIR__.'/Fixtures/autoload_functions.php', $this->vendorDir.'/autoload.php'); - $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_extra.php', $this->vendorDir.'/composer/autoload_real.php'); + $this->assertFileEquals(__DIR__.'/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php', $this->vendorDir.'/composer/autoload_real.php'); $this->assertFileNotExists($this->vendorDir.'/composer/autoload_files.php'); + $this->assertFileNotExists($this->vendorDir.'/composer/include_paths.php'); } public function testFilesAutoloadOrderByDependencies() diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_include_paths.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_include_paths.php new file mode 100644 index 000000000..68fb6ecbf --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_include_paths.php @@ -0,0 +1,59 @@ + $path) { + $loader->set($namespace, $path); + } + + $map = require __DIR__ . '/autoload_psr4.php'; + foreach ($map as $namespace => $path) { + $loader->setPsr4($namespace, $path); + } + + $classMap = require __DIR__ . '/autoload_classmap.php'; + if ($classMap) { + $loader->addClassMap($classMap); + } + + $loader->register(true); + + $includeFiles = require __DIR__ . '/autoload_files.php'; + foreach ($includeFiles as $file) { + composerRequireFilesAutoload($file); + } + + return $loader; + } +} + +function composerRequireFilesAutoload($file) +{ + require $file; +} diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_removed_extra.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php similarity index 100% rename from tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_removed_extra.php rename to tests/Composer/Test/Autoload/Fixtures/autoload_real_functions_with_removed_include_paths_and_autolad_files.php diff --git a/tests/Composer/Test/Autoload/Fixtures/include_paths_functions.php b/tests/Composer/Test/Autoload/Fixtures/include_paths_functions.php new file mode 100644 index 000000000..f1095cf67 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/include_paths_functions.php @@ -0,0 +1,15 @@ +