PluginInstaller.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. */
  31. public function __construct(IOInterface $io, Composer $composer)
  32. {
  33. parent::__construct($io, $composer, 'composer-plugin');
  34. $this->installationManager = $composer->getInstallationManager();
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function supports($packageType)
  40. {
  41. return $packageType === 'composer-plugin' || $packageType === 'composer-installer';
  42. }
  43. /**
  44. * {@inheritDoc}
  45. */
  46. public function install(InstalledRepositoryInterface $repo, PackageInterface $package)
  47. {
  48. $extra = $package->getExtra();
  49. if (empty($extra['class'])) {
  50. throw new \UnexpectedValueException('Error while installing '.$package->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.');
  51. }
  52. parent::install($repo, $package);
  53. try {
  54. $this->composer->getPluginManager()->registerPackage($package, true);
  55. } catch (\Exception $e) {
  56. // Rollback installation
  57. $this->io->writeError('Plugin installation failed, rolling back');
  58. parent::uninstall($repo, $package);
  59. throw $e;
  60. }
  61. }
  62. /**
  63. * {@inheritDoc}
  64. */
  65. public function update(InstalledRepositoryInterface $repo, PackageInterface $initial, PackageInterface $target)
  66. {
  67. $extra = $target->getExtra();
  68. if (empty($extra['class'])) {
  69. throw new \UnexpectedValueException('Error while installing '.$target->getPrettyName().', composer-plugin packages should have a class defined in their extra key to be usable.');
  70. }
  71. parent::update($repo, $initial, $target);
  72. $this->composer->getPluginManager()->registerPackage($target, true);
  73. }
  74. }