Compiler.php 7.0 KB

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