1
0
Fork 0

Allow show command to work without a composer.json

pull/281/head
Jordi Boggiano 2012-02-08 10:24:36 +01:00
parent 2d780fbef6
commit 93baa38d5c
3 changed files with 37 additions and 30 deletions

View File

@ -25,8 +25,8 @@ abstract class Command extends BaseCommand
/**
* @return \Composer\Composer
*/
protected function getComposer()
protected function getComposer($required = true)
{
return $this->getApplication()->getComposer();
return $this->getApplication()->getComposer($required);
}
}

View File

@ -17,6 +17,9 @@ use Composer\Package\PackageInterface;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Output\OutputInterface;
use Composer\Repository\CompositeRepository;
use Composer\Repository\PlatformRepository;
use Composer\Repository\ComposerRepository;
/**
* @author Robert Schönthal <seroscho@googlemail.com>
@ -44,13 +47,22 @@ EOT
protected function execute(InputInterface $input, OutputInterface $output)
{
$composer = $this->getComposer();
$package = $this->getPackage($input, $output, $composer);
if ($composer = $this->getComposer(false)) {
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
$installedRepo = new CompositeRepository(array($localRepo, new PlatformRepository()));
$repos = new CompositeRepository(array_merge(array($installedRepo), $composer->getRepositoryManager()->getRepositories()));
} else {
$output->writeln('No composer.json found in the current directory, showing packages from packagist.org');
$installedRepo = new PlatformRepository;
$repos = new CompositeRepository(array($installedRepo, new ComposerRepository(array('url' => 'http://packagist.org'))));
}
$package = $this->getPackage($input, $output, $installedRepo, $repos);
if (!$package) {
throw new \InvalidArgumentException('no package found');
}
$this->printMeta($input, $output, $package, $composer);
$this->printMeta($input, $output, $package, $installedRepo, $repos);
$this->printLinks($input, $output, $package, 'requires');
$this->printLinks($input, $output, $package, 'recommends');
$this->printLinks($input, $output, $package, 'replaces');
@ -63,16 +75,15 @@ EOT
* @return PackageInterface
* @throws \InvalidArgumentException
*/
protected function getPackage(InputInterface $input, OutputInterface $output, Composer $composer)
protected function getPackage(InputInterface $input, OutputInterface $output, $installedRepo, $repos)
{
// we have a name and a version so we can use ::findPackage
if ($input->getArgument('version')) {
return $composer->getRepositoryManager()->findPackage($input->getArgument('package'), $input->getArgument('version'));
return $repos->findPackage($input->getArgument('package'), $input->getArgument('version'));
}
// check if we have a local installation so we can grab the right package/version
$localRepo = $composer->getRepositoryManager()->getLocalRepository();
foreach ($localRepo->getPackages() as $package) {
foreach ($installedRepo->getPackages() as $package) {
if ($package->getName() === $input->getArgument('package')) {
return $package;
}
@ -80,15 +91,9 @@ EOT
// we only have a name, so search for the highest version of the given package
$highestVersion = null;
$repos = array_merge(
array($composer->getRepositoryManager()->getLocalRepository()),
$composer->getRepositoryManager()->getRepositories()
);
foreach ($repos as $repository) {
foreach ($repository->findPackagesByName($input->getArgument('package')) as $package) {
if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) {
$highestVersion = $package;
}
foreach ($repos->findPackagesByName($input->getArgument('package')) as $package) {
if (null === $highestVersion || version_compare($package->getVersion(), $highestVersion->getVersion(), '>=')) {
$highestVersion = $package;
}
}
@ -98,15 +103,15 @@ EOT
/**
* prints package meta data
*/
protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer)
protected function printMeta(InputInterface $input, OutputInterface $output, PackageInterface $package, $installedRepo, $repos)
{
$output->writeln('<info>name</info> : ' . $package->getPrettyName());
$this->printVersions($input, $output, $package, $composer);
$this->printVersions($input, $output, $package, $installedRepo, $repos);
$output->writeln('<info>type</info> : ' . $package->getType());
$output->writeln('<info>names</info> : ' . join(', ', $package->getNames()));
$output->writeln('<info>source</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getSourceType(), $package->getSourceUrl(), $package->getSourceReference()));
$output->writeln('<info>dist</info> : ' . sprintf('[%s] <comment>%s</comment> %s', $package->getDistType(), $package->getDistUrl(), $package->getDistReference()));
$output->writeln('<info>licence</info> : ' . join(', ', $package->getLicense()));
$output->writeln('<info>license</info> : ' . join(', ', $package->getLicense()));
if ($package->getAutoload()) {
$output->writeln("\n<info>autoload</info>");
@ -123,7 +128,7 @@ EOT
/**
* prints all available versions of this package and highlights the installed one if any
*/
protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, Composer $composer)
protected function printVersions(InputInterface $input, OutputInterface $output, PackageInterface $package, $installedRepo, $repos)
{
if ($input->getArgument('version')) {
$output->writeln('<info>version</info> : ' . $package->getPrettyVersion());
@ -132,16 +137,14 @@ EOT
$versions = array();
foreach ($composer->getRepositoryManager()->getRepositories() as $repository) {
foreach ($repository->findPackagesByName($package->getName()) as $version) {
$versions[] = $version->getPrettyVersion();
}
foreach ($repos->findPackagesByName($package->getName()) as $version) {
$versions[] = $version->getPrettyVersion();
}
$versions = join(', ', $versions);
// highlight installed version
if ($composer->getRepositoryManager()->getLocalRepository()->hasPackage($package)) {
if ($installedRepo->hasPackage($package)) {
$versions = str_replace($package->getPrettyVersion(), '<info>* ' . $package->getPrettyVersion() . '</info>', $versions);
}

View File

@ -71,14 +71,18 @@ class Application extends BaseApplication
/**
* @return Composer
*/
public function getComposer()
public function getComposer($required = true)
{
if (null === $this->composer) {
try {
$this->composer = Factory::create($this->io);
} catch (\InvalidArgumentException $e) {
$this->io->write($e->getMessage());
exit(1);
if ($required) {
$this->io->write($e->getMessage());
exit(1);
}
return;
}
}