title: Marking PHAR archive as PHP executable
body: <p>I'm trying to make my PHAR archive <em>marked as PHP</em> (<em>I don't know correct term for this</em>) so that I can use it from cli without <code>php</code> part. So instead of calling <code>php /path/to/my/archive.phar ARGUMENTS_HERE</code> I can just move it to <code>/usr/local/bin</code> and call it <code>archive</code> from anywhere. (Just like PHPUnit or Composer).</p>
  
  <p>If I'm right, to do that, I just need to add <code>#!/usr/bin/env php</code> on top of my PHAR archive file?</p>
  
  <p>How do I do that? I tried to mimic <a href="https://github.com/composer/composer/blob/master/src/Composer/Compiler.php" rel="nofollow">composer compiler</a> in my <strong>build</strong> script, but it just <strong>crashes PHP_CLI (segmentation fault)</strong>:</p>
  
  <pre><code>$archiveName = 'phpbenchmark.phar';
  
  $phar = new Phar($archiveName);
  
  $phar-&gt;buildFromDirectory(__DIR__, '/\/lib\/|\/vendor\//');
  
  $phar-&gt;setStub(createStub());
  
  function createStub()
  {
      $stub = &lt;&lt;&lt;EOD
  #!/usr/bin/env php
  &lt;?php
  Phar::mapPhar('phpbenchmark.phar');
  require 'phar://phpbenchmark.phar/lib/app.php';
  __HALT_COMPILER();
  EOD;
      return $stub;
  }
  </code></pre>
  
  <p>As you can see, I only need stub to run <code>/lib/app.php</code> file and that's it.</p>
  
  <h3>Prepending to compiled PHAR</h3>
  
  <p>Of course, first thing I tried was manually prepending it to my archive, but it looks like that corrupts it:</p>
  
  <pre><code>PHP Fatal error:  Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
  Stack trace:
  #0 /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar(9): Phar::webPhar(NULL, 'index.php')
  #1 {main}
    thrown in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
  
  Fatal error: Uncaught exception 'PharException' with message 'manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar"' in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
  
  PharException: manifest cannot be larger than 100 MB in phar "/home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar" in /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar on line 9
  
  Call Stack:
      0.0010     306272   1. {main}() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:0
      0.0011     307840   2. Phar::webPhar() /home/igor/Dropbox/www/Github/PIFlexPHPBenchmark/phpbenchmark.phar:9
  </code></pre>
  
  <p>Note that <strong>exactly the same</strong> archive works before I manually modify it. (But needs <code>php /path/to/arch</code> of course)</p>