Compiler.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 the Silex framework.
  16. *
  17. * @author Fabien Potencier <fabien@symfony.com>
  18. */
  19. class Compiler
  20. {
  21. protected $version;
  22. public function compile($pharFile = 'composer.phar')
  23. {
  24. if (file_exists($pharFile)) {
  25. unlink($pharFile);
  26. }
  27. $process = new Process('git log --pretty="%h %ci" -n1 HEAD');
  28. if ($process->run() > 0) {
  29. throw new \RuntimeException('The git binary cannot be found.');
  30. }
  31. $this->version = trim($process->getOutput());
  32. $phar = new \Phar($pharFile, 0, 'composer.phar');
  33. $phar->setSignatureAlgorithm(\Phar::SHA1);
  34. $phar->startBuffering();
  35. $finder = new Finder();
  36. $finder->files()
  37. ->ignoreVCS(true)
  38. ->name('*.php')
  39. ->notName('Compiler.php')
  40. ->in(__DIR__.'/../Composer')
  41. ;
  42. foreach ($finder as $file) {
  43. $this->addFile($phar, $file);
  44. }
  45. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../tests/bootstrap.php'));
  46. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../bin/composer'));
  47. // Stubs
  48. $phar->setStub($this->getStub());
  49. $phar->stopBuffering();
  50. $phar->compressFiles(\Phar::GZ);
  51. $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
  52. unset($phar);
  53. }
  54. protected function addFile($phar, $file, $strip = true)
  55. {
  56. $path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
  57. if ($strip) {
  58. $content = php_strip_whitespace($file);
  59. } else {
  60. $content = "\n".file_get_contents($file)."\n";
  61. }
  62. $content = str_replace('@package_version@', $this->version, $content);
  63. $phar->addFromString($path, $content);
  64. }
  65. protected function getStub()
  66. {
  67. return <<<'EOF'
  68. <?php
  69. /*
  70. * This file is part of Composer.
  71. *
  72. * (c) Nils Adermann <naderman@naderman.de>
  73. * Jordi Boggiano <j.boggiano@seld.be>
  74. *
  75. * For the full copyright and license information, please view
  76. * the license that is located at the bottom of this file.
  77. */
  78. Phar::mapPhar('composer.phar');
  79. require 'phar://composer.phar/bin/composer';
  80. __HALT_COMPILER();
  81. EOF;
  82. }
  83. }