Closed polevaultweb closed 4 years ago
You need to do a composer dump-autoload
afterwards, it is done automatically if you are using Box but still needs to be done if you are using PHP-Scoper alone
hi @theofidry sorry of course, but I think I'm going in circles. Doing that fixes the issue, but then I lose my namespace prefix from the Composer classes, like I reported in https://github.com/humbug/php-scoper/issues/353
But that has the undesired effect of removing my custom prefix from the Composer autoloader classes.
I'm a bit confused:
--no-dev
flag when installing/dumping the autoloaderMaybe it would be easier if you shared your "build" script or "building" process?
PHP-Scoper is unable to properly scope the autoloader as it would require a very tight coupling on the autoloader mechanism; since the Composer autoloader is already pretty unique (in terms of namespace/class collision) there is also no need to actually scope it
So the main issue is the ClassLoader
class, which isn't uniquely named. I had an issue where another WordPress plugin had some packages included with an older version of Composer, their ClassLoader was loaded and setClassMapAuthoritative
didn't exist.
My build process is as you suggested in #353 to remove the dev dependencies
rm -rf ./build
mkdir ./build
cp ./composer.json ./build/composer.json
cp ./scoper.inc.php ./build/scoper.inc.php
composer install --no-dev --optimize-autoloader --working-dir build
cd build
PREFIX=MY_PREFIX
php -d memory_limit=728M ../vendor/bin/php-scoper add-prefix --prefix=${PREFIX} --no-interaction --force --output-dir=prefixed
What about performing the scoping, dumping the autoloader, then copying the prefixed/vendor/composer
dir to another directory and just scoping that and moving it back later?
So the main issue is the ClassLoader class, which isn't uniquely named. I had an issue where another WordPress plugin had some packages included with an older version of Composer, their ClassLoader was loaded and setClassMapAuthoritative didn't exist.
Hm that could be indeed; To which there is two "solutions":
I did not dig into the last one since I wanted to avoid any coupling with Composer internals. However maybe I missed something and there is a way to get it done without too much hacking, e.g. just scoping the ClassLoader
for example.
What about performing the scoping, dumping the autoloader, then copying the prefixed/vendor/composer dir to another directory and just scoping that and moving it back later?
Related to the previous point actually: the issue is that there is the Composer executable which you are using (and is not scoped) and scoping the "dumped" autoloader (`vendor/composer/autoload_*) is horribly complex.
Ok so maybe some clever manual patching of the ClassLoader class after scoping has taken place?
Either that, or we can try to find a way to do it in a more automated way. If you find a reproducible way to do it via a simple script for example, that could be enough and documented
Had a little chat about Nils about it. It looks like it won't be easy to change ClassLoader
since it could be typehinted elsewhere as well, so I only see 3 solutions:
So I hope you can give a shot to the first and report back on it! Depending on the result we can discuss further to see if there is something that can be done to make it easier
Thanks for your time and help @theofidry - will try 1. and report back 👍
@theofidry got this working for me https://gist.github.com/polevaultweb/de44d747b57191a851947d5f5175e922
👍 Might be worth adding a doc entry for it? Alternatively could even be a dedicated command added to PHP-Scoper
Where are the docs? the README.md you mean?
Alternatively could even be a dedicated command added to PHP-Scoper
That would be cool!
Yep the readme
Closing as the original issue has been fixed. Will gladly accept a PR to update the readme to include your work!
Bug report
This is related to https://github.com/humbug/php-scoper/issues/127, but it feels like a regression has happened and now PSR-4 and PSR-0 namespaces aren't prefixed in the Composer autoloader files.
Using the test repo here,
composer install
./vendor/bin/php-scoper add-prefix --output-dir=build-prefixed --force --prefix=Mutant
autoload_static.php
:The
public static $classMap = array(
is correctly prefixedautoload_real.php
ClassLoader isn't prefixed here -
autoload_psr4.php
The array key strings of the class aren't namespaced.
autoload_namespaces.php
Same as
autoload_psr4.php
.scoper.inc.php
```phpOutput
```bash $ command > output ```