2011-08-03 21:57:27 +00:00
|
|
|
<?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;
|
2011-11-12 18:44:24 +00:00
|
|
|
use Symfony\Component\Process\Process;
|
2011-08-03 21:57:27 +00:00
|
|
|
|
|
|
|
/**
|
2011-09-14 00:34:48 +00:00
|
|
|
* The Compiler class compiles composer into a phar
|
2011-08-03 21:57:27 +00:00
|
|
|
*
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
2011-10-02 21:38:54 +00:00
|
|
|
* @author Jordi Boggiano <j.boggiano@seld.be>
|
2011-08-03 21:57:27 +00:00
|
|
|
*/
|
|
|
|
class Compiler
|
|
|
|
{
|
2011-09-14 11:52:21 +00:00
|
|
|
/**
|
|
|
|
* Compiles composer into a single phar file
|
|
|
|
*
|
|
|
|
* @throws \RuntimeException
|
|
|
|
* @param string $pharFile The full path to the file to create
|
|
|
|
*/
|
2011-08-03 21:57:27 +00:00
|
|
|
public function compile($pharFile = 'composer.phar')
|
|
|
|
{
|
|
|
|
if (file_exists($pharFile)) {
|
|
|
|
unlink($pharFile);
|
|
|
|
}
|
|
|
|
|
2011-11-12 18:44:24 +00:00
|
|
|
$process = new Process('git log --pretty="%h" -n1 HEAD');
|
2011-11-12 23:22:52 +00:00
|
|
|
if ($process->run() != 0) {
|
2011-11-12 18:44:24 +00:00
|
|
|
throw new \RuntimeException('The git binary cannot be found.');
|
|
|
|
}
|
|
|
|
$this->version = trim($process->getOutput());
|
|
|
|
|
2011-08-03 21:57:27 +00:00
|
|
|
$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')
|
2011-10-02 21:38:54 +00:00
|
|
|
->in(__DIR__.'/..')
|
2011-08-03 21:57:27 +00:00
|
|
|
;
|
|
|
|
|
|
|
|
foreach ($finder as $file) {
|
|
|
|
$this->addFile($phar, $file);
|
|
|
|
}
|
|
|
|
|
2011-11-04 10:11:06 +00:00
|
|
|
$finder = new Finder();
|
|
|
|
$finder->files()
|
|
|
|
->ignoreVCS(true)
|
|
|
|
->name('*.php')
|
|
|
|
->in(__DIR__.'/../../vendor/symfony/')
|
|
|
|
;
|
|
|
|
|
|
|
|
foreach ($finder as $file) {
|
|
|
|
$this->addFile($phar, $file);
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload.php'));
|
|
|
|
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/autoload_namespaces.php'));
|
2011-10-02 21:38:54 +00:00
|
|
|
$this->addComposerBin($phar);
|
2011-08-03 21:57:27 +00:00
|
|
|
|
|
|
|
// Stubs
|
|
|
|
$phar->setStub($this->getStub());
|
|
|
|
|
|
|
|
$phar->stopBuffering();
|
|
|
|
|
|
|
|
$phar->compressFiles(\Phar::GZ);
|
|
|
|
|
|
|
|
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../LICENSE'), false);
|
|
|
|
|
|
|
|
unset($phar);
|
|
|
|
}
|
|
|
|
|
2011-10-02 21:38:54 +00:00
|
|
|
private function addFile($phar, $file, $strip = true)
|
2011-08-03 21:57:27 +00:00
|
|
|
{
|
|
|
|
$path = str_replace(dirname(dirname(__DIR__)).DIRECTORY_SEPARATOR, '', $file->getRealPath());
|
|
|
|
|
|
|
|
if ($strip) {
|
|
|
|
$content = php_strip_whitespace($file);
|
|
|
|
} else {
|
|
|
|
$content = "\n".file_get_contents($file)."\n";
|
|
|
|
}
|
|
|
|
|
2011-11-12 18:44:24 +00:00
|
|
|
$content = str_replace('@package_version@', $this->version, $content);
|
|
|
|
|
2011-08-03 21:57:27 +00:00
|
|
|
$phar->addFromString($path, $content);
|
|
|
|
}
|
|
|
|
|
2011-10-02 21:38:54 +00:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getStub()
|
2011-08-03 21:57:27 +00:00
|
|
|
{
|
|
|
|
return <<<'EOF'
|
2012-01-22 20:42:50 +00:00
|
|
|
#!/usr/bin/env php
|
2011-08-03 21:57:27 +00:00
|
|
|
<?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;
|
|
|
|
}
|
|
|
|
}
|