From 0a2846cb246ca8bf1430ee1f1f15be6924066e9a Mon Sep 17 00:00:00 2001 From: Jordi Boggiano Date: Fri, 27 Jan 2023 16:26:04 +0100 Subject: [PATCH] Fix autoloading on PHP 5.6 (#11285) * Add a CI check to ensure the autoloader can run on php5.6+ * Fix autoloading on PHP 5.6 --- .github/workflows/autoloader.yml | 40 +++++++++++++++++++ src/Composer/Autoload/AutoloadGenerator.php | 2 +- src/Composer/Autoload/ClassLoader.php | 8 +++- .../autoload_real_files_by_dependency.php | 2 +- .../Fixtures/autoload_real_functions.php | 2 +- ...load_real_functions_with_include_paths.php | 2 +- .../Fixtures/autoload_real_target_dir.php | 2 +- .../Autoload/MinimumVersionSupport/Foo.php | 14 +++++++ .../MinimumVersionSupport/composer.json | 11 +++++ .../Autoload/MinimumVersionSupport/main.php | 9 +++++ .../Autoload/MinimumVersionSupport/test.php | 3 ++ 11 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/autoloader.yml create mode 100644 tests/Composer/Test/Autoload/MinimumVersionSupport/Foo.php create mode 100644 tests/Composer/Test/Autoload/MinimumVersionSupport/composer.json create mode 100644 tests/Composer/Test/Autoload/MinimumVersionSupport/main.php create mode 100644 tests/Composer/Test/Autoload/MinimumVersionSupport/test.php diff --git a/.github/workflows/autoloader.yml b/.github/workflows/autoloader.yml new file mode 100644 index 000000000..5955d5304 --- /dev/null +++ b/.github/workflows/autoloader.yml @@ -0,0 +1,40 @@ +name: "Autoloader" + +on: + push: + paths-ignore: + - 'doc/**' + pull_request: + paths-ignore: + - 'doc/**' + +permissions: + contents: read + +jobs: + tests: + name: "Autoloader" + + runs-on: ubuntu-latest + + steps: + - name: "Checkout" + uses: "actions/checkout@v3" + + - name: "Install Composer dependencies" + run: "composer config platform --unset && composer install" + + - name: "Dump autoloader in the test directory using latest Composer" + run: "./bin/composer install -d tests/Composer/Test/Autoload/MinimumVersionSupport" + + - name: "Install oldest supported PHP version for autoloader" + uses: "shivammathur/setup-php@v2" + with: + coverage: "none" + extensions: "intl, zip" + ini-values: "memory_limit=-1" + php-version: "5.6" + + - name: "Check the autoloader can be executed" + run: "php main.php" + working-directory: tests/Composer/Test/Autoload/MinimumVersionSupport diff --git a/src/Composer/Autoload/AutoloadGenerator.php b/src/Composer/Autoload/AutoloadGenerator.php index fc097a683..a16e6b67b 100644 --- a/src/Composer/Autoload/AutoloadGenerator.php +++ b/src/Composer/Autoload/AutoloadGenerator.php @@ -1029,7 +1029,7 @@ REGISTER_LOADER; } }, null, null); foreach (\$filesToLoad as \$fileIdentifier => \$file) { - (\$requireFile)(\$fileIdentifier, \$file); + \$requireFile(\$fileIdentifier, \$file); } diff --git a/src/Composer/Autoload/ClassLoader.php b/src/Composer/Autoload/ClassLoader.php index 44ee4c819..a72151c77 100644 --- a/src/Composer/Autoload/ClassLoader.php +++ b/src/Composer/Autoload/ClassLoader.php @@ -429,7 +429,8 @@ class ClassLoader public function loadClass($class) { if ($file = $this->findFile($class)) { - (self::$includeFile)($file); + $includeFile = self::$includeFile; + $includeFile($file); return true; } @@ -560,7 +561,10 @@ class ClassLoader return false; } - private static function initializeIncludeClosure(): void + /** + * @return void + */ + private static function initializeIncludeClosure() { if (self::$includeFile !== null) { return; 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 e9344b8a8..b796e5613 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 @@ -40,7 +40,7 @@ class ComposerAutoloaderInitFilesAutoloadOrder } }, null, null); foreach ($filesToLoad as $fileIdentifier => $file) { - ($requireFile)($fileIdentifier, $file); + $requireFile($fileIdentifier, $file); } return $loader; diff --git a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php index 8c3403612..a6122a21e 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_functions.php @@ -40,7 +40,7 @@ class ComposerAutoloaderInitFilesAutoload } }, null, null); foreach ($filesToLoad as $fileIdentifier => $file) { - ($requireFile)($fileIdentifier, $file); + $requireFile($fileIdentifier, $file); } return $loader; 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 164a26309..aebf8cfe6 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 @@ -44,7 +44,7 @@ class ComposerAutoloaderInitFilesAutoload } }, null, null); foreach ($filesToLoad as $fileIdentifier => $file) { - ($requireFile)($fileIdentifier, $file); + $requireFile($fileIdentifier, $file); } return $loader; 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 99d784bc3..0f9494393 100644 --- a/tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php +++ b/tests/Composer/Test/Autoload/Fixtures/autoload_real_target_dir.php @@ -42,7 +42,7 @@ class ComposerAutoloaderInitTargetDir } }, null, null); foreach ($filesToLoad as $fileIdentifier => $file) { - ($requireFile)($fileIdentifier, $file); + $requireFile($fileIdentifier, $file); } return $loader; diff --git a/tests/Composer/Test/Autoload/MinimumVersionSupport/Foo.php b/tests/Composer/Test/Autoload/MinimumVersionSupport/Foo.php new file mode 100644 index 000000000..fe50969f2 --- /dev/null +++ b/tests/Composer/Test/Autoload/MinimumVersionSupport/Foo.php @@ -0,0 +1,14 @@ +