From cd66328d68160911692ede4c6805844a8e1ec46d Mon Sep 17 00:00:00 2001 From: Nils Adermann Date: Thu, 15 Aug 2013 15:11:17 +0200 Subject: [PATCH] Autoload dependencies of plugins using a pool of only the local repo --- src/Composer/Plugin/PluginManager.php | 43 +++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 3 deletions(-) diff --git a/src/Composer/Plugin/PluginManager.php b/src/Composer/Plugin/PluginManager.php index f97900679..f13c9787d 100644 --- a/src/Composer/Plugin/PluginManager.php +++ b/src/Composer/Plugin/PluginManager.php @@ -17,6 +17,8 @@ use Composer\EventDispatcher\EventSubscriberInterface; use Composer\Package\Package; use Composer\IO\IOInterface; use Composer\Package\PackageInterface; +use Composer\Package\Link; +use Composer\DependencyResolver\Pool; /** * Plugin manager @@ -76,20 +78,55 @@ class PluginManager return $this->plugins; } + protected function collectDependencies(Pool $pool, array $collected, PackageInterface $package) + { + $requires = array_merge( + $package->getRequires(), + $package->getDevRequires() + ); + + foreach ($requires as $requireLink) { + $requiredPackage = $this->lookupInstalledPackage($pool, $requireLink); + if ($requiredPackage && !isset($collected[$requiredPackage->getName()])) { + $collected[$requiredPackage->getName()] = $requiredPackage; + $collected = $this->collectDependencies($pool, $collected, $requiredPackage); + } + } + + return $collected; + } + + protected function lookupInstalledPackage(Pool $pool, Link $link) + { + $packages = $pool->whatProvides($link->getTarget(), $link->getConstraint()); + + return (!empty($packages)) ? $packages[0] : null; + } + public function registerPackage(PackageInterface $package) { $oldInstallerPlugin = ($package->getType() === 'composer-installer'); - $downloadPath = $this->getInstallPath($package); - $extra = $package->getExtra(); if (empty($extra['class'])) { throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.'); } $classes = is_array($extra['class']) ? $extra['class'] : array($extra['class']); + $pool = new Pool('dev'); + $pool->addRepository($this->composer->getRepositoryManager()->getLocalRepository()); + + $autoloadPackages = array($package->getName() => $package); + $autoloadPackages = $this->collectDependencies($pool, $autoloadPackages, $package); + $generator = $this->composer->getAutoloadGenerator(); - $map = $generator->parseAutoloads(array(array($package, $downloadPath)), new Package('dummy', '1.0.0.0', '1.0.0')); + $autoloads = array(); + foreach ($autoloadPackages as $autoloadPackage) { + $downloadPath = $this->getInstallPath($autoloadPackage); + $autoloads[] = array($autoloadPackage, $downloadPath); + } + + $map = $generator->parseAutoloads($autoloads, new Package('dummy', '1.0.0.0', '1.0.0')); $classLoader = $generator->createLoader($map); $classLoader->register();