DeployStrategyFactory.php 1.95 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\Deploy\Strategy;

use Magento\Framework\Exception\InputException;
use Magento\Framework\ObjectManagerInterface;

/**
 * Abstract factory class for instances of @see \Magento\Deploy\Strategy\StrategyInterface
 */
class DeployStrategyFactory
{
    /**
     * Standard deploy strategy
     */
    const DEPLOY_STRATEGY_STANDARD = 'standard';

    /**
     * Quick deploy strategy
     */
    const DEPLOY_STRATEGY_QUICK = 'quick';

    /**
     * Standard deploy strategy
     */
    const DEPLOY_STRATEGY_COMPACT = 'compact';

    /**
     * @var ObjectManagerInterface
     */
    private $objectManager;

    /**
     * Deployment strategies
     *
     * @var array
     */
    private $strategies = [];

    /**
     * DeployStrategyFactory constructor
     *
     * @param ObjectManagerInterface $objectManager
     * @param array $strategies
     */
    public function __construct(ObjectManagerInterface $objectManager, array $strategies = [])
    {
        $this->objectManager = $objectManager;
        $defaultStrategies = [
            self::DEPLOY_STRATEGY_STANDARD => StandardDeploy::class,
            self::DEPLOY_STRATEGY_QUICK => QuickDeploy::class,
            self::DEPLOY_STRATEGY_COMPACT => CompactDeploy::class,
        ];
        $this->strategies = array_replace($defaultStrategies, $strategies);
    }

    /**
     * Create new instance of deployment strategy
     *
     * @param string $type
     * @param array $arguments
     * @return StrategyInterface
     * @throws InputException
     */
    public function create($type, array $arguments = [])
    {
        $type = $type ?: self::DEPLOY_STRATEGY_STANDARD;
        if (!isset($this->strategies[$type])) {
            throw new InputException(__('Wrong deploy strategy type: %1', $type));
        }
        return $this->objectManager->create($this->strategies[$type], $arguments);
    }
}