Compiler.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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', __DIR__);
  34. if ($process->run() != 0) {
  35. throw new \RuntimeException('Can\'t run git log. You must ensure to run compile from composer git repository clone and that git binary is available.');
  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__.'/../../src/Composer/IO/hiddeninput.exe'), false);
  59. $finder = new Finder();
  60. $finder->files()
  61. ->ignoreVCS(true)
  62. ->name('*.php')
  63. ->exclude('Tests')
  64. ->in(__DIR__.'/../../vendor/symfony/')
  65. ->in(__DIR__.'/../../vendor/seld/jsonlint/src/')
  66. ->in(__DIR__.'/../../vendor/justinrainbow/json-schema/src/')
  67. ;
  68. foreach ($finder as $file) {
  69. $this->addFile($phar, $file);
  70. }
  71. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/ClassLoader.php'));
  72. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload.php'));
  73. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload_namespaces.php'));
  74. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload_classmap.php'));
  75. $this->addComposerBin($phar);
  76. // Stubs
  77. $phar->setStub($this->getStub());
  78. $phar->stopBuffering();
  79. // disabled for interoperability with systems without gzip ext
  80. // $phar->compressFiles(\Phar::GZ);
  81. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
  82. unset($phar);
  83. }
  84. private function addFile($phar, $file, $strip = true)
  85. {
  86. $path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
  87. $content = file_get_contents($file);
  88. if ($strip) {
  89. $content = $this->stripWhitespace($content);
  90. } elseif ('LICENSE' === basename($file)) {
  91. $content = "\n".$content."\n";
  92. }
  93. $content = str_replace('@package_version@', $this->version, $content);
  94. $phar->addFromString($path, $content);
  95. }
  96. private function addComposerBin($phar)
  97. {
  98. $content = file_get_contents(__DIR__.'/../../bin/composer');
  99. $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
  100. $phar->addFromString('bin/composer', $content);
  101. }
  102. /**
  103. * Removes whitespace from a PHP source string while preserving line numbers.
  104. *
  105. * @param string $source A PHP string
  106. * @return string The PHP string with the whitespace removed
  107. */
  108. private function stripWhitespace($source)
  109. {
  110. if (!function_exists('token_get_all')) {
  111. return $source;
  112. }
  113. $output = '';
  114. foreach (token_get_all($source) as $token) {
  115. if (is_string($token)) {
  116. $output .= $token;
  117. } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  118. $output .= str_repeat("\n", substr_count($token[1], "\n"));
  119. } elseif (T_WHITESPACE === $token[0]) {
  120. // reduce wide spaces
  121. $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
  122. // normalize newlines to \n
  123. $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
  124. // trim leading spaces
  125. $whitespace = preg_replace('{\n +}', "\n", $whitespace);
  126. $output .= $whitespace;
  127. } else {
  128. $output .= $token[1];
  129. }
  130. }
  131. return $output;
  132. }
  133. private function getStub()
  134. {
  135. return <<<'EOF'
  136. #!/usr/bin/env php
  137. <?php
  138. /*
  139. * This file is part of Composer.
  140. *
  141. * (c) Nils Adermann <naderman@naderman.de>
  142. * Jordi Boggiano <j.boggiano@seld.be>
  143. *
  144. * For the full copyright and license information, please view
  145. * the license that is located at the bottom of this file.
  146. */
  147. Phar::mapPhar('composer.phar');
  148. require 'phar://composer.phar/bin/composer';
  149. __HALT_COMPILER();
  150. EOF;
  151. }
  152. }