2022-02-23 15:58:18 +00:00
< ? php declare ( strict_types = 1 );
2011-09-14 12:57:40 +00:00
/*
* 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 ;
2018-03-24 07:32:04 +00:00
use Composer\IO\NullIO ;
2021-05-04 11:25:52 +00:00
use Composer\Util\Filesystem ;
2016-02-03 21:39:16 +00:00
use Composer\Util\Platform ;
2016-01-25 22:37:54 +00:00
use Composer\Util\Silencer ;
2011-09-14 12:57:40 +00:00
use Symfony\Component\Console\Application as BaseApplication ;
2018-09-16 20:12:14 +00:00
use Symfony\Component\Console\Exception\CommandNotFoundException ;
2019-01-02 22:36:53 +00:00
use Symfony\Component\Console\Helper\HelperSet ;
use Symfony\Component\Console\Helper\QuestionHelper ;
2021-12-29 12:30:50 +00:00
use Symfony\Component\Console\Input\InputDefinition ;
2011-09-14 12:57:40 +00:00
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 ;
2020-08-13 14:37:32 +00:00
use Seld\JsonLint\ParsingException ;
2011-10-27 23:19:34 +00:00
use Composer\Command ;
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 ;
2020-10-30 08:56:58 +00:00
use Composer\Util\HttpDownloader ;
2016-05-05 12:26:26 +00:00
use Composer\EventDispatcher\ScriptExecutionException ;
2016-05-31 18:19:14 +00:00
use Composer\Exception\NoSslException ;
2021-04-13 07:59:06 +00:00
use Composer\XdebugHandler\XdebugHandler ;
2021-04-11 11:30:35 +00:00
use Symfony\Component\Process\Exception\ProcessTimedOutException ;
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
/**
2021-03-09 14:49:40 +00:00
* @ var ? Composer
2012-05-08 21:26:01 +00:00
*/
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
2021-08-30 12:14:19 +00:00
/** @var string */
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
2021-08-30 12:14:19 +00:00
/** @var bool */
2016-05-03 06:26:14 +00:00
private $hasPluginCommands = false ;
2021-08-30 12:14:19 +00:00
/** @var bool */
2016-07-12 15:13:02 +00:00
private $disablePluginsByDefault = false ;
2021-12-20 13:23:35 +00:00
/** @var bool */
private $disableScriptsByDefault = false ;
2016-05-03 06:26:14 +00:00
2020-01-07 03:29:00 +00:00
/**
2022-02-22 15:47:09 +00:00
* @ var string | false Store the initial working directory at startup time
2020-01-07 03:29:00 +00:00
*/
2020-09-11 19:58:48 +00:00
private $initialWorkingDirectory ;
2020-01-07 03:29:00 +00:00
2011-11-30 20:30:51 +00:00
public function __construct ()
2011-09-14 12:57:40 +00:00
{
2015-11-17 13:31:07 +00:00
static $shutdownRegistered = false ;
2015-11-19 17:36:52 +00:00
2014-04-30 09:29:52 +00:00
if ( function_exists ( 'ini_set' ) && extension_loaded ( 'xdebug' )) {
2021-08-21 15:41:52 +00:00
ini_set ( 'xdebug.show_exception_trace' , '0' );
ini_set ( 'xdebug.scream' , '0' );
2012-11-20 13:45:30 +00:00
}
2014-04-30 09:29:52 +00:00
2012-11-20 13:45:30 +00:00
if ( function_exists ( 'date_default_timezone_set' ) && function_exists ( 'date_default_timezone_get' )) {
2016-01-25 22:37:54 +00:00
date_default_timezone_set ( Silencer :: call ( 'date_default_timezone_get' ));
2012-08-24 14:38:01 +00:00
}
2015-11-19 17:36:52 +00:00
2015-11-17 13:31:07 +00:00
if ( ! $shutdownRegistered ) {
2020-07-01 09:37:38 +00:00
if ( function_exists ( 'pcntl_async_signals' ) && function_exists ( 'pcntl_signal' )) {
pcntl_async_signals ( true );
2022-02-21 12:37:49 +00:00
pcntl_signal ( SIGINT , function ( $sig ) : void {
2020-07-01 09:37:38 +00:00
exit ( 130 );
});
}
2015-11-17 13:31:07 +00:00
$shutdownRegistered = true ;
2015-11-19 17:36:52 +00:00
2022-02-21 12:37:49 +00:00
register_shutdown_function ( function () : void {
2015-11-17 13:31:07 +00:00
$lastError = error_get_last ();
2015-11-19 17:36:52 +00:00
2015-11-17 13:31:07 +00:00
if ( $lastError && $lastError [ 'message' ] &&
( strpos ( $lastError [ 'message' ], 'Allowed memory' ) !== false /*Zend PHP out of memory error*/ ||
strpos ( $lastError [ 'message' ], 'exceeded memory' ) !== false /*HHVM out of memory errors*/ )) {
echo " \n " . 'Check https://getcomposer.org/doc/articles/troubleshooting.md#memory-limit-errors for more info on how to handle out of memory errors.' ;
}
});
}
2012-08-24 08:57:38 +00:00
2018-03-24 07:32:04 +00:00
$this -> io = new NullIO ();
2020-01-13 14:50:34 +00:00
$this -> initialWorkingDirectory = getcwd ();
2020-01-07 03:29:00 +00:00
2019-02-21 12:39:12 +00:00
parent :: __construct ( 'Composer' , Composer :: getVersion ());
2011-09-14 12:57:40 +00:00
}
2021-12-08 21:06:17 +00:00
public function run ( InputInterface $input = null , OutputInterface $output = null ) : int
2011-12-06 22:07:06 +00:00
{
if ( null === $output ) {
2016-09-06 13:48:37 +00:00
$output = Factory :: createOutput ();
2011-12-06 22:07:06 +00:00
}
return parent :: run ( $input , $output );
}
2021-12-08 21:06:17 +00:00
public function doRun ( InputInterface $input , OutputInterface $output ) : int
2011-09-14 12:57:40 +00:00
{
2016-07-12 15:13:02 +00:00
$this -> disablePluginsByDefault = $input -> hasParameterOption ( '--no-plugins' );
2021-12-20 13:23:35 +00:00
$this -> disableScriptsByDefault = $input -> hasParameterOption ( '--no-scripts' );
2016-07-12 15:13:02 +00:00
2021-11-09 15:31:27 +00:00
if ( Platform :: getEnv ( 'COMPOSER_NO_INTERACTION' ) || ! Platform :: isTty ( defined ( 'STDIN' ) ? STDIN : fopen ( 'php://stdin' , 'r' ))) {
2019-08-25 15:00:25 +00:00
$input -> setInteractive ( false );
}
2019-01-02 22:36:53 +00:00
$io = $this -> io = new ConsoleIO ( $input , $output , new HelperSet ( array (
new QuestionHelper (),
)));
2021-07-22 10:57:55 +00:00
// Register error handler again to pass it the IO instance
2016-01-16 11:24:04 +00:00
ErrorHandler :: register ( $io );
2012-01-10 17:50:16 +00:00
2019-01-28 14:29:37 +00:00
if ( $input -> hasParameterOption ( '--no-cache' )) {
$io -> writeError ( 'Disabling cache usage' , true , IOInterface :: DEBUG );
2021-06-03 08:17:54 +00:00
Platform :: putEnv ( 'COMPOSER_CACHE_DIR' , Platform :: isWindows () ? 'nul' : '/dev/null' );
2019-01-28 14:29:37 +00:00
}
2016-05-05 22:20:54 +00:00
// switch working dir
2022-02-23 15:57:47 +00:00
$newWorkDir = $this -> getNewWorkingDir ( $input );
if ( null !== $newWorkDir ) {
2022-02-22 15:47:09 +00:00
$oldWorkingDir = Platform :: getCwd ( true );
2016-05-05 22:20:54 +00:00
chdir ( $newWorkDir );
2020-01-13 14:50:34 +00:00
$this -> initialWorkingDirectory = $newWorkDir ;
2022-02-22 15:47:09 +00:00
$cwd = Platform :: getCwd ( true );
$io -> writeError ( 'Changed CWD to ' . ( $cwd !== '' ? $cwd : $newWorkDir ), true , IOInterface :: DEBUG );
2016-05-05 22:20:54 +00:00
}
2016-05-05 13:06:04 +00:00
// determine command name to be executed without including plugin commands
2016-01-10 12:43:48 +00:00
$commandName = '' ;
if ( $name = $this -> getCommandName ( $input )) {
try {
$commandName = $this -> find ( $name ) -> getName ();
2018-09-16 20:12:14 +00:00
} catch ( CommandNotFoundException $e ) {
2020-11-22 13:48:56 +00:00
// we'll check command validity again later after plugins are loaded
$commandName = false ;
2016-01-10 12:43:48 +00:00
} catch ( \InvalidArgumentException $e ) {
}
2012-04-29 14:07:47 +00:00
}
2015-11-19 17:36:52 +00:00
2017-08-07 08:36:02 +00:00
// prompt user for dir change if no composer.json is present in current dir
2022-02-23 15:57:47 +00:00
if ( $io -> isInteractive () && null === $newWorkDir && ! in_array ( $commandName , array ( '' , 'list' , 'init' , 'about' , 'help' , 'diagnose' , 'self-update' , 'global' , 'create-project' , 'outdated' ), true ) && ! file_exists ( Factory :: getComposerFile ()) && ( $useParentDirIfNoJsonAvailable = $this -> getUseParentDirConfigValue ()) !== false ) {
2022-02-22 15:47:09 +00:00
$dir = dirname ( Platform :: getCwd ( true ));
2021-11-09 15:31:27 +00:00
$home = realpath ( Platform :: getEnv ( 'HOME' ) ? : Platform :: getEnv ( 'USERPROFILE' ) ? : '/' );
2017-08-07 08:36:02 +00:00
// abort when we reach the home dir or top of the filesystem
while ( dirname ( $dir ) !== $dir && $dir !== $home ) {
2017-08-08 16:08:46 +00:00
if ( file_exists ( $dir . '/' . Factory :: getComposerFile ())) {
2021-12-04 14:00:28 +00:00
if ( $useParentDirIfNoJsonAvailable === true || $io -> askConfirmation ( '<info>No composer.json in current directory, do you want to use the one at ' . $dir . '?</info> [<comment>Y,n</comment>]? ' )) {
if ( $useParentDirIfNoJsonAvailable === true ) {
$io -> writeError ( '<info>No composer.json in current directory, changing working directory to ' . $dir . '</info>' );
} else {
$io -> writeError ( '<info>Always want to use the parent dir? Use "composer config --global use-parent-dir true" to change the default.</info>' );
}
2022-02-22 15:47:09 +00:00
$oldWorkingDir = Platform :: getCwd ( true );
2017-08-07 08:36:02 +00:00
chdir ( $dir );
}
break ;
}
$dir = dirname ( $dir );
}
}
2021-08-19 12:37:43 +00:00
// avoid loading plugins/initializing the Composer instance earlier than necessary if no plugin command is needed
2021-08-29 11:03:45 +00:00
// if showing the version, we never need plugin commands
$mayNeedPluginCommand = false === $input -> hasParameterOption ( array ( '--version' , '-V' ))
&& (
// not a composer command, so try loading plugin ones
false === $commandName
// list command requires plugin commands to show them
2021-10-13 20:03:31 +00:00
|| in_array ( $commandName , array ( '' , 'list' , 'help' ), true )
2021-08-29 11:03:45 +00:00
);
2021-10-13 20:03:31 +00:00
if ( $mayNeedPluginCommand && ! $this -> disablePluginsByDefault && ! $this -> hasPluginCommands ) {
2016-05-31 18:19:14 +00:00
try {
foreach ( $this -> getPluginCommands () as $command ) {
if ( $this -> has ( $command -> getName ())) {
$io -> writeError ( '<warning>Plugin command ' . $command -> getName () . ' (' . get_class ( $command ) . ') would override a Composer command and has been skipped</warning>' );
} else {
$this -> add ( $command );
}
2016-05-03 06:26:14 +00:00
}
2016-05-31 18:19:14 +00:00
} catch ( NoSslException $e ) {
// suppress these as they are not relevant at this point
2020-08-13 14:37:32 +00:00
} catch ( ParsingException $e ) {
$details = $e -> getDetails ();
$file = realpath ( Factory :: getComposerFile ());
$line = null ;
if ( $details && isset ( $details [ 'line' ])) {
$line = $details [ 'line' ];
}
$ghe = new GithubActionError ( $this -> io );
$ghe -> emit ( $e -> getMessage (), $file , $line );
throw $e ;
2016-05-03 06:26:14 +00:00
}
2016-05-31 18:19:14 +00:00
2016-05-03 06:26:14 +00:00
$this -> hasPluginCommands = true ;
}
2016-05-05 13:06:04 +00:00
// determine command name to be executed incl plugin commands, and check if it's a proxy command
2016-05-03 06:26:14 +00:00
$isProxyCommand = false ;
2016-01-10 12:43:48 +00:00
if ( $name = $this -> getCommandName ( $input )) {
try {
2016-05-03 06:26:14 +00:00
$command = $this -> find ( $name );
$commandName = $command -> getName ();
$isProxyCommand = ( $command instanceof Command\BaseCommand && $command -> isProxyCommand ());
2016-01-10 12:43:48 +00:00
} catch ( \InvalidArgumentException $e ) {
}
2012-04-29 14:07:47 +00:00
}
2016-04-29 14:31:18 +00:00
if ( ! $isProxyCommand ) {
2016-04-18 21:09:07 +00:00
$io -> writeError ( sprintf (
'Running %s (%s) with %s on %s' ,
2019-02-21 12:39:12 +00:00
Composer :: getVersion (),
2016-04-18 21:09:07 +00:00
Composer :: RELEASE_DATE ,
defined ( 'HHVM_VERSION' ) ? 'HHVM ' . HHVM_VERSION : 'PHP ' . PHP_VERSION ,
2017-05-21 14:24:35 +00:00
function_exists ( 'php_uname' ) ? php_uname ( 's' ) . ' / ' . php_uname ( 'r' ) : 'Unknown OS'
2016-04-18 21:09:07 +00:00
), true , IOInterface :: DEBUG );
2022-01-03 16:35:32 +00:00
if ( PHP_VERSION_ID < 70205 ) {
$io -> writeError ( '<warning>Composer supports PHP 7.2.5 and above, you will most likely encounter problems with your PHP ' . PHP_VERSION . '. Upgrading is strongly recommended but you can use Composer 2.2.x LTS as a fallback.</warning>' );
2016-01-10 12:43:48 +00:00
}
2012-04-29 14:07:47 +00:00
2021-11-09 15:31:27 +00:00
if ( XdebugHandler :: isXdebugActive () && ! Platform :: getEnv ( 'COMPOSER_DISABLE_XDEBUG_WARN' )) {
2021-01-12 13:55:55 +00:00
$io -> writeError ( '<warning>Composer is operating slower than normal because you have Xdebug enabled. See https://getcomposer.org/xdebug</warning>' );
2014-09-08 10:44:36 +00:00
}
2016-01-10 12:43:48 +00:00
if ( defined ( 'COMPOSER_DEV_WARNING_TIME' ) && $commandName !== 'self-update' && $commandName !== 'selfupdate' && time () > COMPOSER_DEV_WARNING_TIME ) {
2021-01-12 13:55:55 +00:00
$io -> writeError ( sprintf ( '<warning>Warning: This development build of Composer is over 60 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
}
2020-02-07 21:33:39 +00:00
if (
! Platform :: isWindows ()
&& function_exists ( 'exec' )
2021-11-09 15:31:27 +00:00
&& ! Platform :: getEnv ( 'COMPOSER_ALLOW_SUPERUSER' )
2020-02-07 21:33:39 +00:00
&& ( ini_get ( 'open_basedir' ) || ! file_exists ( '/.dockerenv' ))
) {
2016-03-28 11:10:43 +00:00
if ( function_exists ( 'posix_getuid' ) && posix_getuid () === 0 ) {
2016-04-01 11:31:07 +00:00
if ( $commandName !== 'self-update' && $commandName !== 'selfupdate' ) {
2016-05-31 20:00:15 +00:00
$io -> writeError ( '<warning>Do not run Composer as root/super user! See https://getcomposer.org/root for details</warning>' );
2020-08-13 14:37:32 +00:00
2018-12-04 15:03:16 +00:00
if ( $io -> isInteractive ()) {
2020-09-11 20:04:37 +00:00
if ( ! $io -> askConfirmation ( '<info>Continue as root/super user</info> [<comment>yes</comment>]? ' )) {
2018-12-04 17:54:57 +00:00
return 1 ;
2018-12-04 15:03:16 +00:00
}
}
2016-04-01 11:31:07 +00:00
}
2021-11-09 15:31:27 +00:00
if ( $uid = ( int ) Platform :: getEnv ( 'SUDO_UID' )) {
2016-03-28 11:10:43 +00:00
// Silently clobber any sudo credentials on the invoking user to avoid privilege escalations later on
// ref. https://github.com/composer/composer/issues/5119
2016-04-01 00:24:31 +00:00
Silencer :: call ( 'exec' , " sudo -u \\ # { $uid } sudo -K > /dev/null 2>&1 " );
2016-03-28 11:10:43 +00:00
}
2016-03-27 21:42:39 +00:00
}
2016-03-28 11:10:43 +00:00
// Silently clobber any remaining sudo leases on the current user as well to avoid privilege escalations
2016-04-01 00:24:31 +00:00
Silencer :: call ( 'exec' , 'sudo -K > /dev/null 2>&1' );
2016-03-27 21:42:39 +00:00
}
2016-04-16 19:01:10 +00:00
// Check system temp folder for usability as it can cause weird runtime issues otherwise
2022-02-21 12:37:49 +00:00
Silencer :: call ( function () use ( $io ) : void {
2016-04-18 23:39:32 +00:00
$tempfile = sys_get_temp_dir () . '/temp-' . md5 ( microtime ());
if ( ! ( file_put_contents ( $tempfile , __FILE__ ) && ( file_get_contents ( $tempfile ) == __FILE__ ) && unlink ( $tempfile ) && ! file_exists ( $tempfile ))) {
2016-04-19 10:00:39 +00:00
$io -> writeError ( sprintf ( '<error>PHP temp directory (%s) does not exist or is not writable to Composer. Set sys_temp_dir in your php.ini</error>' , sys_get_temp_dir ()));
2016-04-16 19:01:10 +00:00
}
});
2016-01-16 11:24:04 +00:00
// add non-standard scripts as own commands
$file = Factory :: getComposerFile ();
2021-05-04 11:25:52 +00:00
if ( is_file ( $file ) && Filesystem :: isReadable ( $file ) && is_array ( $composer = json_decode ( file_get_contents ( $file ), true ))) {
2016-01-16 11:24:04 +00:00
if ( isset ( $composer [ 'scripts' ]) && is_array ( $composer [ 'scripts' ])) {
foreach ( $composer [ 'scripts' ] as $script => $dummy ) {
if ( ! defined ( 'Composer\Script\ScriptEvents::' . str_replace ( '-' , '_' , strtoupper ( $script )))) {
if ( $this -> has ( $script )) {
2016-04-28 20:12:26 +00:00
$io -> writeError ( '<warning>A script named ' . $script . ' would override a Composer command and has been skipped</warning>' );
2016-01-16 11:24:04 +00:00
} else {
2017-12-18 09:14:24 +00:00
$description = null ;
2017-12-18 09:46:07 +00:00
if ( isset ( $composer [ 'scripts-descriptions' ][ $script ])) {
$description = $composer [ 'scripts-descriptions' ][ $script ];
2017-12-18 09:14:24 +00:00
}
2017-09-12 03:17:08 +00:00
$this -> add ( new Command\ScriptAliasCommand ( $script , $description ));
2016-01-16 11:24:04 +00:00
}
2014-09-05 16:28:50 +00:00
}
2014-07-28 10:59:19 +00:00
}
2014-07-20 19:55:24 +00:00
}
}
}
2016-01-16 11:24:04 +00:00
try {
if ( $input -> hasParameterOption ( '--profile' )) {
$startTime = microtime ( true );
$this -> io -> enableDebugging ( $startTime );
}
2012-08-23 23:32:29 +00:00
2016-01-16 11:24:04 +00:00
$result = parent :: doRun ( $input , $output );
2012-08-23 23:32:29 +00:00
2021-01-26 20:48:19 +00:00
// chdir back to $oldWorkingDir if set
2022-02-22 15:47:09 +00:00
if ( isset ( $oldWorkingDir ) && '' !== $oldWorkingDir ) {
2021-01-27 08:17:25 +00:00
Silencer :: call ( 'chdir' , $oldWorkingDir );
2016-01-16 11:24:04 +00:00
}
2012-09-15 10:29:56 +00:00
2016-01-16 11:24:04 +00:00
if ( isset ( $startTime )) {
2018-12-21 20:57:19 +00:00
$io -> writeError ( '<info>Memory usage: ' . round ( memory_get_usage () / 1024 / 1024 , 2 ) . 'MiB (peak: ' . round ( memory_get_peak_usage () / 1024 / 1024 , 2 ) . 'MiB), time: ' . round ( microtime ( true ) - $startTime , 2 ) . 's' );
2016-01-16 11:24:04 +00:00
}
2012-08-23 23:32:29 +00:00
2016-01-16 11:24:04 +00:00
return $result ;
2016-05-05 12:26:26 +00:00
} catch ( ScriptExecutionException $e ) {
2022-01-07 08:26:22 +00:00
return $e -> getCode ();
2016-01-16 11:24:04 +00:00
} catch ( \Exception $e ) {
2020-08-13 14:37:32 +00:00
$ghe = new GithubActionError ( $this -> io );
$ghe -> emit ( $e -> getMessage ());
2016-01-16 11:24:04 +00:00
$this -> hintCommonErrors ( $e );
2020-08-13 14:37:32 +00:00
2016-01-16 11:24:04 +00:00
throw $e ;
2022-01-04 15:19:58 +00:00
} finally {
restore_error_handler ();
2016-01-16 11:24:04 +00:00
}
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
2022-02-23 15:57:47 +00:00
* @ return ? string
2012-09-15 10:29:56 +00:00
*/
2022-02-23 15:57:47 +00:00
private function getNewWorkingDir ( InputInterface $input ) : ? string
2012-09-15 10:29:56 +00:00
{
2022-02-23 15:57:47 +00:00
/** @var string|null $workingDir */
$workingDir = $input -> getParameterOption ( array ( '--working-dir' , '-d' ), null , true );
if ( null !== $workingDir && ! is_dir ( $workingDir )) {
2015-11-03 20:37:11 +00:00
throw new \RuntimeException ( 'Invalid working directory specified, ' . $workingDir . ' does not exist.' );
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
/**
2021-10-18 20:40:50 +00:00
* @ return void
2013-06-18 12:02:12 +00:00
*/
2022-02-18 10:22:01 +00:00
private function hintCommonErrors ( \Exception $exception ) : void
2013-06-18 12:02:12 +00:00
{
2015-09-13 21:30:09 +00:00
$io = $this -> getIO ();
2016-01-25 22:37:54 +00:00
Silencer :: suppress ();
2013-06-18 12:02:12 +00:00
try {
2014-12-13 18:53:29 +00:00
$composer = $this -> getComposer ( false , true );
2013-06-18 12:02:12 +00:00
if ( $composer ) {
$config = $composer -> getConfig ();
2015-06-30 14:38:32 +00:00
$minSpaceFree = 1024 * 1024 ;
2016-01-25 22:37:54 +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 )
|| (( $df = disk_free_space ( $dir = sys_get_temp_dir ())) !== false && $df < $minSpaceFree )
2013-06-18 12:02:12 +00:00
) {
2016-01-28 13:41:19 +00:00
$io -> writeError ( '<error>The disk hosting ' . $dir . ' is full, this may be the cause of the following exception</error>' , true , IOInterface :: QUIET );
2013-06-18 12:02:12 +00:00
}
}
2014-10-17 17:46:01 +00:00
} catch ( \Exception $e ) {
}
2016-01-25 22:37:54 +00:00
Silencer :: restore ();
2013-06-18 12:02:12 +00:00
2016-02-03 21:39:16 +00:00
if ( Platform :: isWindows () && false !== strpos ( $exception -> getMessage (), 'The system cannot find the path specified' )) {
2016-01-28 13:41:19 +00:00
$io -> writeError ( '<error>The following exception may be caused by a stale entry in your cmd.exe AutoRun</error>' , true , IOInterface :: QUIET );
$io -> writeError ( '<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#-the-system-cannot-find-the-path-specified-windows- for details</error>' , true , IOInterface :: QUIET );
2014-12-13 11:47:22 +00:00
}
if ( false !== strpos ( $exception -> getMessage (), 'fork failed - Cannot allocate memory' )) {
2016-08-10 00:31:19 +00:00
$io -> writeError ( '<error>The following exception is caused by a lack of memory or swap, or not having swap configured</error>' , true , IOInterface :: QUIET );
2016-01-28 13:41:19 +00:00
$io -> writeError ( '<error>Check https://getcomposer.org/doc/articles/troubleshooting.md#proc-open-fork-failed-errors for details</error>' , true , IOInterface :: QUIET );
2014-12-13 11:47:22 +00:00
}
2020-10-30 08:56:58 +00:00
2021-04-11 11:30:35 +00:00
if ( $exception instanceof ProcessTimedOutException ) {
$io -> writeError ( '<error>The following exception is caused by a process timeout</error>' , true , IOInterface :: QUIET );
$io -> writeError ( '<error>Check https://getcomposer.org/doc/06-config.md#process-timeout for details</error>' , true , IOInterface :: QUIET );
}
2022-02-18 10:22:01 +00:00
$hints = HttpDownloader :: getExceptionHints ( $exception );
if ( null !== $hints && count ( $hints ) > 0 ) {
2020-10-30 08:56:58 +00:00
foreach ( $hints as $hint ) {
$io -> writeError ( $hint , true , IOInterface :: QUIET );
}
}
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
2016-07-12 15:13:02 +00:00
* @ param bool | null $disablePlugins
2021-12-20 13:23:35 +00:00
* @ param bool | null $disableScripts
2013-06-13 00:05:44 +00:00
* @ throws JsonValidationException
2021-08-21 15:41:52 +00:00
* @ throws \InvalidArgumentException
2022-02-22 21:10:52 +00:00
* @ return ? Composer If $required is true then the return value is guaranteed
2011-09-14 12:57:40 +00:00
*/
2022-02-22 21:10:52 +00:00
public function getComposer ( bool $required = true , ? bool $disablePlugins = null , ? bool $disableScripts = null ) : ? Composer
2011-09-14 12:57:40 +00:00
{
2016-07-12 15:13:02 +00:00
if ( null === $disablePlugins ) {
$disablePlugins = $this -> disablePluginsByDefault ;
}
2021-12-20 13:23:35 +00:00
if ( null === $disableScripts ) {
$disableScripts = $this -> disableScriptsByDefault ;
}
2016-07-12 15:13:02 +00:00
2011-11-30 20:30:51 +00:00
if ( null === $this -> composer ) {
2012-01-17 09:00:53 +00:00
try {
2021-12-20 13:23:35 +00:00
$this -> composer = Factory :: create ( $this -> io , null , $disablePlugins , $disableScripts );
2012-01-17 09:00:53 +00:00
} catch ( \InvalidArgumentException $e ) {
2012-02-08 09:24:36 +00:00
if ( $required ) {
2015-02-06 12:52:44 +00:00
$this -> io -> writeError ( $e -> getMessage ());
2022-01-03 13:51:41 +00:00
if ( $this -> areExceptionsCaught ()) {
2021-07-22 13:14:15 +00:00
exit ( 1 );
}
throw $e ;
2012-02-08 09:24:36 +00:00
}
2013-02-25 15:34:31 +00:00
} catch ( JsonValidationException $e ) {
2021-06-27 12:31:50 +00:00
if ( $required ) {
throw $e ;
}
2011-11-30 20:30:51 +00:00
}
}
2011-09-14 12:57:40 +00:00
return $this -> composer ;
}
2014-12-05 11:58:21 +00:00
/**
* Removes the cached composer instance
2021-10-18 20:40:50 +00:00
*
* @ return void
2014-12-05 11:58:21 +00:00
*/
2022-02-22 21:10:52 +00:00
public function resetComposer () : void
2014-12-05 11:58:21 +00:00
{
$this -> composer = null ;
2021-08-21 15:41:52 +00:00
if ( method_exists ( $this -> getIO (), 'resetAuthentications' )) {
2019-07-29 15:57:18 +00:00
$this -> getIO () -> resetAuthentications ();
}
2014-12-05 11:58:21 +00:00
}
2012-01-16 19:44:06 +00:00
/**
* @ return IOInterface
*/
2022-02-22 21:10:52 +00:00
public function getIO () : IOInterface
2012-01-16 19:44:06 +00:00
{
return $this -> io ;
}
2021-12-08 21:06:17 +00:00
public function getHelp () : string
2013-01-05 17:33:29 +00:00
{
return self :: $logo . parent :: getHelp ();
}
2011-09-20 21:34:06 +00:00
/**
2016-02-20 00:39:24 +00:00
* Initializes all the composer commands .
2021-12-29 12:30:50 +00:00
* @ return \Symfony\Component\Console\Command\Command []
2011-09-14 12:57:40 +00:00
*/
2021-12-08 21:06:17 +00:00
protected function getDefaultCommands () : array
2011-09-14 12:57:40 +00:00
{
2016-02-20 00:39:24 +00:00
$commands = array_merge ( parent :: getDefaultCommands (), array (
new Command\AboutCommand (),
new Command\ConfigCommand (),
new Command\DependsCommand (),
new Command\ProhibitsCommand (),
new Command\InitCommand (),
new Command\InstallCommand (),
new Command\CreateProjectCommand (),
new Command\UpdateCommand (),
new Command\SearchCommand (),
new Command\ValidateCommand (),
new Command\ShowCommand (),
new Command\SuggestsCommand (),
new Command\RequireCommand (),
new Command\DumpAutoloadCommand (),
new Command\StatusCommand (),
new Command\ArchiveCommand (),
new Command\DiagnoseCommand (),
new Command\RunScriptCommand (),
new Command\LicensesCommand (),
new Command\GlobalCommand (),
new Command\ClearCacheCommand (),
new Command\RemoveCommand (),
new Command\HomeCommand (),
2016-02-25 12:01:33 +00:00
new Command\ExecCommand (),
2016-04-22 20:54:04 +00:00
new Command\OutdatedCommand (),
2017-09-27 09:20:51 +00:00
new Command\CheckPlatformReqsCommand (),
2020-02-14 13:08:26 +00:00
new Command\FundCommand (),
2021-05-25 20:30:15 +00:00
new Command\ReinstallCommand (),
2016-02-20 00:39:24 +00:00
));
2011-11-16 12:49:00 +00:00
2020-09-07 18:06:55 +00:00
if ( strpos ( __FILE__ , 'phar:' ) === 0 ) {
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
2021-12-08 21:06:17 +00:00
public function getLongVersion () : string
2013-09-16 12:08:24 +00:00
{
2019-02-21 12:39:12 +00:00
if ( Composer :: BRANCH_ALIAS_VERSION && Composer :: BRANCH_ALIAS_VERSION !== '@package_branch_alias_version' . '@' ) {
2014-10-17 01:13:17 +00:00
return sprintf (
'<info>%s</info> version <comment>%s (%s)</comment> %s' ,
$this -> getName (),
Composer :: BRANCH_ALIAS_VERSION ,
$this -> getVersion (),
Composer :: RELEASE_DATE
);
}
2013-09-16 12:08:24 +00:00
return parent :: getLongVersion () . ' ' . Composer :: RELEASE_DATE ;
}
2021-12-08 21:06:17 +00:00
protected function getDefaultInputDefinition () : InputDefinition
2012-08-23 23:32:29 +00:00
{
$definition = parent :: getDefaultInputDefinition ();
$definition -> addOption ( new InputOption ( '--profile' , null , InputOption :: VALUE_NONE , 'Display timing and memory usage information' ));
2014-10-26 17:47:29 +00:00
$definition -> addOption ( new InputOption ( '--no-plugins' , null , InputOption :: VALUE_NONE , 'Whether to disable plugins.' ));
2021-12-20 13:23:35 +00:00
$definition -> addOption ( new InputOption ( '--no-scripts' , null , InputOption :: VALUE_NONE , 'Skips the execution of all scripts defined in composer.json file.' ));
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.' ));
2019-01-28 14:29:37 +00:00
$definition -> addOption ( new InputOption ( '--no-cache' , null , InputOption :: VALUE_NONE , 'Prevent use of the cache' ));
2012-08-23 23:32:29 +00:00
return $definition ;
}
2021-10-17 08:18:22 +00:00
/**
* @ return Command\BaseCommand []
*/
2022-02-18 10:22:01 +00:00
private function getPluginCommands () : array
2014-10-26 17:47:29 +00:00
{
$commands = array ();
$composer = $this -> getComposer ( false , false );
if ( null === $composer ) {
2022-02-18 13:45:08 +00:00
$composer = Factory :: createGlobal ( $this -> io , $this -> disablePluginsByDefault , $this -> disableScriptsByDefault );
2014-10-26 17:47:29 +00:00
}
if ( null !== $composer ) {
2016-04-28 19:37:34 +00:00
$pm = $composer -> getPluginManager ();
2016-04-28 20:12:26 +00:00
foreach ( $pm -> getPluginCapabilities ( 'Composer\Plugin\Capability\CommandProvider' , array ( 'composer' => $composer , 'io' => $this -> io )) as $capability ) {
2016-04-28 19:37:34 +00:00
$newCommands = $capability -> getCommands ();
if ( ! is_array ( $newCommands )) {
throw new \UnexpectedValueException ( 'Plugin capability ' . get_class ( $capability ) . ' failed to return an array from getCommands' );
2014-10-26 17:47:29 +00:00
}
2016-04-28 19:37:34 +00:00
foreach ( $newCommands as $command ) {
if ( ! $command instanceof Command\BaseCommand ) {
throw new \UnexpectedValueException ( 'Plugin capability ' . get_class ( $capability ) . ' returned an invalid value, we expected an array of Composer\Command\BaseCommand objects' );
}
}
$commands = array_merge ( $commands , $newCommands );
2014-10-26 17:47:29 +00:00
}
}
return $commands ;
}
2020-01-07 03:29:00 +00:00
/**
2020-04-13 23:45:35 +00:00
* Get the working directory at startup time
2020-01-07 03:29:00 +00:00
*
2022-02-22 15:47:09 +00:00
* @ return string | false
2020-01-07 03:29:00 +00:00
*/
2020-01-13 14:50:34 +00:00
public function getInitialWorkingDirectory ()
2020-01-07 03:29:00 +00:00
{
2020-01-13 14:50:34 +00:00
return $this -> initialWorkingDirectory ;
2020-01-07 03:29:00 +00:00
}
2021-12-04 14:00:28 +00:00
/**
* @ return 'prompt' | bool
*/
private function getUseParentDirConfigValue ()
{
$config = Factory :: createConfig ( $this -> io );
return $config -> get ( 'use-parent-dir' );
}
2011-09-20 21:34:06 +00:00
}