JobComponentUninstall.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. /**
  3. * Copyright © 2013-2017 Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Update\Queue;
  7. use Magento\Composer\MagentoComposerApplication;
  8. use Magento\Update\Queue;
  9. use Magento\Update\Backup;
  10. use Magento\Update\Status;
  11. /**
  12. * Magento updater application 'uninstall component' job.
  13. */
  14. class JobComponentUninstall extends AbstractJob
  15. {
  16. /**
  17. * @var MagentoComposerApplication
  18. */
  19. protected $composerApp;
  20. /**
  21. * @var \Magento\Update\Queue
  22. */
  23. protected $queue;
  24. /**
  25. * Constructor
  26. *
  27. * @param string $name
  28. * @param array $params
  29. * @param MagentoComposerApplication $composerApp
  30. * @param Status $status
  31. */
  32. public function __construct(
  33. $name,
  34. $params,
  35. MagentoComposerApplication $composerApp = null,
  36. Status $status = null,
  37. Queue $queue = null
  38. ) {
  39. parent::__construct($name, $params, $status);
  40. $this->queue = $queue ? $queue : new Queue();
  41. $this->composerApp = $composerApp;
  42. }
  43. /**
  44. * {@inheritdoc}
  45. */
  46. public function execute()
  47. {
  48. try {
  49. $this->composerApp = $this->composerApp ? : $this->getComposerApp();
  50. $this->status->add('Starting composer remove...', \Psr\Log\LogLevel::INFO);
  51. if (isset($this->params['components'])) {
  52. $packages = [];
  53. foreach ($this->params['components'] as $compObj) {
  54. $packages[] = $compObj['name'];
  55. }
  56. $this->status->add(
  57. $this->composerApp->runComposerCommand(
  58. ['command' => 'remove', 'packages' => $packages, '--no-update' => true]
  59. ),
  60. \Psr\Log\LogLevel::INFO
  61. );
  62. } else {
  63. throw new \RuntimeException('Cannot find component to uninstall');
  64. }
  65. $this->status->add(
  66. $this->composerApp->runComposerCommand(['command' => 'update']),
  67. \Psr\Log\LogLevel::INFO
  68. );
  69. $this->status->add('Composer remove completed successfully', \Psr\Log\LogLevel::INFO);
  70. $this->queue->addJobs(
  71. [['name' => \Magento\Update\Queue\JobFactory::NAME_MAINTENANCE_MODE, 'params' => ['enable' => false]]]
  72. );
  73. } catch (\Exception $e) {
  74. $this->status->setUpdateError(true);
  75. throw new \RuntimeException(sprintf('Could not complete %s successfully: %s', $this, $e->getMessage()));
  76. }
  77. return $this;
  78. }
  79. /**
  80. * Get composer application
  81. *
  82. * @return MagentoComposerApplication
  83. */
  84. private function getComposerApp()
  85. {
  86. $vendorPath = MAGENTO_BP . '/' . (include (MAGENTO_BP . '/app/etc/vendor_path.php'));
  87. $composerPath = $vendorPath . '/../composer.json';
  88. $composerPath = realpath($composerPath);
  89. return new MagentoComposerApplication(MAGENTO_BP . '/var/composer_home', $composerPath);
  90. }
  91. }