Compiler.php 6.4 KB

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