Skip to content

Commit

Permalink
fix: Cleanup the autoloader from temporary variables (#866)
Browse files Browse the repository at this point in the history
The goal here is to reduce the "footprint" of PHP-Scoper and avoid
introducing unnecessary conflicts by overriding potential variables.
  • Loading branch information
theofidry authored Oct 31, 2023
1 parent 6933d7c commit cff4a1b
Show file tree
Hide file tree
Showing 4 changed files with 665 additions and 169 deletions.
66 changes: 37 additions & 29 deletions src/Autoload/ScoperAutoloadGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,42 @@ public function dump(): string
// scoper-autoload.php @generated by PhpScoper
namespace {
\$loader = (static function () {
// 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
\$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php';
if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath;
// Restore the backup
if (isset(\$existingComposerAutoloadFiles)) {
\$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles;
} else {
unset(\$GLOBALS['__composer_autoload_files']);
}
return \$loader;
})();
}
{$statements}
namespace {
return \$loader;
}
PHP;
} else {
$dump = <<<PHP
<?php
// scoper-autoload.php @generated by PhpScoper
\$loader = (static function () {
// Backup the autoloaded Composer files
if (isset(\$GLOBALS['__composer_autoload_files'])) {
\$existingComposerAutoloadFiles = \$GLOBALS['__composer_autoload_files'];
Expand All @@ -105,37 +141,9 @@ public function dump(): string
} else {
unset(\$GLOBALS['__composer_autoload_files']);
}
}
{$statements}
namespace {
return \$loader;
}
PHP;
} else {
$dump = <<<PHP
<?php
// 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
\$installedVersionsPath = __DIR__.'/composer/InstalledVersions.php';
if (file_exists(\$installedVersionsPath)) require_once \$installedVersionsPath;
// Restore the backup
if (isset(\$existingComposerAutoloadFiles)) {
\$GLOBALS['__composer_autoload_files'] = \$existingComposerAutoloadFiles;
} else {
unset(\$GLOBALS['__composer_autoload_files']);
}
})();
{$statements}
Expand Down
Loading

0 comments on commit cff4a1b

Please sign in to comment.