Compiler.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. private $version;
  23. private $versionDate;
  24. /**
  25. * Compiles composer into a single phar file
  26. *
  27. * @throws \RuntimeException
  28. * @param string $pharFile The full path to the file to create
  29. */
  30. public function compile($pharFile = 'composer.phar')
  31. {
  32. if (file_exists($pharFile)) {
  33. unlink($pharFile);
  34. }
  35. $process = new Process('git log --pretty="%H" -n1 HEAD', __DIR__);
  36. if ($process->run() != 0) {
  37. 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.');
  38. }
  39. $this->version = trim($process->getOutput());
  40. $process = new Process('git log -n1 --pretty=%ci HEAD', __DIR__);
  41. if ($process->run() != 0) {
  42. 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.');
  43. }
  44. $date = new \DateTime(trim($process->getOutput()));
  45. $date->setTimezone(new \DateTimeZone('UTC'));
  46. $this->versionDate = $date->format('Y-m-d H:i:s');
  47. $process = new Process('git describe --tags HEAD');
  48. if ($process->run() == 0) {
  49. $this->version = trim($process->getOutput());
  50. }
  51. $phar = new \Phar($pharFile, 0, 'composer.phar');
  52. $phar->setSignatureAlgorithm(\Phar::SHA1);
  53. $phar->startBuffering();
  54. $finder = new Finder();
  55. $finder->files()
  56. ->ignoreVCS(true)
  57. ->name('*.php')
  58. ->notName('Compiler.php')
  59. ->notName('ClassLoader.php')
  60. ->in(__DIR__.'/..')
  61. ;
  62. foreach ($finder as $file) {
  63. $this->addFile($phar, $file);
  64. }
  65. $this->addFile($phar, new \SplFileInfo(__DIR__ . '/Autoload/ClassLoader.php'), false);
  66. $finder = new Finder();
  67. $finder->files()
  68. ->name('*.json')
  69. ->in(__DIR__ . '/../../res')
  70. ;
  71. foreach ($finder as $file) {
  72. $this->addFile($phar, $file, false);
  73. }
  74. $this->addFile($phar, new \SplFileInfo(__DIR__ . '/../../src/Composer/IO/hiddeninput.exe'), false);
  75. $finder = new Finder();
  76. $finder->files()
  77. ->ignoreVCS(true)
  78. ->name('*.php')
  79. ->exclude('Tests')
  80. ->in(__DIR__.'/../../vendor/symfony/')
  81. ->in(__DIR__.'/../../vendor/seld/jsonlint/src/')
  82. ->in(__DIR__.'/../../vendor/justinrainbow/json-schema/src/')
  83. ;
  84. foreach ($finder as $file) {
  85. $this->addFile($phar, $file);
  86. }
  87. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload.php'));
  88. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_namespaces.php'));
  89. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_psr4.php'));
  90. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_classmap.php'));
  91. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_real.php'));
  92. if (file_exists(__DIR__.'/../../vendor/composer/include_paths.php')) {
  93. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/include_paths.php'));
  94. }
  95. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/ClassLoader.php'));
  96. $this->addComposerBin($phar);
  97. // Stubs
  98. $phar->setStub($this->getStub());
  99. $phar->stopBuffering();
  100. // disabled for interoperability with systems without gzip ext
  101. // $phar->compressFiles(\Phar::GZ);
  102. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
  103. unset($phar);
  104. }
  105. private function addFile($phar, $file, $strip = true)
  106. {
  107. $path = strtr(str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath()), '\\', '/');
  108. $content = file_get_contents($file);
  109. if ($strip) {
  110. $content = $this->stripWhitespace($content);
  111. } elseif ('LICENSE' === basename($file)) {
  112. $content = "\n".$content."\n";
  113. }
  114. if ($path === 'src/Composer/Composer.php') {
  115. $content = str_replace('@package_version@', $this->version, $content);
  116. $content = str_replace('@release_date@', $this->versionDate, $content);
  117. }
  118. $phar->addFromString($path, $content);
  119. }
  120. private function addComposerBin($phar)
  121. {
  122. $content = file_get_contents(__DIR__.'/../../bin/composer');
  123. $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
  124. $phar->addFromString('bin/composer', $content);
  125. }
  126. /**
  127. * Removes whitespace from a PHP source string while preserving line numbers.
  128. *
  129. * @param string $source A PHP string
  130. * @return string The PHP string with the whitespace removed
  131. */
  132. private function stripWhitespace($source)
  133. {
  134. if (!function_exists('token_get_all')) {
  135. return $source;
  136. }
  137. $output = '';
  138. foreach (token_get_all($source) as $token) {
  139. if (is_string($token)) {
  140. $output .= $token;
  141. } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
  142. $output .= str_repeat("\n", substr_count($token[1], "\n"));
  143. } elseif (T_WHITESPACE === $token[0]) {
  144. // reduce wide spaces
  145. $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
  146. // normalize newlines to \n
  147. $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
  148. // trim leading spaces
  149. $whitespace = preg_replace('{\n +}', "\n", $whitespace);
  150. $output .= $whitespace;
  151. } else {
  152. $output .= $token[1];
  153. }
  154. }
  155. return $output;
  156. }
  157. private function getStub()
  158. {
  159. $stub = <<<'EOF'
  160. #!/usr/bin/env php
  161. <?php
  162. /*
  163. * This file is part of Composer.
  164. *
  165. * (c) Nils Adermann <naderman@naderman.de>
  166. * Jordi Boggiano <j.boggiano@seld.be>
  167. *
  168. * For the full copyright and license information, please view
  169. * the license that is located at the bottom of this file.
  170. */
  171. Phar::mapPhar('composer.phar');
  172. EOF;
  173. // add warning once the phar is older than 30 days
  174. if (preg_match('{^[a-f0-9]+$}', $this->version)) {
  175. $warningTime = time() + 30*86400;
  176. $stub .= "define('COMPOSER_DEV_WARNING_TIME', $warningTime);\n";
  177. }
  178. return $stub . <<<'EOF'
  179. require 'phar://composer.phar/bin/composer';
  180. __HALT_COMPILER();
  181. EOF;
  182. }
  183. }