Code execution after __halt_compiler

The next time in my life, I see some bytecode after the __halt_compiler function. In this case, it is a symfony installer. How to do it? How to generate code that will be executed after __halt_compiler?

+5
source share
1 answer

I think you mean https://symfony.com/installer , PHAR , which starts as follows:

#!/usr/bin/env php <?php /** * Generated by Box. * * @link https://github.com/herrera-io/php-box/ */ if (class_exists('Phar')) { Phar::mapPhar('default.phar'); require 'phar://' . __FILE__ . '/symfony'; } __HALT_COMPILER(); ?> 

Conceptually speaking, PHAR archives are not too different from self-extracting ZIP files (in fact, ZIP is one of the supported internal formats). The archive contains :

  • A stub , which is a short PHP script that you can see.

  • manifest describing the contents of the archive

  • Archive with the content itself

  • Optional signature

The top-end is a regular PHP script, so the PHAR archive can work with a regular PHP interpreter, and not require a special executable file:

 php foo.phar 

This script block ends with __ HALT_COMPILER (); to ::

  • Prevent parsing the rest of the file and possibly execute it as PHP code (the main script does everything that is needed for the initial phase).

  • Fill in the __COMPILER_HALT_OFFSET__ constant __COMPILER_HALT_OFFSET__ that the PHAR library can easily find where the archived data begins.

Scenarios do three things:

  • Determine if the Phar library is available on the computer.
  • Upload the current file to the Phar archive and register a manifest with a description of the contents.
  • Run the symfony script inside the archive, which is the actual installer code.
+5
source

Source: https://habr.com/ru/post/1268484/


All Articles