DeployManager.php 5.96 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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Deploy\Model;

use Magento\Framework\App\View\Deployment\Version\StorageInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\Deploy\Console\Command\DeployStaticOptionsInterface as Options;
use Magento\Deploy\Model\Deploy\TemplateMinifier;
use Magento\Framework\App\State;

class DeployManager
{
    /**
     * @var array
     */
    private $packages = [];

    /**
     * @var OutputInterface
     */
    private $output;

    /**
     * @var array
     */
    private $options;

    /**
     * @var StorageInterface
     */
    private $versionStorage;

    /**
     * @var DeployStrategyProviderFactory
     */
    private $deployStrategyProviderFactory;

    /**
     * @var ProcessQueueManagerFactory
     */
    private $processQueueManagerFactory;

    /**
     * @var TemplateMinifier
     */
    private $templateMinifier;

    /**
     * @var bool
     */
    private $idDryRun;

    /**
     * @var State
     */
    private $state;

    /**
     * @param OutputInterface $output
     * @param StorageInterface $versionStorage
     * @param DeployStrategyProviderFactory $deployStrategyProviderFactory
     * @param ProcessQueueManagerFactory $processQueueManagerFactory
     * @param TemplateMinifier $templateMinifier
     * @param State $state
     * @param array $options
     */
    public function __construct(
        OutputInterface $output,
        StorageInterface $versionStorage,
        DeployStrategyProviderFactory $deployStrategyProviderFactory,
        ProcessQueueManagerFactory $processQueueManagerFactory,
        TemplateMinifier $templateMinifier,
        State $state,
        array $options
    ) {
        $this->output = $output;
        $this->options = $options;
        $this->versionStorage = $versionStorage;
        $this->deployStrategyProviderFactory = $deployStrategyProviderFactory;
        $this->processQueueManagerFactory = $processQueueManagerFactory;
        $this->templateMinifier = $templateMinifier;
        $this->state = $state;
        $this->idDryRun = !empty($this->options[Options::DRY_RUN]);
    }

    /**
     * Add package tie to area and theme
     *
     * @param string $area
     * @param string $themePath
     * @param string $locale
     * @return void
     */
    public function addPack($area, $themePath, $locale)
    {
        $this->packages[$area . '-' . $themePath][$locale] = [$area, $themePath];
    }

    /**
     * Deploy local packages with chosen deploy strategy
     * @return int
     */
    public function deploy()
    {
        if ($this->idDryRun) {
            $this->output->writeln('Dry run. Nothing will be recorded to the target directory.');
        } else {
            $version = (new \DateTime())->getTimestamp();
            $this->versionStorage->save($version);
        }

        /** @var DeployStrategyProvider $strategyProvider */
        $strategyProvider = $this->deployStrategyProviderFactory->create(
            ['output' => $this->output, 'options' => $this->options]
        );

        if ($this->isCanBeParalleled()) {
            $result = $this->runInParallel($strategyProvider);
        } else {
            $result = 0;
            foreach ($this->packages as $package) {
                $locales = array_keys($package);
                list($area, $themePath) = current($package);
                foreach ($strategyProvider->getDeployStrategies($area, $themePath, $locales) as $locale => $strategy) {
                    $result |= $this->state->emulateAreaCode(
                        $area,
                        [$strategy, 'deploy'],
                        [$area, $themePath, $locale]
                    );
                }
            }
        }

        $this->minifyTemplates();
        if (!$this->idDryRun) {
            $this->output->writeln("New version of deployed files: {$version}");
        }

        return $result;
    }

    /**
     * @return void
     */
    private function minifyTemplates()
    {
        $noHtmlMinify = isset($this->options[Options::NO_HTML_MINIFY]) ? $this->options[Options::NO_HTML_MINIFY] : null;
        if (!$noHtmlMinify && !$this->idDryRun) {
            $this->output->writeln('=== Minify templates ===');
            $minified = $this->templateMinifier->minifyTemplates();
            $this->output->writeln("\nSuccessful: {$minified} files modified\n---\n");
        }
    }

    /**
     * @param DeployStrategyProvider $strategyProvider
     * @return int
     */
    private function runInParallel($strategyProvider)
    {
        $processQueueManager = $this->processQueueManagerFactory->create(
            ['maxProcesses' => $this->getProcessesAmount()]
        );
        foreach ($this->packages as $package) {
            $locales = array_keys($package);
            list($area, $themePath) = current($package);
            $baseStrategy = null;
            $dependentStrategy = [];
            foreach ($strategyProvider->getDeployStrategies($area, $themePath, $locales) as $locale => $strategy) {
                $deploymentFunc = function () use ($area, $themePath, $locale, $strategy) {
                    return $this->state->emulateAreaCode($area, [$strategy, 'deploy'], [$area, $themePath, $locale]);
                };
                if (null === $baseStrategy) {
                    $baseStrategy = $deploymentFunc;
                } else {
                    $dependentStrategy[] = $deploymentFunc;
                }

            }
            $processQueueManager->addTaskToQueue($baseStrategy, $dependentStrategy);
        }

        return $processQueueManager->process();
    }

    /**
     * @return bool
     */
    private function isCanBeParalleled()
    {
        return function_exists('pcntl_fork') && $this->getProcessesAmount() > 1;
    }

    /**
     * @return int
     */
    private function getProcessesAmount()
    {
        return isset($this->options[Options::JOBS_AMOUNT]) ? (int)$this->options[Options::JOBS_AMOUNT] : 0;
    }
}