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

112 lines
2.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;
/**
* 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);
}
$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);
}
$this->addFile($phar, new \SplFileInfo(__DIR__.'/../../tests/bootstrap.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";
}
$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'
2011-10-02 21:38:54 +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;
}
}