JobUpdate.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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\Rollback;
  11. /**
  12. * Magento updater application 'update' job.
  13. */
  14. class JobUpdate extends AbstractJob
  15. {
  16. /**
  17. * @var \Magento\Update\Backup
  18. */
  19. protected $backup;
  20. /**
  21. * @var \Magento\Update\Queue\JobRollback
  22. */
  23. protected $jobRollback;
  24. /**
  25. * @var Rollback
  26. */
  27. protected $rollback;
  28. /**
  29. * @var MagentoComposerApplication
  30. */
  31. protected $composerApp;
  32. /**
  33. * @var Queue
  34. */
  35. protected $queue;
  36. /**
  37. * Constructor
  38. *
  39. * @param string $name
  40. * @param array $params
  41. * @param Queue $queue
  42. * @param MagentoComposerApplication $composerApp
  43. * @param \Magento\Update\Status $status
  44. * @param Backup $backup
  45. * @param Rollback $rollback
  46. */
  47. public function __construct(
  48. $name,
  49. $params,
  50. Queue $queue = null,
  51. MagentoComposerApplication $composerApp = null,
  52. \Magento\Update\Status $status = null,
  53. Backup $backup = null,
  54. Rollback $rollback = null
  55. ) {
  56. parent::__construct($name, $params, $status);
  57. $this->queue = $queue ? $queue : new Queue();
  58. $this->backup = $backup ? $backup : new Backup();
  59. $this->rollback = $rollback ? $rollback : new Rollback();
  60. $this->composerApp = $composerApp;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function execute()
  66. {
  67. try {
  68. $this->composerApp = $this->composerApp ? : $this->getComposerApp();
  69. $this->status->add('Starting composer update...', \Psr\Log\LogLevel::INFO);
  70. if (isset($this->params['components'])) {
  71. $packages = [];
  72. foreach ($this->params['components'] as $compObj) {
  73. $packages[] = implode(' ', $compObj);
  74. }
  75. foreach ($packages as $package) {
  76. if (strpos($package, 'magento/product-enterprise-edition') !== false) {
  77. $this->composerApp->runComposerCommand(
  78. [
  79. 'command' => 'remove',
  80. 'packages' => ['magento/product-community-edition'],
  81. '--no-update' => true
  82. ]
  83. );
  84. }
  85. }
  86. $this->status->add(
  87. $this->composerApp->runComposerCommand(
  88. ['command' => 'require', 'packages' => $packages, '--no-update' => true]
  89. ),
  90. \Psr\Log\LogLevel::INFO
  91. );
  92. } else {
  93. throw new \RuntimeException('Cannot find component to update');
  94. }
  95. $this->status->add(
  96. $this->composerApp->runComposerCommand(['command' => 'update']),
  97. \Psr\Log\LogLevel::INFO
  98. );
  99. $this->status->add('Composer update completed successfully', \Psr\Log\LogLevel::INFO);
  100. $this->createSetupUpgradeTasks();
  101. } catch (\Exception $e) {
  102. $this->status->setUpdateError(true);
  103. throw new \RuntimeException(sprintf('Could not complete %s successfully: %s', $this, $e->getMessage()));
  104. }
  105. return $this;
  106. }
  107. /**
  108. * Create setup:upgrade task for setup application cron
  109. *
  110. * @return void
  111. */
  112. private function createSetupUpgradeTasks()
  113. {
  114. $jobs = [['name' => 'setup:upgrade', 'params' => []]];
  115. $this->queue->addJobs($jobs);
  116. }
  117. /**
  118. * Get composer application
  119. *
  120. * @return MagentoComposerApplication
  121. */
  122. private function getComposerApp()
  123. {
  124. $vendorPath = MAGENTO_BP . '/' . (include (MAGENTO_BP . '/app/etc/vendor_path.php'));
  125. $composerPath = $vendorPath . '/../composer.json';
  126. $composerPath = realpath($composerPath);
  127. return new MagentoComposerApplication(MAGENTO_BP . '/var/composer_home', $composerPath);
  128. }
  129. }