PluginInstaller.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php
  2. /*
  3. * This file is part of Composer.
  4. *
  5. * (c) Nils Adermann <naderman@naderman.de>
  6. * Jordi Boggiano <j.boggiano@seld.be>
  7. *
  8. * For the full copyright and license information, please view the LICENSE
  9. * file that was distributed with this source code.
  10. */
  11. namespace Composer\Installer;
  12. use Composer\Composer;
  13. use Composer\IO\IOInterface;
  14. use Composer\Repository\InstalledRepositoryInterface;
  15. use Composer\Package\PackageInterface;
  16. /**
  17. * Installer for plugin packages
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. * @author Nils Adermann <naderman@naderman.de>
  21. */
  22. class PluginInstaller extends LibraryInstaller
  23. {
  24. private $installationManager;
  25. /**
  26. * Initializes Plugin installer.
  27. *
  28. * @param IOInterface $io
  29. * @param Composer $composer
  30. * @param string $type
  31. */
  32. public function __construct(IOInterface $io, Composer $composer, $type = 'library')
  33. {
  34. parent::__construct($io, $composer, 'composer-plugin');
  35. $this->installationManager = $composer->getInstallationManager();
  36. }
  37. /**
  38. * {@inheritDoc}
  39. */
  40. public function supports($packageType)
  41. {
  42. return $packageType === 'composer-plugin' || $packageType === 'composer-installer';
  43. }
  44. /**
  45. * {@inheritDoc}
  46. */
  47. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  48. {
  49. $extra = $package->getExtra();
  50. if (empty($extra['class'])) {
  51. throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.');
  52. }
  53. parent::install($repo, $package);
  54. try {
  55. $this->composer->getPluginManager()->registerPackage($package, true);
  56. } catch(\Exception $e) {
  57. // Rollback installation
  58. $this->io->writeError('Plugin installation failed, rolling back');
  59. parent::uninstall($repo, $package);
  60. throw $e;
  61. }
  62. }
  63. /**
  64. * {@inheritDoc}
  65. */
  66. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  67. {
  68. $extra = $target->getExtra();
  69. if (empty($extra['class'])) {
  70. throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.');
  71. }
  72. parent::update($repo, $initial, $target);
  73. $this->composer->getPluginManager()->registerPackage($target, true);
  74. }
  75. }