Compiler.php 8.3 KB

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