2012-01-05 03:11:37 +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 ;
2012-12-02 14:15:37 +00:00
use Composer\DependencyResolver\Pool ;
2012-01-05 03:11:37 +00:00
use Symfony\Component\Console\Input\InputInterface ;
use Symfony\Component\Console\Input\InputArgument ;
2012-01-15 12:00:39 +00:00
use Symfony\Component\Console\Input\InputOption ;
2012-01-05 03:11:37 +00:00
use Symfony\Component\Console\Output\OutputInterface ;
/**
* @ author Justin Rainbow < justin . rainbow @ gmail . com >
2012-01-15 12:00:39 +00:00
* @ author Jordi Boggiano < j . boggiano @ seld . be >
2012-01-05 03:11:37 +00:00
*/
class DependsCommand extends Command
{
2012-04-14 21:53:12 +00:00
protected $linkTypes = array (
2013-01-08 16:38:27 +00:00
'require' => array ( 'requires' , 'requires' ),
'require-dev' => array ( 'devRequires' , 'requires (dev)' ),
2012-04-14 21:53:12 +00:00
);
2012-02-19 14:04:40 +00:00
2012-01-05 03:11:37 +00:00
protected function configure ()
{
$this
-> setName ( 'depends' )
2012-01-15 12:00:39 +00:00
-> setDescription ( 'Shows which packages depend on the given package' )
2012-01-05 03:11:37 +00:00
-> setDefinition ( array (
2012-01-15 12:00:39 +00:00
new InputArgument ( 'package' , InputArgument :: REQUIRED , 'Package to inspect' ),
2012-12-02 14:15:37 +00:00
new InputOption ( 'link-type' , '' , InputOption :: VALUE_REQUIRED | InputOption :: VALUE_IS_ARRAY , 'Link types to show (require, require-dev)' , array_keys ( $this -> linkTypes )),
2012-01-05 03:11:37 +00:00
))
-> setHelp ( <<< EOT
2012-01-15 12:00:39 +00:00
Displays detailed information about where a package is referenced .
2012-01-05 03:11:37 +00:00
< info > php composer . phar depends composer / composer </ info >
EOT
)
;
}
protected function execute ( InputInterface $input , OutputInterface $output )
{
2013-03-03 00:54:14 +00:00
$repo = $this -> getComposer () -> getRepositoryManager () -> getLocalRepository ();
2012-12-02 14:15:37 +00:00
$needle = $input -> getArgument ( 'package' );
$pool = new Pool ();
2013-03-03 00:54:14 +00:00
$pool -> addRepository ( $repo );
2012-12-02 14:15:37 +00:00
$packages = $pool -> whatProvides ( $needle );
if ( empty ( $packages )) {
throw new \InvalidArgumentException ( 'Could not find package "' . $needle . '" in your project.' );
}
2012-01-05 03:11:37 +00:00
2012-08-23 18:05:16 +00:00
$linkTypes = $this -> linkTypes ;
2012-01-05 03:11:37 +00:00
2012-08-23 18:05:16 +00:00
$types = array_map ( function ( $type ) use ( $linkTypes ) {
$type = rtrim ( $type , 's' );
if ( ! isset ( $linkTypes [ $type ])) {
throw new \InvalidArgumentException ( 'Unexpected link type: ' . $type . ', valid types: ' . implode ( ', ' , array_keys ( $linkTypes )));
}
2012-01-05 03:11:37 +00:00
2012-08-23 18:05:16 +00:00
return $type ;
}, $input -> getOption ( 'link-type' ));
2013-01-08 16:38:27 +00:00
$messages = array ();
2013-03-03 00:54:14 +00:00
$repo -> filterPackages ( function ( $package ) use ( $needle , $types , $linkTypes , & $messages ) {
static $outputPackages = array ();
foreach ( $types as $type ) {
foreach ( $package -> { 'get' . $linkTypes [ $type ][ 0 ]}() as $link ) {
if ( $link -> getTarget () === $needle ) {
if ( ! isset ( $outputPackages [ $package -> getName ()])) {
$messages [] = '<info>' . $package -> getPrettyName () . '</info> ' . $linkTypes [ $type ][ 1 ] . ' ' . $needle . ' (<info>' . $link -> getPrettyConstraint () . '</info>)' ;
$outputPackages [ $package -> getName ()] = true ;
2012-01-08 21:39:42 +00:00
}
2012-01-05 03:11:37 +00:00
}
}
2013-03-03 00:54:14 +00:00
}
});
2012-12-02 14:15:37 +00:00
2013-01-08 16:38:27 +00:00
if ( $messages ) {
sort ( $messages );
$output -> writeln ( $messages );
} else {
2012-12-02 14:15:37 +00:00
$output -> writeln ( '<info>There is no installed package depending on "' . $needle . '".</info>' );
}
2012-01-15 12:00:39 +00:00
}
2012-05-22 10:07:08 +00:00
}