GeneratePatchCommand.php 4.16 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */
declare(strict_types=1);

namespace Magento\Developer\Console\Command;

use Magento\Developer\Model\Di\Information;
use Magento\Framework\Component\ComponentRegistrar;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Exception\InvalidArgumentException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Helper\Table;

/**
 * Allows to generate setup patches
 */
class GeneratePatchCommand extends Command
{
    /**
     * Command arguments and options
     */
    const COMMAND_NAME = 'setup:db-declaration:generate-patch';
    const MODULE_NAME = 'module';
    const INPUT_KEY_IS_REVERTABLE = 'revertable';
    const INPUT_KEY_PATCH_TYPE = 'type';
    const INPUT_KEY_PATCH_NAME = 'patch';

    /**
     * @var ComponentRegistrar
     */
    private $componentRegistrar;

    /**
     * GeneratePatchCommand constructor.
     * @param ComponentRegistrar $componentRegistrar
     */
    public function __construct(ComponentRegistrar $componentRegistrar)
    {
        $this->componentRegistrar = $componentRegistrar;
        parent::__construct();
    }

    /**
     * Configure command
     *
     * @inheritdoc
     * @throws InvalidArgumentException
     */
    protected function configure()
    {
        $this->setName(self::COMMAND_NAME)
            ->setDescription('Generate patch and put it in specific folder.')
            ->setDefinition([
                new InputArgument(
                    self::MODULE_NAME,
                    InputArgument::REQUIRED,
                    'Module name'
                ),
                new InputArgument(
                    self::INPUT_KEY_PATCH_NAME,
                    InputArgument::REQUIRED,
                    'Patch name'
                ),
                new InputOption(
                    self::INPUT_KEY_IS_REVERTABLE,
                    null,
                    InputOption::VALUE_OPTIONAL,
                    'Check whether patch is revertable or not.',
                    false
                ),
                new InputOption(
                    self::INPUT_KEY_PATCH_TYPE,
                    null,
                    InputOption::VALUE_OPTIONAL,
                    'Find out what type of patch should be generated.',
                    'data'
                ),
            ]);

        parent::configure();
    }

    /**
     * Patch template
     *
     * @return string
     */
    private function getPatchTemplate() : string
    {
        return file_get_contents(__DIR__ . '/patch_template.php.dist');
    }

    /**
     * @inheritdoc
     * @throws \InvalidArgumentException
     */
    protected function execute(InputInterface $input, OutputInterface $output) : int
    {
        $moduleName = $input->getArgument(self::MODULE_NAME);
        $patchName = $input->getArgument(self::INPUT_KEY_PATCH_NAME);
        $type = $input->getOption(self::INPUT_KEY_PATCH_TYPE);
        $modulePath = $this->componentRegistrar->getPath(ComponentRegistrar::MODULE, $moduleName);
        $preparedModuleName = str_replace('_', '\\', $moduleName);
        $preparedType = ucfirst($type);
        $patchInterface = sprintf('%sPatchInterface', $preparedType);
        $patchTemplateData = $this->getPatchTemplate();
        $patchTemplateData = str_replace('%moduleName%', $preparedModuleName, $patchTemplateData);
        $patchTemplateData = str_replace('%patchType%', $preparedType, $patchTemplateData);
        $patchTemplateData = str_replace('%patchInterface%', $patchInterface, $patchTemplateData);
        $patchTemplateData = str_replace('%class%', $patchName, $patchTemplateData);
        $patchDir = $patchToFile = $modulePath . '/Setup/Patch/' . $preparedType;

        if (!is_dir($patchDir)) {
            mkdir($patchDir, 0777, true);
        }
        $patchToFile = $patchDir . '/' . $patchName . '.php';
        file_put_contents($patchToFile, $patchTemplateData);
        return Cli::RETURN_SUCCESS;
    }
}