Compiler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer;
  12. use Symfony\Component\Finder\Finder;
  13. use Symfony\Component\Process\Process;
  14. /**
  15. * The Compiler class compiles composer into a phar
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. class Compiler
  21. {
  22. /**
  23. * Compiles composer into a single phar file
  24. *
  25. * @throws \RuntimeException
  26. * @param string $pharFile The full path to the file to create
  27. */
  28. public function compile($pharFile = 'composer.phar')
  29. {
  30. if (file_exists($pharFile)) {
  31. unlink($pharFile);
  32. }
  33. $process = new Process('git log --pretty="%h" -n1 HEAD');
  34. if ($process->run() != 0) {
  35. throw new \RuntimeException('The git binary cannot be found.');
  36. }
  37. $this->version = trim($process->getOutput());
  38. $phar = new \Phar($pharFile, 0, 'composer.phar');
  39. $phar->setSignatureAlgorithm(\Phar::SHA1);
  40. $phar->startBuffering();
  41. $finder = new Finder();
  42. $finder->files()
  43. ->ignoreVCS(true)
  44. ->name('*.php')
  45. ->notName('Compiler.php')
  46. ->in(__DIR__.'/..')
  47. ;
  48. foreach ($finder as $file) {
  49. $this->addFile($phar, $file);
  50. }
  51. $finder = new Finder();
  52. $finder->files()
  53. ->ignoreVCS(true)
  54. ->name('*.php')
  55. ->in(__DIR__.'/../../vendor/symfony/')
  56. ;
  57. foreach ($finder as $file) {
  58. $this->addFile($phar, $file);
  59. }
  60. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/ClassLoader.php'));
  61. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload.php'));
  62. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload_namespaces.php'));
  63. $this->addComposerBin($phar);
  64. // Stubs
  65. $phar->setStub($this->getStub());
  66. $phar->stopBuffering();
  67. $phar->compressFiles(\Phar::GZ);
  68. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
  69. unset($phar);
  70. }
  71. private function addFile($phar, $file, $strip = true)
  72. {
  73. $path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
  74. if ($strip) {
  75. $content = php_strip_whitespace($file);
  76. } else {
  77. $content = "\n".file_get_contents($file)."\n";
  78. }
  79. $content = str_replace('@package_version@', $this->version, $content);
  80. $phar->addFromString($path, $content);
  81. }
  82. private function addComposerBin($phar)
  83. {
  84. $content = file_get_contents(__DIR__.'/../../bin/composer');
  85. $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
  86. $phar->addFromString('bin/composer', $content);
  87. }
  88. private function getStub()
  89. {
  90. return <<<'EOF'
  91. #!/usr/bin/env php
  92. <?php
  93. /*
  94. * This file is part of Composer.
  95. *
  96. * (c) Nils Adermann <naderman@naderman.de>
  97. * Jordi Boggiano <j.boggiano@seld.be>
  98. *
  99. * For the full copyright and license information, please view
  100. * the license that is located at the bottom of this file.
  101. */
  102. Phar::mapPhar('composer.phar');
  103. require 'phar://composer.phar/bin/composer';
  104. __HALT_COMPILER();
  105. EOF;
  106. }
  107. }