Skip to content

Commit

Permalink
Fix the autoloading of Composer files (#774)
Browse files Browse the repository at this point in the history
Related to #298
  • Loading branch information
theofidry authored Nov 20, 2022
1 parent 2647b91 commit f7bd92f
Show file tree
Hide file tree
Showing 3 changed files with 157 additions and 1 deletion.
2 changes: 1 addition & 1 deletion fixtures/set035-composer-files-autoload/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
);
Expand Down
26 changes: 26 additions & 0 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}
Expand All @@ -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;
Expand Down
130 changes: 130 additions & 0 deletions tests/Autoload/ScoperAutoloadGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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()); } }
Expand All @@ -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()); } }
Expand All @@ -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:
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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()); } }
Expand All @@ -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:
Expand Down

0 comments on commit f7bd92f

Please sign in to comment.