JobComponentUninstall.php 3.01 KB
Newer Older
Ketan's avatar
Ketan committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
<?php
/**
 * Copyright © 2013-2017 Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Update\Queue;

use Magento\Composer\MagentoComposerApplication;
use Magento\Update\Queue;
use Magento\Update\Backup;
use Magento\Update\Status;

/**
 * Magento updater application 'uninstall component' job.
 */
class JobComponentUninstall extends AbstractJob
{
    /**
     * @var MagentoComposerApplication
     */
    protected $composerApp;

    /**
     * @var \Magento\Update\Queue
     */
    protected $queue;

    /**
     * Constructor
     *
     * @param string $name
     * @param array $params
     * @param MagentoComposerApplication $composerApp
     * @param Status $status
     */
    public function __construct(
        $name,
        $params,
        MagentoComposerApplication $composerApp = null,
        Status $status = null,
        Queue $queue = null
    ) {
        parent::__construct($name, $params, $status);
        $this->queue = $queue ? $queue : new Queue();
        $this->composerApp = $composerApp;
    }

    /**
     * {@inheritdoc}
     */
    public function execute()
    {
        try {
            $this->composerApp = $this->composerApp ? : $this->getComposerApp();
            $this->status->add('Starting composer remove...', \Psr\Log\LogLevel::INFO);
            if (isset($this->params['components'])) {
                $packages = [];
                foreach ($this->params['components'] as $compObj) {
                    $packages[] = $compObj['name'];
                }
                $this->status->add(
                    $this->composerApp->runComposerCommand(
                        ['command' => 'remove', 'packages' => $packages, '--no-update' => true]
                    ),
                    \Psr\Log\LogLevel::INFO
                );
            } else {
                throw new \RuntimeException('Cannot find component to uninstall');
            }
            $this->status->add(
                $this->composerApp->runComposerCommand(['command' => 'update']),
                \Psr\Log\LogLevel::INFO
            );
            $this->status->add('Composer remove completed successfully', \Psr\Log\LogLevel::INFO);
            $this->queue->addJobs(
                [['name' => \Magento\Update\Queue\JobFactory::NAME_MAINTENANCE_MODE, 'params' => ['enable' => false]]]
            );
        } catch (\Exception $e) {
            $this->status->setUpdateError(true);
            throw new \RuntimeException(sprintf('Could not complete %s successfully: %s', $this, $e->getMessage()));
        }
        return $this;
    }

    /**
     * Get composer application
     *
     * @return MagentoComposerApplication
     */
    private function getComposerApp()
    {
        $vendorPath = MAGENTO_BP . '/' . (include (MAGENTO_BP . '/app/etc/vendor_path.php'));
        $composerPath = $vendorPath . '/../composer.json';
        $composerPath = realpath($composerPath);

        return new MagentoComposerApplication(MAGENTO_BP . '/var/composer_home', $composerPath);
    }
}