1
0
Fork 0

Removed unnecessary state from Depends command.

pull/4917/head
Niels Keurentjes 2016-02-15 12:48:33 +01:00
parent 21e262cb8a
commit 86374034b6
1 changed files with 7 additions and 10 deletions

View File

@ -35,9 +35,6 @@ use Symfony\Component\Console\Output\OutputInterface;
*/ */
class DependsCommand extends Command class DependsCommand extends Command
{ {
/** @var CompositeRepository */
private $repository;
protected function configure() protected function configure()
{ {
$this $this
@ -70,13 +67,13 @@ EOT
// Prepare repositories and set up a pool // Prepare repositories and set up a pool
$platformOverrides = $composer->getConfig()->get('platform') ?: array(); $platformOverrides = $composer->getConfig()->get('platform') ?: array();
$this->repository = new CompositeRepository(array( $repository = new CompositeRepository(array(
new ArrayRepository(array($composer->getPackage())), new ArrayRepository(array($composer->getPackage())),
$composer->getRepositoryManager()->getLocalRepository(), $composer->getRepositoryManager()->getLocalRepository(),
new PlatformRepository(array(), $platformOverrides), new PlatformRepository(array(), $platformOverrides),
)); ));
$pool = new Pool(); $pool = new Pool();
$pool->addRepository($this->repository); $pool->addRepository($repository);
// Find packages that are or provide the requested package first // Find packages that are or provide the requested package first
$needle = $input->getArgument('package'); $needle = $input->getArgument('package');
@ -97,7 +94,7 @@ EOT
$recursive = $renderTree || $input->getOption('recursive'); $recursive = $renderTree || $input->getOption('recursive');
// Resolve dependencies // Resolve dependencies
$results = $this->getDependers($needle, $constraint, $matchInvert, $recursive); $results = $this->getDependents($needle, $repository->getPackages(), $constraint, $matchInvert, $recursive);
if (empty($results)) { if (empty($results)) {
$extra = isset($constraint) ? sprintf(' in versions %smatching %s', $matchInvert ? 'not ' : '', $textConstraint) : ''; $extra = isset($constraint) ? sprintf(' in versions %smatching %s', $matchInvert ? 'not ' : '', $textConstraint) : '';
$this->getIO()->writeError(sprintf('<info>There is no installed package depending on "%s"%s</info>', $this->getIO()->writeError(sprintf('<info>There is no installed package depending on "%s"%s</info>',
@ -176,21 +173,21 @@ EOT
/** /**
* @param string $needle The package to inspect. * @param string $needle The package to inspect.
* @param PackageInterface[] $packages List of installed packages.
* @param ConstraintInterface|null $constraint Optional constraint to filter by. * @param ConstraintInterface|null $constraint Optional constraint to filter by.
* @param bool $invert Whether to invert matches on the previous constraint. * @param bool $invert Whether to invert matches on the previous constraint.
* @param bool $recurse Whether to recursively expand the requirement tree. * @param bool $recurse Whether to recursively expand the requirement tree.
* @return array An array with dependers as key, and as values an array containing the source package and the link respectively * @return array An array with dependers as key, and as values an array containing the source package and the link respectively
*/ */
private function getDependers($needle, $constraint = null, $invert = false, $recurse = true) private function getDependents($needle, $packages, $constraint = null, $invert = false, $recurse = true)
{ {
$needles = is_array($needle) ? $needle : array($needle); $needles = is_array($needle) ? $needle : array($needle);
$results = array(); $results = array();
/** /**
* Loop over all currently installed packages. * Loop over all currently installed packages.
* @var PackageInterface $package
*/ */
foreach ($this->repository->getPackages() as $package) { foreach ($packages as $package) {
// Retrieve all requirements, but dev only for the root package // Retrieve all requirements, but dev only for the root package
$links = $package->getRequires(); $links = $package->getRequires();
$links += $package->getReplaces(); $links += $package->getReplaces();
@ -203,7 +200,7 @@ EOT
foreach ($needles as $needle) { foreach ($needles as $needle) {
if ($link->getTarget() === $needle) { if ($link->getTarget() === $needle) {
if (is_null($constraint) || (($link->getConstraint()->matches($constraint) === !$invert))) { if (is_null($constraint) || (($link->getConstraint()->matches($constraint) === !$invert))) {
$results[$link->getSource()] = array($package, $link, $recurse ? $this->getDependers($link->getSource(), null, false, true) : array()); $results[$link->getSource()] = array($package, $link, $recurse ? $this->getDependents($link->getSource(), $packages, null, false, true) : array());
} }
} }
} }