diff --git a/fixtures/set035-composer-files-autoload/test.php b/fixtures/set035-composer-files-autoload/test.php index 9db720284..03c636ade 100644 --- a/fixtures/set035-composer-files-autoload/test.php +++ b/fixtures/set035-composer-files-autoload/test.php @@ -2,7 +2,7 @@ $output = file_get_contents(__DIR__.'/output'); -$functionAutoloadFailed = 0 === preg_match( +$functionAutoloadFailed = 1 === preg_match( '#PHP Fatal error: Uncaught Error: Call to undefined function GuzzleHttp\\describe_type\(\)#', $output, ); diff --git a/src/Autoload/ScoperAutoloadGenerator.php b/src/Autoload/ScoperAutoloadGenerator.php index 90b71f1a9..881131f1b 100644 --- a/src/Autoload/ScoperAutoloadGenerator.php +++ b/src/Autoload/ScoperAutoloadGenerator.php @@ -87,8 +87,21 @@ public function dump(): string // scoper-autoload.php @generated by PhpScoper namespace { + // Backup the autoloaded Composer files + if (isset(\$GLOBALS['__composer_autoload_files'])) { + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; + } + \$loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + + // Restore the backup + if (isset(\$existingComposerAutoloadFiles)) { + \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; + } else { + unset(\$GLOBALS['__composer_autoload_files']); + } } {$statements} @@ -104,9 +117,22 @@ public function dump(): string // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset(\$GLOBALS['__composer_autoload_files'])) { + \$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files']; + } + \$loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset(\$existingComposerAutoloadFiles)) { + \$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles; + } else { + unset(\$GLOBALS['__composer_autoload_files']); + } + {$statements} return \$loader; diff --git a/tests/Autoload/ScoperAutoloadGeneratorTest.php b/tests/Autoload/ScoperAutoloadGeneratorTest.php index a58dfdc81..16ad0c92f 100644 --- a/tests/Autoload/ScoperAutoloadGeneratorTest.php +++ b/tests/Autoload/ScoperAutoloadGeneratorTest.php @@ -44,9 +44,22 @@ public static function provideRegistry(): iterable // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } + return $loader; PHP, @@ -65,9 +78,22 @@ public static function provideRegistry(): iterable // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } + // Function aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases if (!function_exists('bar')) { function bar() { return \Humbug\bar(...func_get_args()); } } @@ -91,9 +117,22 @@ public static function provideRegistry(): iterable // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } + // Function aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases if (!function_exists('bar')) { function bar() { return \Humbug\bar(...func_get_args()); } } @@ -119,8 +158,21 @@ public static function provideRegistry(): iterable // scoper-autoload.php @generated by PhpScoper namespace { + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } } // Function aliases. For more information see: @@ -156,8 +208,21 @@ public static function provideRegistry(): iterable // scoper-autoload.php @generated by PhpScoper namespace { + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } } // Function aliases. For more information see: @@ -190,9 +255,22 @@ public static function provideRegistry(): iterable // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } + // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases function humbug_phpscoper_expose_class(string $exposed, string $prefixed): void { @@ -220,9 +298,22 @@ function humbug_phpscoper_expose_class(string $exposed, string $prefixed): void // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } + // Class aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#class-aliases function humbug_phpscoper_expose_class(string $exposed, string $prefixed): void { @@ -257,8 +348,21 @@ function humbug_phpscoper_expose_class(string $exposed, string $prefixed): void // scoper-autoload.php @generated by PhpScoper namespace { + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } } // Class aliases. For more information see: @@ -308,9 +412,22 @@ function humbug_phpscoper_expose_class(string $exposed, string $prefixed): void // scoper-autoload.php @generated by PhpScoper + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } + // Function aliases. For more information see: // https://github.com/humbug/php-scoper/blob/master/docs/further-reading.md#function-aliases if (!function_exists('__autoload')) { function __autoload($className) { return \Humbug\__autoload(...func_get_args()); } } @@ -335,8 +452,21 @@ function humbug_phpscoper_expose_class(string $exposed, string $prefixed): void // scoper-autoload.php @generated by PhpScoper namespace { + // Backup the autoloaded Composer files + if (isset($GLOBALS['__composer_autoload_files'])) { + $existingComposerAutoloadFiles = $GLOBALS['__composer_autoload_files']; + } + $loader = require_once __DIR__.'/autoload.php'; + // Ensure InstalledVersions is available require_once __DIR__.'/composer/InstalledVersions.php'; + + // Restore the backup + if (isset($existingComposerAutoloadFiles)) { + $GLOBALS['__composer_autoload_files'] = $existingComposerAutoloadFiles; + } else { + unset($GLOBALS['__composer_autoload_files']); + } } // Function aliases. For more information see: