box icon indicating copy to clipboard operation
box copied to clipboard

Including new autoloader when running in PHAR

Open rask opened this issue 6 years ago • 4 comments

I have a tool I'm writing and want to bundle it up with box.

It has a spot where it evals code, but before that it must include a new autoloader for the code that will be evaled, so the code finds classes and all that jazz properly when being evaled.

Assume I built a PHAR and I wish to run that PHAR against the codebase I built it from.

$ box build
$ bin/mytool.phar command ./
> PHP Fatal error:  Cannot redeclare composerRequiref0c22e411e267794e39d513db52f6d38()
    (previously declared in phar:///mytool.phar/vendor/composer/autoload_real.php:54)
    in /home/ojrask/project/mytool/vendor/composer/autoload_real.php on line 63

In the PHP-Scoper documentation it states that Box has some trick to avoid this situation, but I can't find anything related to it. I also dumped the "working copy" autoloader and tried to run the Phar but I get the same error.

If I run the PHAR against some other codebase (e.g. not against itself) then the autoloading thingy seems to work normally.

rask avatar Sep 06 '19 14:09 rask

Usually, Box tries to dump the autoloader before bundling the code in the PHAR. As a result, the autoloader within the PHAR should be unique. When using PHP-Scoper, dumping the autoloader is necessary for the scoping to work, but with Box, it already does it so you don't have any extra action to take (except if you explicitly request to not dump it).

You should be able to get more information by compiling using --debug. It will create a .box_dump directory or something with the extracted PHAR + a configuration dump.

theofidry avatar Sep 06 '19 21:09 theofidry

Okay, will have to see what the config and runtime dumps say.

rask avatar Sep 09 '19 08:09 rask

Any update @rask ?

theofidry avatar Dec 13 '19 16:12 theofidry

Sorry, been busy lately with other projects, I will set a reminder to get back to you on this.

rask avatar Dec 16 '19 09:12 rask

@rask feel free to open a new issue if you still run into a problem :)

theofidry avatar Oct 11 '22 16:10 theofidry