DeployManager.php 3.06 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
<?php
/**
 *
 *
 *
 *
 */

namespace MagentoHackathon\Composer\Magento;


use Composer\IO\IOInterface;
use MagentoHackathon\Composer\Magento\Deploy\Manager\Entry;
use MagentoHackathon\Composer\Magento\Deploystrategy\Copy;

class DeployManager
{
    /**
     * @var Entry[]
     */
    protected $packages = array();

    /**
     * @var IOInterface
     */
    protected $io;

    /**
     * an array with package names as key and priorities as value
     *
     * @var array
     */
    protected $sortPriority = array();

    /**
     * High priority
     *
     * An array of packages that must have high priority for deployment
     * For packages that need to be deployed before all other packages
     *
     * @var array
     */
    private $highPriority = [
        'magento/magento2-base' => 10
    ];

    public function __construct(IOInterface $io)
    {
        $this->io = $io;
    }

    public function addPackage(Entry $package)
    {
        $this->packages[] = $package;
    }

    public function setSortPriority($priorities)
    {
        $this->sortPriority = $priorities;
    }

    /**
     * Uses the sortPriority Array to sort the packages.
     *
     * Highest priority first.
     * Copy gets per default higher priority then others
     *
     * @return array
     */
    protected function sortPackages()
    {
        usort(
            $this->packages,
            function ($a, $b) {
                $aPriority = $this->getPackagePriority($a);
                $bPriority = $this->getPackagePriority($b);
                if ($aPriority == $bPriority) {
                    return 0;
                }
                return ($aPriority > $bPriority) ? -1 : 1;
            }
        );

        return $this->packages;
    }

    public function doDeploy()
    {
        $this->sortPackages();

        /** @var Entry $package */
        foreach ($this->packages as $package) {
            if ($this->io->isDebug()) {
                $this->io->write('start magento deploy for ' . $package->getPackageName());
            }
            try {
                $package->getDeployStrategy()->deploy();
            } catch (\ErrorException $e) {
                if ($this->io->isDebug()) {
                    $this->io->write($e->getMessage());
                }
            }
        }
    }

    /**
     * Determine the priority in which the package should be deployed
     *
     * @param Entry $package
     * @return int
     */
    private function getPackagePriority(Entry $package)
    {
        $result = 100;
        $maxPriority = max(array_merge($this->sortPriority, [100, 101]));

        if (isset($this->highPriority[$package->getPackageName()])) {
            $packagePriority = $this->highPriority[$package->getPackageName()];
            $result = intval($maxPriority) + intval($packagePriority);
        } elseif (isset($this->sortPriority[$package->getPackageName()])) {
            $result = $this->sortPriority[$package->getPackageName()];
        } elseif ($package->getDeployStrategy() instanceof Copy) {
            $result = 101;
        }

        return $result;
    }

}