123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182 |
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Composer;
- use Symfony\Component\Finder\Finder;
- use Symfony\Component\Process\Process;
- /**
- * The Compiler class compiles composer into a phar
- *
- * @author Fabien Potencier <fabien@symfony.com>
- * @author Jordi Boggiano <j.boggiano@seld.be>
- */
- class Compiler
- {
- /**
- * Compiles composer into a single phar file
- *
- * @throws \RuntimeException
- * @param string $pharFile The full path to the file to create
- */
- public function compile($pharFile = 'composer.phar')
- {
- if (file_exists($pharFile)) {
- unlink($pharFile);
- }
- $process = new Process('git log --pretty="%h" -n1 HEAD', __DIR__);
- if ($process->run() != 0) {
- 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.');
- }
- $this->version = trim($process->getOutput());
- $process = new Process('git describe --tags HEAD');
- if ($process->run() == 0) {
- $this->version = trim($process->getOutput());
- }
- $phar = new \Phar($pharFile, 0, 'composer.phar');
- $phar->setSignatureAlgorithm(\Phar::SHA1);
- $phar->startBuffering();
- $finder = new Finder();
- $finder->files()
- ->ignoreVCS(true)
- ->name('*.php')
- ->notName('Compiler.php')
- ->notName('ClassLoader.php')
- ->in(__DIR__.'/..')
- ;
- foreach ($finder as $file) {
- $this->addFile($phar, $file);
- }
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/Autoload/ClassLoader.php'), false);
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../res/composer-schema.json'), false);
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../src/Composer/IO/hiddeninput.exe'), false);
- $finder = new Finder();
- $finder->files()
- ->ignoreVCS(true)
- ->name('*.php')
- ->exclude('Tests')
- ->in(__DIR__.'/../../vendor/symfony/')
- ->in(__DIR__.'/../../vendor/seld/jsonlint/src/')
- ->in(__DIR__.'/../../vendor/justinrainbow/json-schema/src/')
- ;
- foreach ($finder as $file) {
- $this->addFile($phar, $file);
- }
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/autoload.php'));
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_namespaces.php'));
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/autoload_classmap.php'));
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/composer/ClassLoader.php'));
- $this->addComposerBin($phar);
- // Stubs
- $phar->setStub($this->getStub());
- $phar->stopBuffering();
- // disabled for interoperability with systems without gzip ext
- // $phar->compressFiles(\Phar::GZ);
- $this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
- unset($phar);
- }
- private function addFile($phar, $file, $strip = true)
- {
- $path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
- $content = file_get_contents($file);
- if ($strip) {
- $content = $this->stripWhitespace($content);
- } elseif ('LICENSE' === basename($file)) {
- $content = "\n".$content."\n";
- }
- $content = str_replace('@package_version@', $this->version, $content);
- $phar->addFromString($path, $content);
- }
- private function addComposerBin($phar)
- {
- $content = file_get_contents(__DIR__.'/../../bin/composer');
- $content = preg_replace('{^#!/usr/bin/env php\s*}', '', $content);
- $phar->addFromString('bin/composer', $content);
- }
- /**
- * Removes whitespace from a PHP source string while preserving line numbers.
- *
- * @param string $source A PHP string
- * @return string The PHP string with the whitespace removed
- */
- private function stripWhitespace($source)
- {
- if (!function_exists('token_get_all')) {
- return $source;
- }
- $output = '';
- foreach (token_get_all($source) as $token) {
- if (is_string($token)) {
- $output .= $token;
- } elseif (in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
- $output .= str_repeat("\n", substr_count($token[1], "\n"));
- } elseif (T_WHITESPACE === $token[0]) {
- // reduce wide spaces
- $whitespace = preg_replace('{[ \t]+}', ' ', $token[1]);
- // normalize newlines to \n
- $whitespace = preg_replace('{(?:\r\n|\r|\n)}', "\n", $whitespace);
- // trim leading spaces
- $whitespace = preg_replace('{\n +}', "\n", $whitespace);
- $output .= $whitespace;
- } else {
- $output .= $token[1];
- }
- }
- return $output;
- }
- private function getStub()
- {
- return <<<'EOF'
- #!/usr/bin/env php
- <?php
- /*
- * This file is part of Composer.
- *
- * (c) Nils Adermann <naderman@naderman.de>
- * Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view
- * the license that is located at the bottom of this file.
- */
- Phar::mapPhar('composer.phar');
- require 'phar://composer.phar/bin/composer';
- __HALT_COMPILER();
- EOF;
- }
- }
|