2012-01-04 00:57:31 +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\Command ;
2014-07-05 18:51:15 +00:00
use Composer\DependencyResolver\Pool ;
2012-01-04 00:57:31 +00:00
use Composer\Json\JsonFile ;
2012-04-17 08:51:44 +00:00
use Composer\Factory ;
2012-08-14 19:03:38 +00:00
use Composer\Package\BasePackage ;
2014-07-15 02:25:44 +00:00
use Composer\Package\Version\VersionSelector ;
2012-02-16 19:39:59 +00:00
use Composer\Repository\CompositeRepository ;
use Composer\Repository\PlatformRepository ;
2012-11-24 14:34:50 +00:00
use Composer\Package\Version\VersionParser ;
2014-09-24 16:30:12 +00:00
use Composer\Util\ProcessExecutor ;
2012-01-04 00:57:31 +00:00
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputOption ;
use Symfony\Component\Console\Output\OutputInterface ;
2012-01-08 22:01:49 +00:00
use Symfony\Component\Process\Process ;
use Symfony\Component\Process\ExecutableFinder ;
2012-01-04 00:57:31 +00:00
/**
* @ author Justin Rainbow < justin . rainbow @ gmail . com >
2012-02-16 19:39:59 +00:00
* @ author Jordi Boggiano < j . boggiano @ seld . be >
2012-01-04 00:57:31 +00:00
*/
class InitCommand extends Command
{
2012-01-08 22:01:49 +00:00
private $gitConfig ;
2012-02-16 19:39:59 +00:00
private $repos ;
2014-07-05 18:51:15 +00:00
private $pool ;
2012-01-08 22:01:49 +00:00
public function parseAuthorString ( $author )
{
2013-07-25 20:40:41 +00:00
if ( preg_match ( '/^(?P<name>[- \.,\p{L}\'’ ]+) <(?P<email>.+?)>$/u' , $author , $match )) {
2013-05-25 16:01:14 +00:00
if ( $this -> isValidEmail ( $match [ 'email' ])) {
2012-01-08 22:01:49 +00:00
return array (
'name' => trim ( $match [ 'name' ]),
'email' => $match [ 'email' ]
);
}
}
throw new \InvalidArgumentException (
2012-02-16 19:40:57 +00:00
'Invalid author string. Must be in the format: ' .
'John Smith <john@example.com>'
2012-01-08 22:01:49 +00:00
);
}
2012-01-04 00:57:31 +00:00
protected function configure ()
{
$this
-> setName ( 'init' )
-> setDescription ( 'Creates a basic composer.json file in current directory.' )
-> setDefinition ( array (
2012-08-15 16:29:43 +00:00
new InputOption ( 'name' , null , InputOption :: VALUE_REQUIRED , 'Name of the package' ),
new InputOption ( 'description' , null , InputOption :: VALUE_REQUIRED , 'Description of package' ),
new InputOption ( 'author' , null , InputOption :: VALUE_REQUIRED , 'Author name of package' ),
2012-01-04 00:57:31 +00:00
// new InputOption('version', null, InputOption::VALUE_NONE, 'Version of package'),
2012-08-15 16:29:43 +00:00
new InputOption ( 'homepage' , null , InputOption :: VALUE_REQUIRED , 'Homepage of package' ),
2012-05-26 12:45:19 +00:00
new InputOption ( 'require' , null , InputOption :: VALUE_IS_ARRAY | InputOption :: VALUE_REQUIRED , 'Package to require with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"' ),
new InputOption ( 'require-dev' , null , InputOption :: VALUE_IS_ARRAY | InputOption :: VALUE_REQUIRED , 'Package to require for development with a version constraint, e.g. foo/bar:1.0.0 or foo/bar=1.0.0 or "foo/bar 1.0.0"' ),
2012-11-30 16:40:52 +00:00
new InputOption ( 'stability' , 's' , InputOption :: VALUE_REQUIRED , 'Minimum stability (empty or one of: ' . implode ( ', ' , array_keys ( BasePackage :: $stabilities )) . ')' ),
2013-03-03 06:06:29 +00:00
new InputOption ( 'license' , 'l' , InputOption :: VALUE_REQUIRED , 'License of package' ),
2012-01-04 00:57:31 +00:00
))
-> setHelp ( <<< EOT
2012-01-04 01:03:09 +00:00
The < info > init </ info > command creates a basic composer . json file
in the current directory .
2012-01-04 00:57:31 +00:00
2012-01-04 01:03:09 +00:00
< info > php composer . phar init </ info >
2012-01-04 00:57:31 +00:00
EOT
)
;
}
protected function execute ( InputInterface $input , OutputInterface $output )
{
2012-01-08 22:40:30 +00:00
$dialog = $this -> getHelperSet () -> get ( 'dialog' );
2012-01-04 00:57:31 +00:00
2013-03-03 06:06:29 +00:00
$whitelist = array ( 'name' , 'description' , 'author' , 'homepage' , 'require' , 'require-dev' , 'stability' , 'license' );
2012-01-04 00:57:31 +00:00
2012-01-08 22:01:49 +00:00
$options = array_filter ( array_intersect_key ( $input -> getOptions (), array_flip ( $whitelist )));
if ( isset ( $options [ 'author' ])) {
$options [ 'authors' ] = $this -> formatAuthors ( $options [ 'author' ]);
unset ( $options [ 'author' ]);
}
2012-12-15 17:35:32 +00:00
if ( isset ( $options [ 'stability' ])) {
$options [ 'minimum-stability' ] = $options [ 'stability' ];
unset ( $options [ 'stability' ]);
}
2012-08-24 11:38:54 +00:00
$options [ 'require' ] = isset ( $options [ 'require' ]) ? $this -> formatRequirements ( $options [ 'require' ]) : new \stdClass ;
if ( array () === $options [ 'require' ]) {
$options [ 'require' ] = new \stdClass ;
}
2012-01-04 00:57:31 +00:00
2012-08-14 19:03:38 +00:00
if ( isset ( $options [ 'require-dev' ])) {
$options [ 'require-dev' ] = $this -> formatRequirements ( $options [ 'require-dev' ]) ;
2012-08-24 11:38:54 +00:00
if ( array () === $options [ 'require-dev' ]) {
$options [ 'require-dev' ] = new \stdClass ;
}
2012-08-14 19:03:38 +00:00
}
2012-01-08 19:48:05 +00:00
$file = new JsonFile ( 'composer.json' );
2012-01-04 00:57:31 +00:00
2012-01-08 19:48:05 +00:00
$json = $file -> encode ( $options );
2012-01-04 00:57:31 +00:00
if ( $input -> isInteractive ()) {
$output -> writeln ( array (
'' ,
$json ,
''
));
if ( ! $dialog -> askConfirmation ( $output , $dialog -> getQuestion ( 'Do you confirm generation' , 'yes' , '?' ), true )) {
$output -> writeln ( '<error>Command aborted</error>' );
return 1 ;
}
}
$file -> write ( $options );
2012-01-08 22:27:19 +00:00
2012-12-08 16:47:44 +00:00
if ( $input -> isInteractive () && is_dir ( '.git' )) {
2012-01-08 22:27:19 +00:00
$ignoreFile = realpath ( '.gitignore' );
if ( false === $ignoreFile ) {
$ignoreFile = realpath ( '.' ) . '/.gitignore' ;
}
if ( ! $this -> hasVendorIgnore ( $ignoreFile )) {
$question = 'Would you like the <info>vendor</info> directory added to your <info>.gitignore</info> [<comment>yes</comment>]?' ;
if ( $dialog -> askConfirmation ( $output , $question , true )) {
$this -> addVendorIgnore ( $ignoreFile );
}
}
}
2012-01-04 00:57:31 +00:00
}
protected function interact ( InputInterface $input , OutputInterface $output )
{
2012-01-08 22:01:49 +00:00
$git = $this -> getGitConfig ();
2012-01-08 22:40:30 +00:00
$dialog = $this -> getHelperSet () -> get ( 'dialog' );
2012-01-08 22:55:12 +00:00
$formatter = $this -> getHelperSet () -> get ( 'formatter' );
$output -> writeln ( array (
'' ,
$formatter -> formatBlock ( 'Welcome to the Composer config generator' , 'bg=blue;fg=white' , true ),
''
));
2012-01-04 00:57:31 +00:00
// namespace
$output -> writeln ( array (
'' ,
'This command will guide you through creating your composer.json config.' ,
'' ,
));
$cwd = realpath ( " . " );
2012-08-15 16:29:43 +00:00
if ( ! $name = $input -> getOption ( 'name' )) {
2012-01-08 22:01:49 +00:00
$name = basename ( $cwd );
2012-11-20 19:53:19 +00:00
$name = preg_replace ( '{(?:([a-z])([A-Z])|([A-Z])([A-Z][a-z]))}' , '\\1\\3-\\2\\4' , $name );
$name = strtolower ( $name );
2012-01-08 22:01:49 +00:00
if ( isset ( $git [ 'github.user' ])) {
$name = $git [ 'github.user' ] . '/' . $name ;
2012-02-16 19:40:57 +00:00
} elseif ( ! empty ( $_SERVER [ 'USERNAME' ])) {
$name = $_SERVER [ 'USERNAME' ] . '/' . $name ;
} elseif ( get_current_user ()) {
$name = get_current_user () . '/' . $name ;
2012-01-08 22:33:06 +00:00
} else {
// package names must be in the format foo/bar
$name = $name . '/' . $name ;
2012-01-08 22:01:49 +00:00
}
2012-11-20 19:54:48 +00:00
} else {
if ( ! preg_match ( '{^[a-z0-9_.-]+/[a-z0-9_.-]+$}' , $name )) {
throw new \InvalidArgumentException (
'The package name ' . $name . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+'
);
}
2012-01-08 22:01:49 +00:00
}
2012-01-08 22:33:06 +00:00
$name = $dialog -> askAndValidate (
2012-01-04 00:57:31 +00:00
$output ,
2012-02-16 19:40:57 +00:00
$dialog -> getQuestion ( 'Package name (<vendor>/<name>)' , $name ),
2012-01-08 22:33:06 +00:00
function ( $value ) use ( $name ) {
if ( null === $value ) {
return $name ;
}
2012-11-20 19:49:45 +00:00
if ( ! preg_match ( '{^[a-z0-9_.-]+/[a-z0-9_.-]+$}' , $value )) {
2012-01-08 22:33:06 +00:00
throw new \InvalidArgumentException (
2012-11-20 19:49:45 +00:00
'The package name ' . $value . ' is invalid, it should be lowercase and have a vendor name, a forward slash, and a package name, matching: [a-z0-9_.-]+/[a-z0-9_.-]+'
2012-01-08 22:33:06 +00:00
);
}
return $value ;
}
2012-01-04 00:57:31 +00:00
);
$input -> setOption ( 'name' , $name );
$description = $input -> getOption ( 'description' ) ? : false ;
$description = $dialog -> ask (
$output ,
2013-09-20 23:10:24 +00:00
$dialog -> getQuestion ( 'Description' , $description ),
$description
2012-01-04 00:57:31 +00:00
);
$input -> setOption ( 'description' , $description );
2012-01-08 19:48:05 +00:00
2012-08-15 16:29:43 +00:00
if ( null === $author = $input -> getOption ( 'author' )) {
2012-01-08 22:01:49 +00:00
if ( isset ( $git [ 'user.name' ]) && isset ( $git [ 'user.email' ])) {
$author = sprintf ( '%s <%s>' , $git [ 'user.name' ], $git [ 'user.email' ]);
}
}
$self = $this ;
$author = $dialog -> askAndValidate (
$output ,
$dialog -> getQuestion ( 'Author' , $author ),
function ( $value ) use ( $self , $author ) {
if ( null === $value ) {
return $author ;
}
$author = $self -> parseAuthorString ( $value );
return sprintf ( '%s <%s>' , $author [ 'name' ], $author [ 'email' ]);
}
);
$input -> setOption ( 'author' , $author );
2012-11-30 16:40:52 +00:00
$minimumStability = $input -> getOption ( 'stability' ) ? : '' ;
2012-08-14 19:03:38 +00:00
$minimumStability = $dialog -> askAndValidate (
$output ,
$dialog -> getQuestion ( 'Minimum Stability' , $minimumStability ),
function ( $value ) use ( $self , $minimumStability ) {
if ( null === $value ) {
return $minimumStability ;
}
if ( ! isset ( BasePackage :: $stabilities [ $value ])) {
throw new \InvalidArgumentException (
'Invalid minimum stability "' . $value . '". Must be empty or one of: ' .
implode ( ', ' , array_keys ( BasePackage :: $stabilities ))
);
}
return $value ;
}
);
2012-11-30 16:40:52 +00:00
$input -> setOption ( 'stability' , $minimumStability );
2012-08-14 19:03:38 +00:00
2013-03-03 06:06:29 +00:00
$license = $input -> getOption ( 'license' ) ? : false ;
$license = $dialog -> ask (
$output ,
2013-09-20 23:10:24 +00:00
$dialog -> getQuestion ( 'License' , $license ),
$license
2013-03-03 06:06:29 +00:00
);
$input -> setOption ( 'license' , $license );
2012-01-08 19:48:05 +00:00
$output -> writeln ( array (
'' ,
'Define your dependencies.' ,
''
));
2012-01-08 22:01:49 +00:00
$requirements = array ();
2012-05-26 12:45:19 +00:00
if ( $dialog -> askConfirmation ( $output , $dialog -> getQuestion ( 'Would you like to define your dependencies (require) interactively' , 'yes' , '?' ), true )) {
$requirements = $this -> determineRequirements ( $input , $output , $input -> getOption ( 'require' ));
2012-01-08 19:48:05 +00:00
}
2012-01-08 22:01:49 +00:00
$input -> setOption ( 'require' , $requirements );
2012-05-26 12:45:19 +00:00
$devRequirements = array ();
if ( $dialog -> askConfirmation ( $output , $dialog -> getQuestion ( 'Would you like to define your dev dependencies (require-dev) interactively' , 'yes' , '?' ), true )) {
$devRequirements = $this -> determineRequirements ( $input , $output , $input -> getOption ( 'require-dev' ));
}
$input -> setOption ( 'require-dev' , $devRequirements );
2012-01-04 00:57:31 +00:00
}
2012-01-08 19:48:05 +00:00
protected function findPackages ( $name )
{
2014-07-05 18:51:15 +00:00
return $this -> getRepos () -> search ( $name );
}
2012-01-08 19:48:05 +00:00
2014-07-05 18:51:15 +00:00
protected function getRepos ()
{
2012-02-16 19:39:59 +00:00
if ( ! $this -> repos ) {
2012-04-27 00:29:43 +00:00
$this -> repos = new CompositeRepository ( array_merge (
array ( new PlatformRepository ),
2012-06-24 11:03:55 +00:00
Factory :: createDefaultRepositories ( $this -> getIO ())
2012-02-16 19:39:59 +00:00
));
}
2012-01-08 19:48:05 +00:00
2014-07-05 18:51:15 +00:00
return $this -> repos ;
}
2012-05-26 12:45:19 +00:00
protected function determineRequirements ( InputInterface $input , OutputInterface $output , $requires = array ())
2012-01-08 19:48:05 +00:00
{
2012-01-08 22:40:30 +00:00
$dialog = $this -> getHelperSet () -> get ( 'dialog' );
2012-01-08 19:48:05 +00:00
$prompt = $dialog -> getQuestion ( 'Search for a package' , false , ':' );
2012-05-26 12:45:19 +00:00
if ( $requires ) {
2012-11-24 14:34:50 +00:00
$requires = $this -> normalizeRequirements ( $requires );
$result = array ();
2014-07-05 18:51:15 +00:00
foreach ( $requires as $requirement ) {
2012-11-24 14:34:50 +00:00
if ( ! isset ( $requirement [ 'version' ])) {
2014-07-05 18:51:15 +00:00
2014-07-15 02:25:44 +00:00
// determine the best version automatically
2014-09-23 14:17:53 +00:00
$version = $this -> findBestVersionForPackage ( $input , $requirement [ 'name' ]);
2014-07-05 19:09:03 +00:00
$requirement [ 'version' ] = $version ;
2014-07-05 18:51:15 +00:00
$output -> writeln ( sprintf (
'Using version <info>%s</info> for <info>%s</info>' ,
$requirement [ 'version' ],
$requirement [ 'name' ]
));
2012-05-26 12:51:06 +00:00
}
2012-11-24 14:34:50 +00:00
$result [] = $requirement [ 'name' ] . ' ' . $requirement [ 'version' ];
2012-05-26 12:45:19 +00:00
}
2012-11-24 14:34:50 +00:00
return $result ;
2012-05-26 12:45:19 +00:00
}
2012-01-08 19:48:05 +00:00
while ( null !== $package = $dialog -> ask ( $output , $prompt )) {
$matches = $this -> findPackages ( $package );
if ( count ( $matches )) {
$output -> writeln ( array (
'' ,
sprintf ( 'Found <info>%s</info> packages matching <info>%s</info>' , count ( $matches ), $package ),
''
));
2013-03-10 12:32:59 +00:00
$exactMatch = null ;
$choices = array ();
2012-01-08 19:48:05 +00:00
foreach ( $matches as $position => $package ) {
2013-03-10 12:32:59 +00:00
$choices [] = sprintf ( ' <info>%5s</info> %s' , " [ $position ] " , $package [ 'name' ]);
if ( $package [ 'name' ] === $package ) {
$exactMatch = true ;
break ;
}
2012-01-08 19:48:05 +00:00
}
2013-03-10 12:32:59 +00:00
// no match, prompt which to pick
if ( ! $exactMatch ) {
$output -> writeln ( $choices );
$output -> writeln ( '' );
2012-01-08 19:48:05 +00:00
2013-03-10 12:32:59 +00:00
$validator = function ( $selection ) use ( $matches ) {
if ( '' === $selection ) {
return false ;
}
2012-01-08 19:48:05 +00:00
2013-03-10 12:32:59 +00:00
if ( ! is_numeric ( $selection ) && preg_match ( '{^\s*(\S+)\s+(\S.*)\s*$}' , $selection , $matches )) {
return $matches [ 1 ] . ' ' . $matches [ 2 ];
}
2012-02-16 19:41:12 +00:00
2013-03-10 12:32:59 +00:00
if ( ! isset ( $matches [( int ) $selection ])) {
throw new \Exception ( 'Not a valid selection' );
}
2012-01-08 19:48:05 +00:00
2013-03-10 12:32:59 +00:00
$package = $matches [( int ) $selection ];
2012-02-16 19:41:12 +00:00
2013-03-10 12:32:59 +00:00
return $package [ 'name' ];
};
2012-01-08 19:48:05 +00:00
2013-03-10 12:32:59 +00:00
$package = $dialog -> askAndValidate ( $output , $dialog -> getQuestion ( 'Enter package # to add, or the complete package name if it is not listed' , false , ':' ), $validator , 3 );
}
2014-07-15 02:25:44 +00:00
// no constraint yet, determine the best version automatically
2013-03-10 12:32:59 +00:00
if ( false !== $package && false === strpos ( $package , ' ' )) {
$validator = function ( $input ) {
$input = trim ( $input );
return $input ? : false ;
};
2014-07-15 02:25:44 +00:00
$constraint = $dialog -> askAndValidate (
$output ,
$dialog -> getQuestion ( 'Enter the version constraint to require (or leave blank to use the latest version)' , false , ':' ),
$validator ,
3 )
;
2013-03-10 12:32:59 +00:00
if ( false === $constraint ) {
2014-09-23 14:17:53 +00:00
$constraint = $this -> findBestVersionForPackage ( $input , $package );
2014-07-15 02:25:44 +00:00
$output -> writeln ( sprintf (
'Using version <info>%s</info> for <info>%s</info>' ,
$constraint ,
$package
));
2013-03-10 12:32:59 +00:00
}
$package .= ' ' . $constraint ;
}
2012-01-08 19:48:05 +00:00
if ( false !== $package ) {
2012-02-16 19:41:12 +00:00
$requires [] = $package ;
2012-01-08 19:48:05 +00:00
}
}
}
return $requires ;
}
2012-01-08 22:01:49 +00:00
protected function formatAuthors ( $author )
{
return array ( $this -> parseAuthorString ( $author ));
}
2012-01-08 19:48:05 +00:00
protected function formatRequirements ( array $requirements )
{
$requires = array ();
2012-11-24 14:34:50 +00:00
$requirements = $this -> normalizeRequirements ( $requirements );
2012-01-08 19:48:05 +00:00
foreach ( $requirements as $requirement ) {
2012-11-24 14:34:50 +00:00
$requires [ $requirement [ 'name' ]] = $requirement [ 'version' ];
2012-01-08 19:48:05 +00:00
}
2012-08-24 11:38:54 +00:00
return $requires ;
2012-01-08 19:48:05 +00:00
}
2012-01-08 22:01:49 +00:00
protected function getGitConfig ()
{
if ( null !== $this -> gitConfig ) {
return $this -> gitConfig ;
}
$finder = new ExecutableFinder ();
$gitBin = $finder -> find ( 'git' );
2014-09-24 16:30:12 +00:00
$cmd = new Process ( sprintf ( '%s config -l' , ProcessExecutor :: escape ( $gitBin )));
2012-01-08 22:01:49 +00:00
$cmd -> run ();
if ( $cmd -> isSuccessful ()) {
2012-04-08 20:17:15 +00:00
$this -> gitConfig = array ();
preg_match_all ( '{^([^=]+)=(.*)$}m' , $cmd -> getOutput (), $matches , PREG_SET_ORDER );
foreach ( $matches as $match ) {
$this -> gitConfig [ $match [ 1 ]] = $match [ 2 ];
}
2012-05-22 10:07:08 +00:00
2012-04-08 20:17:15 +00:00
return $this -> gitConfig ;
2012-01-08 22:01:49 +00:00
}
return $this -> gitConfig = array ();
}
2012-01-08 22:27:19 +00:00
/**
* Checks the local . gitignore file for the Composer vendor directory .
*
* Tested patterns include :
* " / $vendor "
* " $vendor "
* " $vendor / "
* " / $vendor / "
* " / $vendor /* "
* " $vendor /* "
*
* @ param string $ignoreFile
* @ param string $vendor
*
2012-06-23 09:58:18 +00:00
* @ return bool
2012-01-08 22:27:19 +00:00
*/
protected function hasVendorIgnore ( $ignoreFile , $vendor = 'vendor' )
{
if ( ! file_exists ( $ignoreFile )) {
return false ;
}
2013-01-05 17:04:57 +00:00
$pattern = sprintf ( '{^/?%s(/\*?)?$}' , preg_quote ( $vendor ));
2012-01-08 22:27:19 +00:00
$lines = file ( $ignoreFile , FILE_IGNORE_NEW_LINES );
foreach ( $lines as $line ) {
if ( preg_match ( $pattern , $line )) {
return true ;
}
}
return false ;
}
2012-11-24 14:34:50 +00:00
protected function normalizeRequirements ( array $requirements )
{
$parser = new VersionParser ();
return $parser -> parseNameVersionPairs ( $requirements );
}
2013-01-02 09:55:34 +00:00
protected function addVendorIgnore ( $ignoreFile , $vendor = '/vendor/' )
2012-01-08 22:27:19 +00:00
{
$contents = " " ;
if ( file_exists ( $ignoreFile )) {
$contents = file_get_contents ( $ignoreFile );
if ( " \n " !== substr ( $contents , 0 , - 1 )) {
$contents .= " \n " ;
}
}
2012-02-16 19:40:57 +00:00
file_put_contents ( $ignoreFile , $contents . $vendor . " \n " );
2012-01-08 22:27:19 +00:00
}
2013-05-25 16:01:14 +00:00
protected function isValidEmail ( $email )
{
2013-05-27 08:41:50 +00:00
// assume it's valid if we can't validate it
if ( ! function_exists ( 'filter_var' )) {
return true ;
}
// php <5.3.3 has a very broken email validator, so bypass checks
if ( version_compare ( PHP_VERSION , '5.3.3' , '<' )) {
return true ;
}
2013-05-25 16:01:14 +00:00
return false !== filter_var ( $email , FILTER_VALIDATE_EMAIL );
}
2014-07-15 02:25:44 +00:00
2014-09-23 14:17:53 +00:00
private function getPool ( InputInterface $input )
{
if ( ! $this -> pool ) {
$this -> pool = new Pool ( $this -> getMinimumStability ( $input ));
$this -> pool -> addRepository ( $this -> getRepos ());
}
return $this -> pool ;
}
private function getMinimumStability ( InputInterface $input )
{
if ( $input -> hasOption ( 'stability' )) {
return $input -> getOption ( 'stability' ) ? : 'stable' ;
}
$file = Factory :: getComposerFile ();
if ( is_file ( $file ) && is_readable ( $file ) && is_array ( $composer = json_decode ( file_get_contents ( $file ), true ))) {
if ( ! empty ( $composer [ 'minimum-stability' ])) {
return $composer [ 'minimum-stability' ];
}
}
return 'stable' ;
}
2014-07-15 02:25:44 +00:00
/**
* Given a package name , this determines the best version to use in the require key .
*
* This returns a version with the ~ operator prefixed when possible .
*
2014-09-23 14:17:53 +00:00
* @ param InputInterface $input
* @ param string $name
2014-07-15 02:25:44 +00:00
* @ return string
* @ throws \InvalidArgumentException
*/
2014-09-23 14:17:53 +00:00
private function findBestVersionForPackage ( InputInterface $input , $name )
2014-07-15 02:25:44 +00:00
{
// find the latest version allowed in this pool
2014-09-23 14:17:53 +00:00
$versionSelector = new VersionSelector ( $this -> getPool ( $input ));
2014-07-15 02:25:44 +00:00
$package = $versionSelector -> findBestCandidate ( $name );
if ( ! $package ) {
throw new \InvalidArgumentException ( sprintf (
'Could not find package %s at any version for your minimum-stability (%s). Check the package spelling or your minimum-stability' ,
$name ,
2014-09-23 14:17:53 +00:00
$this -> getMinimumStability ( $input )
2014-07-15 02:25:44 +00:00
));
}
2014-09-12 15:23:20 +00:00
return $versionSelector -> findRecommendedRequireVersion ( $package );
2014-07-15 02:25:44 +00:00
}
2012-05-22 10:07:08 +00:00
}