From 3748c11709ef0578709d34b201ff699b5846749f Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Sat, 21 Nov 2015 18:30:35 +0000 Subject: [PATCH] Simplify files autoload include function, and make sure files are included once per package even if exactly the same, refs #4186 --- src/Composer/Autoload/AutoloadGenerator.php | 17 ++++++----------- .../Test/Autoload/Fixtures/autoload_files2.php | 2 +- .../Fixtures/autoload_files_functions.php | 10 +++++----- ...oload_files_functions_with_removed_extra.php | 2 +- .../Fixtures/autoload_files_target_dir.php | 4 ++-- .../autoload_real_files_by_dependency.php | 8 ++------ .../Fixtures/autoload_real_functions.php | 8 ++------ ...toload_real_functions_with_include_paths.php | 12 ++++++++---- .../Fixtures/autoload_real_target_dir.php | 8 ++------ 9 files changed, 29 insertions(+), 42 deletions(-) diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index 9d9583e55..026f22843 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -476,7 +476,7 @@ EOF; { $filesCode = ''; foreach ($files as $fileIdentifier => $functionFile) { - $filesCode .= ' ' . $fileIdentifier . ' => ' + $filesCode .= ' ' . var_export($fileIdentifier, true) . ' => ' . $this->getPathCode($filesystem, $basePath, $vendorPath, $functionFile) . ",\n"; } @@ -671,14 +671,10 @@ METHOD_FOOTER; function composerRequire$suffix(\$fileIdentifier, \$file) { - if (empty(\$GLOBALS['composerRequiredFiles'])) { - \$GLOBALS['composerRequiredFiles'] = array(); - } - - if (empty(\$GLOBALS['composerRequiredFiles'][\$fileIdentifier])) { + if (empty(\$GLOBALS['__composer_autoload_files'][\$fileIdentifier])) { require \$file; - \$GLOBALS['composerRequiredFiles'][\$fileIdentifier] = true; + \$GLOBALS['__composer_autoload_files'][\$fileIdentifier] = true; } } @@ -752,8 +748,7 @@ FOOTER; $relativePath = empty($installPath) ? (empty($path) ? '.' : $path) : $installPath.'/'.$path; if ($type === 'files') { - $autoloads[var_export($this->getFileIdentifier($relativePath), true)] - = $relativePath; + $autoloads[$this->getFileIdentifier($package, $relativePath)] = $relativePath; continue; } elseif ($type === 'classmap') { $autoloads[] = $relativePath; @@ -768,9 +763,9 @@ FOOTER; return $autoloads; } - protected function getFileIdentifier($path) + protected function getFileIdentifier(PackageInterface $package, $path) { - return md5_file($path); + return md5($package->getName() . ':' . str_replace("\r\n", "\n", file_get_contents($path))); } /** diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_files2.php b/tests/Composer/Test/Autoload/Fixtures/autoload_files2.php index 3b10ed004..b23ccba93 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_files2.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_files2.php @@ -6,5 +6,5 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( - 'fc1e99cd93a6aa58e3b1565e688bcd87' => $baseDir . '/devfiles/foo.php', + 'ca64fb7d3f21566080b945f028f30a2e' => $baseDir . '/devfiles/foo.php', ); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions.php b/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions.php index 502239a93..ae7ffe461 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_files_functions.php @@ -6,9 +6,9 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( - 'b18a3c4a24b1dc69fdd96098e5c28e01' => $vendorDir . '/a/a/test.php', - '93797e35718fdafb825b1fe25c08a92c' => $vendorDir . '/b/b/test2.php', - 'a57008eef928ced7c299bfea724d8441' => $vendorDir . '/c/c/foo/bar/test3.php', - '32e13a06a803ac6553a93454bcd3d2da' => $vendorDir . '/c/c/foo/bar/test4.php', - '504bb142db8acef729eeeb06b0aedec5' => $baseDir . '/root.php', + '358464b1e68dac136ef7d2ec6242b425' => $vendorDir . '/a/a/test.php', + '03c187060069af833190628645c24aca' => $vendorDir . '/b/b/test2.php', + 'e6a1485c8598883535f318df5b64632f' => $vendorDir . '/c/c/foo/bar/test3.php', + 'c1fb30d7fed4befd8653c015f6c004f6' => $vendorDir . '/c/c/foo/bar/test4.php', + '168fcd186353db6d3200ede690c8cfa8' => $baseDir . '/root.php', ); 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 index 8d124df37..725839abe 100644 --- 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 @@ -6,5 +6,5 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( - $baseDir . '/root.php', + '168fcd186353db6d3200ede690c8cfa8' => $baseDir . '/root.php', ); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_files_target_dir.php b/tests/Composer/Test/Autoload/Fixtures/autoload_files_target_dir.php index 9ffb4b7e2..d5bfd778e 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_files_target_dir.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_files_target_dir.php @@ -6,6 +6,6 @@ $vendorDir = dirname(dirname(__FILE__)); $baseDir = dirname($vendorDir); return array( - '25b429360a61ca2629fdf9a4f484981c' => $baseDir . '/foo.php', - '524f65941cc9a0fa65ff0ec097ccde8a' => $baseDir . '/bar.php', + '8d2db77ca7611f10a41ca43619ad167f' => $baseDir . '/foo.php', + '54c6b911effcf924f7ff3e89a29f9217' => $baseDir . '/bar.php', ); diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php index 978077f55..7c7b6994a 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_files_by_dependency.php @@ -51,13 +51,9 @@ class ComposerAutoloaderInitFilesAutoloadOrder function composerRequireFilesAutoloadOrder($fileIdentifier, $file) { - if (empty($GLOBALS['composerRequiredFiles'])) { - $GLOBALS['composerRequiredFiles'] = array(); - } - - if (empty($GLOBALS['composerRequiredFiles'][$fileIdentifier])) { + if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; - $GLOBALS['composerRequiredFiles'][$fileIdentifier] = true; + $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } } diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php index cb9ab3e73..e7c22c88d 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php @@ -51,13 +51,9 @@ class ComposerAutoloaderInitFilesAutoload function composerRequireFilesAutoload($fileIdentifier, $file) { - if (empty($GLOBALS['composerRequiredFiles'])) { - $GLOBALS['composerRequiredFiles'] = array(); - } - - if (empty($GLOBALS['composerRequiredFiles'][$fileIdentifier])) { + if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; - $GLOBALS['composerRequiredFiles'][$fileIdentifier] = true; + $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } } 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 index 68fb6ecbf..75bc332af 100644 --- 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 @@ -45,15 +45,19 @@ class ComposerAutoloaderInitFilesAutoload $loader->register(true); $includeFiles = require __DIR__ . '/autoload_files.php'; - foreach ($includeFiles as $file) { - composerRequireFilesAutoload($file); + foreach ($includeFiles as $fileIdentifier => $file) { + composerRequireFilesAutoload($fileIdentifier, $file); } return $loader; } } -function composerRequireFilesAutoload($file) +function composerRequireFilesAutoload($fileIdentifier, $file) { - require $file; + if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { + require $file; + + $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; + } } diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php index 7fc42d325..1058b7e91 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php @@ -71,13 +71,9 @@ class ComposerAutoloaderInitTargetDir function composerRequireTargetDir($fileIdentifier, $file) { - if (empty($GLOBALS['composerRequiredFiles'])) { - $GLOBALS['composerRequiredFiles'] = array(); - } - - if (empty($GLOBALS['composerRequiredFiles'][$fileIdentifier])) { + if (empty($GLOBALS['__composer_autoload_files'][$fileIdentifier])) { require $file; - $GLOBALS['composerRequiredFiles'][$fileIdentifier] = true; + $GLOBALS['__composer_autoload_files'][$fileIdentifier] = true; } }