1
0
Fork 0
mirror of https://github.com/composer/composer synced 2025-05-11 09:32:55 +00:00

Load installed plugins at appropriate time and adapt tests accordingly

This commit is contained in:
Nils Adermann 2013-08-13 19:13:17 +02:00
parent 3e41977be7
commit 2f43e9aefb
9 changed files with 122 additions and 119 deletions

View file

@ -13,6 +13,8 @@
namespace Composer\Plugin;
use Composer\Composer;
use Composer\Package\Package;
use Composer\IO\IOInterface;
use Composer\Package\PackageInterface;
/**
@ -23,17 +25,34 @@ use Composer\Package\PackageInterface;
class PluginManager
{
protected $composer;
protected $io;
protected $plugins = array();
private static $classCounter = 0;
/**
* Initializes plugin manager
*
* @param Composer $composer
*/
public function __construct(Composer $composer)
public function __construct(Composer $composer, IOInterface $io)
{
$this->composer = $composer;
$this->io = $io;
}
public function loadInstalledPlugins()
{
$repo = $this->composer->getRepositoryManager()->getLocalRepository();
if ($repo) {
foreach ($repo->getPackages() as $package) {
if ('composer-plugin' === $package->getType() || 'composer-installer' === $package->getType()) {
$this->registerPackage($package);
}
}
}
}
/**
@ -46,4 +65,58 @@ class PluginManager
$this->plugins[] = $plugin;
$plugin->activate($this->composer);
}
public function getPlugins()
{
return $this->plugins;
}
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']);
$generator = $this->composer->getAutoloadGenerator();
$map = $generator->parseAutoloads(array(array($package, $downloadPath)), new Package('dummy', '1.0.0.0', '1.0.0'));
$classLoader = $generator->createLoader($map);
$classLoader->register();
foreach ($classes as $class) {
if (class_exists($class, false)) {
$code = file_get_contents($classLoader->findFile($class));
$code = preg_replace('{^(\s*)class\s+(\S+)}mi', '$1class $2_composer_tmp'.self::$classCounter, $code);
eval('?>'.$code);
$class .= '_composer_tmp'.self::$classCounter;
self::$classCounter++;
}
$plugin = new $class($this->io, $this->composer);
if ($oldInstallerPlugin) {
$this->composer->getInstallationManager()->addInstaller($installer);
} else {
$this->addPlugin($plugin);
}
}
}
public function getInstallPath(PackageInterface $package)
{
$targetDir = $package->getTargetDir();
return $this->getPackageBasePath($package) . ($targetDir ? '/'.$targetDir : '');
}
protected function getPackageBasePath(PackageInterface $package)
{
$vendorDir = rtrim($this->composer->getConfig()->get('vendor-dir'), '/');
return ($vendorDir ? $vendorDir.'/' : '') . $package->getPrettyName();
}
}