2011-09-14 12:57:40 +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\Console ;
use Symfony\Component\Console\Application as BaseApplication ;
use Symfony\Component\Console\Input\InputInterface ;
2012-08-23 23:32:29 +00:00
use Symfony\Component\Console\Input\InputOption ;
2011-09-14 12:57:40 +00:00
use Symfony\Component\Console\Output\OutputInterface ;
2012-01-11 12:55:05 +00:00
use Symfony\Component\Console\Output\ConsoleOutput ;
2011-12-06 22:07:06 +00:00
use Symfony\Component\Console\Formatter\OutputFormatter ;
2011-10-27 23:19:34 +00:00
use Composer\Command ;
2012-01-08 22:40:30 +00:00
use Composer\Command\Helper\DialogHelper ;
2011-09-20 21:34:06 +00:00
use Composer\Composer ;
2012-01-17 09:00:53 +00:00
use Composer\Factory ;
2012-01-17 22:07:33 +00:00
use Composer\IO\IOInterface ;
2012-01-16 13:14:15 +00:00
use Composer\IO\ConsoleIO ;
2013-02-25 15:55:37 +00:00
use Composer\Json\JsonValidationException ;
2012-01-27 08:29:07 +00:00
use Composer\Util\ErrorHandler ;
2011-09-14 12:57:40 +00:00
/**
* The console application that handles the commands
*
* @ author Ryan Weaver < ryan @ knplabs . com >
2011-11-30 20:30:51 +00:00
* @ author Jordi Boggiano < j . boggiano @ seld . be >
2012-01-10 17:50:16 +00:00
* @ author François Pluchino < francois . pluchino @ opendisplay . com >
2011-09-14 12:57:40 +00:00
*/
class Application extends BaseApplication
{
2012-05-08 21:26:01 +00:00
/**
* @ var Composer
*/
2011-11-30 20:30:51 +00:00
protected $composer ;
2012-05-08 21:26:01 +00:00
/**
2012-06-23 09:47:53 +00:00
* @ var IOInterface
2012-05-08 21:26:01 +00:00
*/
2012-01-16 13:14:15 +00:00
protected $io ;
2011-09-14 12:57:40 +00:00
2013-01-05 17:33:29 +00:00
private static $logo = ' ______
2012-12-28 19:24:21 +00:00
/ ____ / ___ ____ ___ ____ ____ ________ _____
/ / / __ \ / __ ` __ \ / __ \ / __ \ / ___ / _ \ / ___ /
2013-01-05 17:33:29 +00:00
/ / ___ / / _ / / / / / / / / _ / / / _ / ( __ ) __ / /
\____ / \____ / _ / / _ / / _ / . ___ / \____ / ____ / \___ / _ /
/ _ /
' ;
2012-12-28 19:24:21 +00:00
2011-11-30 20:30:51 +00:00
public function __construct ()
2011-09-14 12:57:40 +00:00
{
2012-08-24 14:38:01 +00:00
if ( function_exists ( 'ini_set' )) {
ini_set ( 'xdebug.show_exception_trace' , false );
ini_set ( 'xdebug.scream' , false );
2012-11-20 13:45:30 +00:00
}
if ( function_exists ( 'date_default_timezone_set' ) && function_exists ( 'date_default_timezone_get' )) {
date_default_timezone_set ( @ date_default_timezone_get ());
2012-08-24 14:38:01 +00:00
}
2012-08-24 08:57:38 +00:00
2012-11-20 13:45:30 +00:00
ErrorHandler :: register ();
2013-01-05 17:33:29 +00:00
parent :: __construct ( 'Composer' , Composer :: VERSION );
2011-09-14 12:57:40 +00:00
}
/**
2011-12-06 22:07:06 +00:00
* { @ inheritDoc }
*/
public function run ( InputInterface $input = null , OutputInterface $output = null )
{
if ( null === $output ) {
2012-11-05 12:51:43 +00:00
$styles = Factory :: createAdditionalStyles ();
2011-12-06 22:07:06 +00:00
$formatter = new OutputFormatter ( null , $styles );
$output = new ConsoleOutput ( ConsoleOutput :: VERBOSITY_NORMAL , null , $formatter );
}
return parent :: run ( $input , $output );
}
/**
* { @ inheritDoc }
2011-09-14 12:57:40 +00:00
*/
public function doRun ( InputInterface $input , OutputInterface $output )
{
2012-01-16 13:14:15 +00:00
$this -> io = new ConsoleIO ( $input , $output , $this -> getHelperSet ());
2012-01-10 17:50:16 +00:00
2012-04-29 14:07:47 +00:00
if ( version_compare ( PHP_VERSION , '5.3.2' , '<' )) {
$output -> writeln ( '<warning>Composer only officially supports PHP 5.3.2 and above, you will most likely encounter problems with your PHP ' . PHP_VERSION . ', upgrading is strongly recommended.</warning>' );
}
2013-05-06 16:53:43 +00:00
if ( defined ( 'COMPOSER_DEV_WARNING_TIME' ) && $this -> getCommandName ( $input ) !== 'self-update' && $this -> getCommandName ( $input ) !== 'selfupdate' ) {
2012-06-24 10:18:07 +00:00
if ( time () > COMPOSER_DEV_WARNING_TIME ) {
2012-11-10 20:37:18 +00:00
$output -> writeln ( sprintf ( '<warning>Warning: This development build of composer is over 30 days old. It is recommended to update it by running "%s self-update" to get the latest version.</warning>' , $_SERVER [ 'PHP_SELF' ]));
2012-06-24 10:18:07 +00:00
}
}
2013-04-02 08:34:49 +00:00
if ( getenv ( 'COMPOSER_NO_INTERACTION' )) {
$input -> setInteractive ( false );
}
2012-08-23 23:32:29 +00:00
if ( $input -> hasParameterOption ( '--profile' )) {
$startTime = microtime ( true );
2013-03-05 14:21:54 +00:00
$this -> io -> enableDebugging ( $startTime );
2012-08-23 23:32:29 +00:00
}
2013-03-15 14:21:22 +00:00
if ( $newWorkDir = $this -> getNewWorkingDir ( $input )) {
$oldWorkingDir = getcwd ();
chdir ( $newWorkDir );
}
2012-09-15 10:29:56 +00:00
2012-08-23 23:32:29 +00:00
$result = parent :: doRun ( $input , $output );
2013-03-15 14:21:22 +00:00
if ( isset ( $oldWorkingDir )) {
chdir ( $oldWorkingDir );
}
2012-09-15 10:29:56 +00:00
2012-08-23 23:32:29 +00:00
if ( isset ( $startTime )) {
$output -> writeln ( '<info>Memory usage: ' . round ( memory_get_usage () / 1024 / 1024 , 2 ) . 'MB (peak: ' . round ( memory_get_peak_usage () / 1024 / 1024 , 2 ) . 'MB), time: ' . round ( microtime ( true ) - $startTime , 2 ) . 's' );
}
return $result ;
2011-09-14 12:57:40 +00:00
}
2012-09-15 10:29:56 +00:00
/**
2012-10-18 08:35:06 +00:00
* @ param InputInterface $input
2012-09-15 10:29:56 +00:00
* @ throws \RuntimeException
*/
2013-03-15 14:21:22 +00:00
private function getNewWorkingDir ( InputInterface $input )
2012-09-15 10:29:56 +00:00
{
2013-03-15 14:21:22 +00:00
$workingDir = $input -> getParameterOption ( array ( '--working-dir' , '-d' ));
if ( false !== $workingDir && ! is_dir ( $workingDir )) {
2012-09-16 16:19:46 +00:00
throw new \RuntimeException ( 'Invalid working directory specified.' );
2012-09-15 10:29:56 +00:00
}
2013-03-15 14:21:22 +00:00
return $workingDir ;
2012-09-15 10:29:56 +00:00
}
2013-06-18 12:02:12 +00:00
/**
* { @ inheritDoc }
*/
2013-06-18 12:33:16 +00:00
public function renderException ( $exception , $output )
2013-06-18 12:02:12 +00:00
{
try {
$composer = $this -> getComposer ( false );
if ( $composer ) {
$config = $composer -> getConfig ();
$minSpaceFree = 1024 * 1024 ;
2013-06-26 11:56:30 +00:00
if ((( $df = @ disk_free_space ( $dir = $config -> get ( 'home' ))) !== false && $df < $minSpaceFree )
|| (( $df = @ disk_free_space ( $dir = $config -> get ( 'vendor-dir' ))) !== false && $df < $minSpaceFree )
2013-06-18 12:02:12 +00:00
) {
$output -> writeln ( '<error>The disk hosting ' . $dir . ' is full, this may be the cause of the following exception</error>' );
}
}
} catch ( \Exception $e ) {}
2013-06-18 12:33:16 +00:00
return parent :: renderException ( $exception , $output );
2013-06-18 12:02:12 +00:00
}
2011-09-14 12:57:40 +00:00
/**
2013-06-13 11:28:24 +00:00
* @ param bool $required
2013-08-15 16:46:17 +00:00
* @ param bool $disablePlugins
2013-06-13 00:05:44 +00:00
* @ throws JsonValidationException
2012-06-23 09:58:18 +00:00
* @ return \Composer\Composer
2011-09-14 12:57:40 +00:00
*/
2013-08-15 16:46:17 +00:00
public function getComposer ( $required = true , $disablePlugins = false )
2011-09-14 12:57:40 +00:00
{
2011-11-30 20:30:51 +00:00
if ( null === $this -> composer ) {
2012-01-17 09:00:53 +00:00
try {
2013-08-15 16:46:17 +00:00
$this -> composer = Factory :: create ( $this -> io , null , $disablePlugins );
2012-01-17 09:00:53 +00:00
} catch ( \InvalidArgumentException $e ) {
2012-02-08 09:24:36 +00:00
if ( $required ) {
$this -> io -> write ( $e -> getMessage ());
exit ( 1 );
}
2013-02-25 15:34:31 +00:00
} catch ( JsonValidationException $e ) {
$errors = ' - ' . implode ( PHP_EOL . ' - ' , $e -> getErrors ());
$message = $e -> getMessage () . ':' . PHP_EOL . $errors ;
throw new JsonValidationException ( $message );
2011-11-30 20:30:51 +00:00
}
2013-02-25 15:34:31 +00:00
2011-11-30 20:30:51 +00:00
}
2011-09-14 12:57:40 +00:00
return $this -> composer ;
}
2012-01-16 19:44:06 +00:00
/**
* @ return IOInterface
*/
public function getIO ()
{
return $this -> io ;
}
2013-01-05 17:33:29 +00:00
public function getHelp ()
{
return self :: $logo . parent :: getHelp ();
}
2011-09-20 21:34:06 +00:00
/**
2011-09-25 21:19:12 +00:00
* Initializes all the composer commands
2011-09-14 12:57:40 +00:00
*/
2012-05-27 22:10:02 +00:00
protected function getDefaultCommands ()
2011-09-14 12:57:40 +00:00
{
2012-05-27 22:10:02 +00:00
$commands = parent :: getDefaultCommands ();
$commands [] = new Command\AboutCommand ();
2012-10-02 15:42:48 +00:00
$commands [] = new Command\ConfigCommand ();
2012-05-27 22:10:02 +00:00
$commands [] = new Command\DependsCommand ();
$commands [] = new Command\InitCommand ();
$commands [] = new Command\InstallCommand ();
$commands [] = new Command\CreateProjectCommand ();
$commands [] = new Command\UpdateCommand ();
$commands [] = new Command\SearchCommand ();
$commands [] = new Command\ValidateCommand ();
$commands [] = new Command\ShowCommand ();
$commands [] = new Command\RequireCommand ();
2012-06-25 12:20:50 +00:00
$commands [] = new Command\DumpAutoloadCommand ();
2012-06-25 23:18:10 +00:00
$commands [] = new Command\StatusCommand ();
2013-02-07 10:37:27 +00:00
$commands [] = new Command\ArchiveCommand ();
2013-04-04 13:44:18 +00:00
$commands [] = new Command\DiagnoseCommand ();
2013-04-15 13:20:00 +00:00
$commands [] = new Command\RunScriptCommand ();
2013-03-26 11:14:41 +00:00
$commands [] = new Command\LicensesCommand ();
2013-08-13 12:15:54 +00:00
$commands [] = new Command\GlobalCommand ();
2011-11-16 12:49:00 +00:00
if ( 'phar:' === substr ( __FILE__ , 0 , 5 )) {
2012-05-27 22:10:02 +00:00
$commands [] = new Command\SelfUpdateCommand ();
2011-11-16 12:49:00 +00:00
}
2012-05-27 22:10:02 +00:00
return $commands ;
2011-09-14 12:57:40 +00:00
}
2012-01-08 22:40:30 +00:00
2012-08-23 23:32:29 +00:00
/**
* { @ inheritDoc }
*/
protected function getDefaultInputDefinition ()
{
$definition = parent :: getDefaultInputDefinition ();
$definition -> addOption ( new InputOption ( '--profile' , null , InputOption :: VALUE_NONE , 'Display timing and memory usage information' ));
2012-09-15 09:36:57 +00:00
$definition -> addOption ( new InputOption ( '--working-dir' , '-d' , InputOption :: VALUE_REQUIRED , 'If specified, use the given directory as working directory.' ));
2012-08-23 23:32:29 +00:00
return $definition ;
}
2012-01-08 22:40:30 +00:00
/**
* { @ inheritDoc }
*/
protected function getDefaultHelperSet ()
{
$helperSet = parent :: getDefaultHelperSet ();
$helperSet -> set ( new DialogHelper ());
return $helperSet ;
}
2011-09-20 21:34:06 +00:00
}