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 ;
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
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 ();
2011-09-14 12:57:40 +00:00
parent :: __construct ( 'Composer' , Composer :: VERSION );
}
/**
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>' );
}
2012-06-24 10:18:07 +00:00
if ( defined ( 'COMPOSER_DEV_WARNING_TIME' ) && $this -> getCommandName ( $input ) !== 'self-update' ) {
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
}
}
2012-08-23 23:32:29 +00:00
if ( $input -> hasParameterOption ( '--profile' )) {
$startTime = microtime ( true );
}
2012-09-15 10:29:56 +00:00
$oldWorkingDir = getcwd ();
$this -> switchWorkingDir ( $input );
2012-08-23 23:32:29 +00:00
$result = parent :: doRun ( $input , $output );
2012-09-15 10:29:56 +00:00
chdir ( $oldWorkingDir );
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
*/
private function switchWorkingDir ( InputInterface $input )
{
$workingDir = $input -> getParameterOption ( array ( '--working-dir' , '-d' ), getcwd ());
if ( ! 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
}
chdir ( $workingDir );
}
2011-09-14 12:57:40 +00:00
/**
2012-06-23 09:58:18 +00:00
* @ param bool $required
* @ return \Composer\Composer
2011-09-14 12:57:40 +00:00
*/
2012-02-08 09:24:36 +00:00
public function getComposer ( $required = true )
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 {
2012-01-17 16:56:06 +00:00
$this -> composer = Factory :: create ( $this -> io );
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 );
}
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 ;
}
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 ();
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
}