Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the autoloading of Composer files #774

Merged
merged 6 commits into from
Nov 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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