Make sure the plugins can use InstalledVersions to introspect themselves when being activated right after initial install

main
Jordi Boggiano 3 years ago
parent d4073a40f9
commit cf324ee664
No known key found for this signature in database
GPG Key ID: 7BBD42C429EC80BC

@ -467,7 +467,11 @@ class InstallationManager
{ {
$package = $operation->getPackage(); $package = $operation->getPackage();
$installer = $this->getInstaller($package->getType()); $installer = $this->getInstaller($package->getType());
if ($installer instanceof PluginInstaller) {
$promise = $installer->install($repo, $package, $this);
} else {
$promise = $installer->install($repo, $package); $promise = $installer->install($repo, $package);
}
$this->markForNotification($package); $this->markForNotification($package);
return $promise; return $promise;
@ -489,7 +493,11 @@ class InstallationManager
if ($initialType === $targetType) { if ($initialType === $targetType) {
$installer = $this->getInstaller($initialType); $installer = $this->getInstaller($initialType);
if ($installer instanceof PluginInstaller) {
$promise = $installer->update($repo, $initial, $target, $this);
} else {
$promise = $installer->update($repo, $initial, $target); $promise = $installer->update($repo, $initial, $target);
}
$this->markForNotification($target); $this->markForNotification($target);
} else { } else {
$promise = $this->getInstaller($initialType)->uninstall($repo, $initial); $promise = $this->getInstaller($initialType)->uninstall($repo, $initial);
@ -497,8 +505,12 @@ class InstallationManager
$promise = \React\Promise\resolve(); $promise = \React\Promise\resolve();
} }
$installManager = $this;
$installer = $this->getInstaller($targetType); $installer = $this->getInstaller($targetType);
$promise->then(function () use ($installer, $repo, $target) { $promise->then(function () use ($installer, $repo, $target, $installManager) {
if ($installer instanceof PluginInstaller) {
return $installer->install($repo, $target, $installManager);
}
return $installer->install($repo, $target); return $installer->install($repo, $target);
}); });
} }

@ -17,6 +17,7 @@ use Composer\IO\IOInterface;
use Composer\Repository\InstalledRepositoryInterface; use Composer\Repository\InstalledRepositoryInterface;
use Composer\Package\PackageInterface; use Composer\Package\PackageInterface;
use Composer\Util\Filesystem; use Composer\Util\Filesystem;
use Composer\Installer\InstallationManager;
use React\Promise\PromiseInterface; use React\Promise\PromiseInterface;
/** /**
@ -62,7 +63,7 @@ class PluginInstaller extends LibraryInstaller
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function install(InstalledRepositoryInterface $repo, PackageInterface $package) public function install(InstalledRepositoryInterface $repo, PackageInterface $package, InstallationManager $installationManager = null)
{ {
$promise = parent::install($repo, $package); $promise = parent::install($repo, $package);
if (!$promise instanceof PromiseInterface) { if (!$promise instanceof PromiseInterface) {
@ -72,8 +73,11 @@ class PluginInstaller extends LibraryInstaller
$pluginManager = $this->composer->getPluginManager(); $pluginManager = $this->composer->getPluginManager();
$self = $this; $self = $this;
return $promise->then(function () use ($self, $pluginManager, $package, $repo) { return $promise->then(function () use ($self, $pluginManager, $package, $repo, $installationManager) {
try { try {
if ($installationManager) {
$self->updateInstalledVersions($repo, $installationManager);
}
$pluginManager->registerPackage($package, true); $pluginManager->registerPackage($package, true);
} catch (\Exception $e) { } catch (\Exception $e) {
$self->rollbackInstall($e, $repo, $package); $self->rollbackInstall($e, $repo, $package);
@ -84,7 +88,7 @@ class PluginInstaller extends LibraryInstaller
/** /**
* {@inheritDoc} * {@inheritDoc}
*/ */
public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target) public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target, InstallationManager $installationManager = null)
{ {
$promise = parent::update($repo, $initial, $target); $promise = parent::update($repo, $initial, $target);
if (!$promise instanceof PromiseInterface) { if (!$promise instanceof PromiseInterface) {
@ -94,9 +98,12 @@ class PluginInstaller extends LibraryInstaller
$pluginManager = $this->composer->getPluginManager(); $pluginManager = $this->composer->getPluginManager();
$self = $this; $self = $this;
return $promise->then(function () use ($self, $pluginManager, $initial, $target, $repo) { return $promise->then(function () use ($self, $pluginManager, $initial, $target, $repo, $installationManager) {
try { try {
$pluginManager->deactivatePackage($initial, true); $pluginManager->deactivatePackage($initial, true);
if ($installationManager) {
$self->updateInstalledVersions($repo, $installationManager);
}
$pluginManager->registerPackage($target, true); $pluginManager->registerPackage($target, true);
} catch (\Exception $e) { } catch (\Exception $e) {
$self->rollbackInstall($e, $repo, $target); $self->rollbackInstall($e, $repo, $target);
@ -121,4 +128,16 @@ class PluginInstaller extends LibraryInstaller
parent::uninstall($repo, $package); parent::uninstall($repo, $package);
throw $e; throw $e;
} }
/**
* TODO v3 should make this private once we can drop PHP 5.3 support
* @private
*/
public function updateInstalledVersions(InstalledRepositoryInterface $repo, InstallationManager $installationManager)
{
$versions = $repo->generateInstalledVersions($installationManager);
if ($versions) {
\Composer\InstalledVersions::reload($versions);
}
}
} }

@ -123,7 +123,32 @@ class FilesystemRepository extends WritableArrayRepository
$this->file->write($data); $this->file->write($data);
if ($this->dumpVersions) { if ($this->dumpVersions) {
$versions = $this->generateInstalledVersions($installationManager);
$fs->filePutContentsIfModified($repoDir.'/installed.php', '<?php return '.var_export($versions, true).';'."\n");
$installedVersionsClass = file_get_contents(__DIR__.'/../InstalledVersions.php');
// while not strictly needed since https://github.com/composer/composer/pull/9635 - we keep this for BC
// and overall broader compatibility with people that may not use Composer's ClassLoader. They can
// simply include InstalledVersions.php manually and have it working in a basic way.
$installedVersionsClass = str_replace('private static $installed;', 'private static $installed = '.var_export($versions, true).';', $installedVersionsClass);
$fs->filePutContentsIfModified($repoDir.'/InstalledVersions.php', $installedVersionsClass);
\Composer\InstalledVersions::reload($versions);
}
}
/**
* @return ?array
* @internal
*/
public function generateInstalledVersions(InstallationManager $installationManager)
{
if (!$this->dumpVersions) {
return null;
}
$versions = array('versions' => array()); $versions = array('versions' => array());
$packages = $this->getPackages(); $packages = $this->getPackages();
$packages[] = $rootPackage = $this->rootPackage; $packages[] = $rootPackage = $this->rootPackage;
@ -202,21 +227,6 @@ class FilesystemRepository extends WritableArrayRepository
ksort($versions['versions']); ksort($versions['versions']);
ksort($versions); ksort($versions);
$fs->filePutContentsIfModified($repoDir.'/installed.php', '<?php return '.var_export($versions, true).';'."\n"); return $versions;
$installedVersionsClass = file_get_contents(__DIR__.'/../InstalledVersions.php');
// while not strictly needed since https://github.com/composer/composer/pull/9635 - we keep this for BC
// and overall broader compatibility with people that may not use Composer's ClassLoader. They can
// simply include InstalledVersions.php manually and have it working in a basic way.
$installedVersionsClass = str_replace('private static $installed;', 'private static $installed = '.var_export($versions, true).';', $installedVersionsClass);
$fs->filePutContentsIfModified($repoDir.'/InstalledVersions.php', $installedVersionsClass);
// make sure the InstalledVersions class is loaded and has the latest state
// not using the autoloader here to avoid loading the one from Composer's vendor dir
if (!class_exists('Composer\InstalledVersions', false)) {
include $repoDir.'/InstalledVersions.php';
} else {
\Composer\InstalledVersions::reload($versions);
}
}
} }
} }

Loading…
Cancel
Save