Skip to content

Commit cbef87b

Browse files
authored
fix: Correct the Composer autoload (#864)
In [0.18.0-rc.0](https://github.com/humbug/php-scoper/releases/tag/0.18.0-rc.0), more specifically in #298, the composer autoload global variable is completely reset. This is actually a problem for when there is excluded files, as they now count as not loaded and if included again in another project, will be loaded, which can cause problems.
1 parent b8fc1d1 commit cbef87b

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+4226
-142
lines changed

.makefile/e2e.file

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,10 @@ e2e_034: $(PHP_SCOPER_PHAR_BIN) fixtures/set034-installed-versions/vendor
314314

315315
.PHONY: e2e_035
316316
e2e_035: # Runs end-to-end tests for the fixture set 035 — Tests tha composer autoloaded files are working fine
317-
e2e_035: $(PHP_SCOPER_PHAR_BIN) fixtures/set035-composer-files-autoload/vendor fixtures/set035-composer-files-autoload/guzzle5-include/vendor
317+
e2e_035: $(PHP_SCOPER_PHAR_BIN) \
318+
fixtures/set035-composer-files-autoload/vendor \
319+
fixtures/set035-composer-files-autoload/guzzle5-include/vendor \
320+
fixtures/set035-composer-files-autoload/composer-variable-access/vendor
318321
rm -rf build/set035-composer-files-autoload || true
319322
cp -R fixtures/set035-composer-files-autoload build/set035-composer-files-autoload
320323

@@ -328,6 +331,16 @@ e2e_035: $(PHP_SCOPER_PHAR_BIN) fixtures/set035-composer-files-autoload/vendor f
328331
composer --working-dir=build/set035-composer-files-autoload/scoped-guzzle5-include dump-autoload
329332
rm -rf build/set035-composer-files-autoload/guzzle5-include || true
330333

334+
$(PHP_SCOPER_PHAR) add-prefix \
335+
--working-dir=fixtures/set035-composer-files-autoload/composer-variable-access \
336+
--output-dir=../../../build/set035-composer-files-autoload/scoped-composer-variable-access \
337+
--force \
338+
--config=scoper.inc.php \
339+
--no-interaction \
340+
--stop-on-failure
341+
composer --working-dir=build/set035-composer-files-autoload/scoped-composer-variable-access dump-autoload
342+
rm -rf build/set035-composer-files-autoload/composer-variable-access || true
343+
331344
php build/set035-composer-files-autoload/index.php 2>&1 > build/set035-composer-files-autoload/output
332345
php build/set035-composer-files-autoload/test.php
333346

@@ -530,6 +543,13 @@ fixtures/set035-composer-files-autoload/guzzle5-include/composer.lock: fixtures/
530543
@echo "$(@) is not up to date. You may want to run the following command:"
531544
@echo "$$ composer --working-dir=fixtures/set035-composer-files-autoload/guzzle5-include update --lock && touch -c $(@)"
532545

546+
fixtures/set035-composer-files-autoload/composer-variable-access/vendor: fixtures/set035-composer-files-autoload/composer-variable-access/composer.lock
547+
composer --working-dir=fixtures/set035-composer-files-autoload/composer-variable-access install --no-dev --no-scripts
548+
touch -c $@
549+
fixtures/set035-composer-files-autoload/composer-variable-access/composer.lock: fixtures/set035-composer-files-autoload/composer-variable-access/composer.json
550+
@echo "$(@) is not up to date. You may want to run the following command:"
551+
@echo "$$ composer --working-dir=fixtures/set035-composer-files-autoload/composer-variable-access update --lock && touch -c $(@)"
552+
533553
build/set038/phpunit:
534554
rm -rf $(E2E_PHPUNIT_DIR) || true
535555
git clone --depth=1 --single-branch [email protected]:sebastianbergmann/phpunit.git $@
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php declare(strict_types=1);
2+
3+
if (!isset($GLOBALS['__composer_autoload_files'])) {
4+
// This is to mimic a scoped app that may access to the Composer related globals like
5+
// PHPStan does.
6+
echo 'Expected to be able to access the composer autoload files!'.PHP_EOL;
7+
exit(1);
8+
}
9+
10+
$composerAutoloadFiles = $GLOBALS['__composer_autoload_files'];
11+
12+
$expectedPresentComposerAutoloadFiles = [
13+
'a4a119a56e50fbb293281d9a48007e0e' => true, // vendor/symfony/polyfill-php80/bootstrap.php
14+
'60884d26763a20c18bdf80c8935efaac' => true, // included-file.php
15+
];
16+
$expectedMissingComposerAutoloadFiles = [
17+
'430aabe1de335715bfb79e58e8c22198' => true, // excluded-file.php
18+
];
19+
20+
$actualExpectedPresent = array_diff_key(
21+
$expectedPresentComposerAutoloadFiles,
22+
$composerAutoloadFiles,
23+
);
24+
$actualExpectedMissing = array_diff_key(
25+
$expectedMissingComposerAutoloadFiles,
26+
$composerAutoloadFiles,
27+
);
28+
29+
if (count($actualExpectedPresent) !== 0) {
30+
echo 'Expected the following hashes to be present:'.PHP_EOL;
31+
echo var_export($actualExpectedPresent, true).PHP_EOL;
32+
exit(1);
33+
}
34+
35+
if (count($actualExpectedMissing) !== count($expectedMissingComposerAutoloadFiles)) {
36+
echo 'Expected the following hashes to be missing:'.PHP_EOL;
37+
echo var_export($actualExpectedMissing, true).PHP_EOL;
38+
exit(1);
39+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{
2+
"bin": "index.php",
3+
"autoload": {
4+
"files": [
5+
"included-file.php",
6+
"excluded-file.php"
7+
]
8+
},
9+
"require": {
10+
"symfony/polyfill-php80": "^1.28"
11+
}
12+
}

fixtures/set035-composer-files-autoload/composer-variable-access/composer.lock

Lines changed: 102 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
if (!function_exists('test_global_function')) {
4+
function test_global_function() {}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php
2+
3+
if (!function_exists('test_global_function')) {
4+
function test_global_function() {}
5+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
<?php declare(strict_types=1);
2+
3+
require_once file_exists(__DIR__.'/vendor/scoper-autoload.php')
4+
? __DIR__.'/vendor/scoper-autoload.php'
5+
: __DIR__.'/vendor/autoload.php';
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
return [
4+
'exclude-files' => [
5+
'included-file.php',
6+
'vendor/symfony/polyfill-php80/bootstrap.php',
7+
],
8+
];
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
// autoload.php @generated by Composer
4+
5+
if (PHP_VERSION_ID < 50600) {
6+
if (!headers_sent()) {
7+
header('HTTP/1.1 500 Internal Server Error');
8+
}
9+
$err = 'Composer 2.3.0 dropped support for autoloading on PHP <5.6 and you are running '.PHP_VERSION.', please upgrade PHP or use Composer 2.2 LTS via "composer self-update --2.2". Aborting.'.PHP_EOL;
10+
if (!ini_get('display_errors')) {
11+
if (PHP_SAPI === 'cli' || PHP_SAPI === 'phpdbg') {
12+
fwrite(STDERR, $err);
13+
} elseif (!headers_sent()) {
14+
echo $err;
15+
}
16+
}
17+
trigger_error(
18+
$err,
19+
E_USER_ERROR
20+
);
21+
}
22+
23+
require_once __DIR__ . '/composer/autoload_real.php';
24+
25+
return ComposerAutoloaderInit945f0706470c9d6642a4450ed45b7c04::getLoader();

0 commit comments

Comments
 (0)