Compiler.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 or compile was run not inside git repository folder.');
  36. }
  37. $this->version = trim($process->getOutput());
  38. $process = new Process('git describe --tags HEAD');
  39. if ($process->run() == 0) {
  40. $this->version = trim($process->getOutput());
  41. }
  42. $phar = new \Phar($pharFile, 0, 'composer.phar');
  43. $phar->setSignatureAlgorithm(\Phar::SHA1);
  44. $phar->startBuffering();
  45. $finder = new Finder();
  46. $finder->files()
  47. ->ignoreVCS(true)
  48. ->name('*.php')
  49. ->notName('Compiler.php')
  50. ->notName('ClassLoader.php')
  51. ->in(__DIR__.'/..')
  52. ;
  53. foreach ($finder as $file) {
  54. $this->addFile($phar, $file);
  55. }
  56. $this->addFile($phar, new \SplFileInfo(__DIR__.'/Autoload/ClassLoader.php'), false);
  57. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../res/composer-schema.json'), false);
  58. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../res/composer-schema-lax.json'), false);
  59. $finder = new Finder();
  60. $finder->files()
  61. ->ignoreVCS(true)
  62. ->name('*.php')
  63. ->in(__DIR__.'/../../vendor/symfony/')
  64. ->in(__DIR__.'/../../vendor/seld/jsonlint/src/')
  65. ->in(__DIR__.'/../../vendor/justinrainbow/json-schema/src/')
  66. ;
  67. foreach ($finder as $file) {
  68. $this->addFile($phar, $file);
  69. }
  70. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/ClassLoader.php'));
  71. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload.php'));
  72. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload_namespaces.php'));
  73. $this->addComposerBin($phar);
  74. // Stubs
  75. $phar->setStub($this->getStub());
  76. $phar->stopBuffering();
  77. // disabled for interoperability with systems without gzip ext
  78. // $phar->compressFiles(\Phar::GZ);
  79. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
  80. unset($phar);
  81. }
  82. private function addFile($phar, $file, $strip = true)
  83. {
  84. $path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
  85. if ($strip) {
  86. $content = php_strip_whitespace($file);
  87. } elseif ('LICENSE' === basename($file)) {
  88. $content = "\n".file_get_contents($file)."\n";
  89. } else {
  90. $content = file_get_contents($file);
  91. }
  92. $content = str_replace('@package_version@', $this->version, $content);
  93. $phar->addFromString($path, $content);
  94. }
  95. private function addComposerBin($phar)
  96. {
  97. $content = file_get_contents(__DIR__.'/../../bin/composer');
  98. $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
  99. $phar->addFromString('bin/composer', $content);
  100. }
  101. private function getStub()
  102. {
  103. return <<<'EOF'
  104. #!/usr/bin/env php
  105. <?php
  106. /*
  107. * This file is part of Composer.
  108. *
  109. * (c) Nils Adermann <naderman@naderman.de>
  110. * Jordi Boggiano <j.boggiano@seld.be>
  111. *
  112. * For the full copyright and license information, please view
  113. * the license that is located at the bottom of this file.
  114. */
  115. Phar::mapPhar('composer.phar');
  116. require 'phar://composer.phar/bin/composer';
  117. __HALT_COMPILER();
  118. EOF;
  119. }
  120. }