1
0
Fork 0

Add RepositoryInterface::filterPackages to stream ops on lists

This cuts down on memory usage and also speeds up the search command to a third of its previous time
pull/1015/head
Jordi Boggiano 2012-08-23 19:16:23 +02:00
parent 9965f02951
commit e3b6bd781c
4 changed files with 64 additions and 1 deletions

View File

@ -112,6 +112,20 @@ class ArrayRepository implements RepositoryInterface
} }
} }
/**
* {@inheritDoc}
*/
public function filterPackages($callback, $class = 'Composer\Package\Package')
{
foreach ($this->getPackages() as $package) {
if (false === $callback($package)) {
return false;
}
}
return true;
}
protected function createAliasPackage(PackageInterface $package, $alias = null, $prettyAlias = null) protected function createAliasPackage(PackageInterface $package, $alias = null, $prettyAlias = null)
{ {
return new AliasPackage($package, $alias ?: $package->getAlias(), $prettyAlias ?: $package->getPrettyAlias()); return new AliasPackage($package, $alias ?: $package->getAlias(), $prettyAlias ?: $package->getPrettyAlias());

View File

@ -122,6 +122,27 @@ class ComposerRepository extends ArrayRepository implements NotifiableRepository
return $this->minimalPackages; return $this->minimalPackages;
} }
/**
* {@inheritDoc}
*/
public function filterPackages($callback, $class = 'Composer\Package\Package')
{
$repoData = $this->loadDataFromServer();
foreach ($repoData as $package) {
if (false === $callback($package = $this->loader->load($package, $class))) {
return false;
}
if ($package->getAlias()) {
if (false === $callback($this->createAliasPackage($package))) {
return false;
}
}
}
return true;
}
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
@ -154,7 +175,7 @@ class ComposerRepository extends ArrayRepository implements NotifiableRepository
$repoData = $this->loadDataFromServer(); $repoData = $this->loadDataFromServer();
foreach ($repoData as $package) { foreach ($repoData as $package) {
$this->addPackage($this->loader->load($package, 'Composer\Package\Package')); $this->addPackage($this->loader->load($package, 'Composer\Package\CompletePackage'));
} }
} }

View File

@ -91,6 +91,20 @@ class CompositeRepository implements RepositoryInterface
return call_user_func_array('array_merge', $packages); return call_user_func_array('array_merge', $packages);
} }
/**
* {@inheritDoc}
*/
public function filterPackages($callback, $class = 'Composer\Package\Package')
{
foreach ($this->repositories as $repository) {
if (false === $repository->filterPackages($callback, $class)) {
return false;
}
}
return true;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */

View File

@ -51,6 +51,20 @@ interface RepositoryInterface extends \Countable
*/ */
public function findPackages($name, $version = null); public function findPackages($name, $version = null);
/**
* Filters all the packages throuhg a callback
*
* The packages are not guaranteed to be instances in the repository
* and this can only be used for streaming through a list of packages.
*
* If the callback returns false, the process stops
*
* @param callable $callback
* @param string $class
* @return bool false if the process was interrupted, true otherwise
*/
public function filterPackages($callback, $class = 'Composer\Package\Package');
/** /**
* Returns list of registered packages. * Returns list of registered packages.
* *