Compiler.php 6.2 KB

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