1
0
Fork 0
composer/src/Composer/Compiler.php

140 lines
3.7 KiB
PHP
Raw Normal View History

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;
use Symfony\Component\Process\Process;
2011-08-03 21:57:27 +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);
}
$process = new Process('git log --pretty="%h" -n1 HEAD');
if ($process->run() != 0) {
throw new \RuntimeException('The git binary cannot be found.');
}
$this->version = trim($process->getOutput());
$process = new Process('git describe --tags HEAD');
if ($process->run() == 0) {
$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);
}
$finder = new Finder();
$finder->files()
->ignoreVCS(true)
->name('*.php')
->in(__DIR__.'/../../vendor/symfony/')
;
foreach ($finder as $file) {
$this->addFile($phar, $file);
}
2012-01-23 09:02:28 +00:00
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../vendor/.composer/ClassLoader.php'));
$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();
2012-02-15 17:21:17 +00:00
// disabled for interoperability with systems without gzip ext
// $phar->compressFiles(\Phar::GZ);
2011-08-03 21:57:27 +00:00
$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";
}
$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'
#!/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;
}
}