ConfigSetCommand.php 6.56 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
<?php
/**
 * Copyright © Magento, Inc. All rights reserved.
 * See COPYING.txt for license details.
 */

namespace Magento\Config\Console\Command;

use Magento\Config\App\Config\Type\System;
use Magento\Config\Console\Command\ConfigSet\ProcessorFacadeFactory;
use Magento\Deploy\Model\DeploymentConfig\ChangeDetector;
use Magento\Framework\App\DeploymentConfig;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Config\File\ConfigFilePool;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Output\OutputInterface;

/**
 * Command provides possibility to change system configuration.
 *
 * @api
 * @SuppressWarnings(PHPMD.CouplingBetweenObjects)
 * @since 101.0.0
 */
class ConfigSetCommand extends Command
{
    /**#@+
     * Constants for arguments and options.
     */
    const ARG_PATH = 'path';
    const ARG_VALUE = 'value';
    const OPTION_SCOPE = 'scope';
    const OPTION_SCOPE_CODE = 'scope-code';
    const OPTION_LOCK = 'lock';
    const OPTION_LOCK_ENV = 'lock-env';
    const OPTION_LOCK_CONFIG = 'lock-config';
    /**#@-*/

    /**#@-*/
    private $emulatedAreaProcessor;

    /**
     * The config change detector.
     *
     * @var ChangeDetector
     */
    private $changeDetector;

    /**
     * The factory for processor facade.
     *
     * @var ProcessorFacadeFactory
     */
    private $processorFacadeFactory;

    /**
     * Application deployment configuration
     *
     * @var DeploymentConfig
     */
    private $deploymentConfig;

    /**
     * @param EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor Emulator adminhtml area for CLI command
     * @param ChangeDetector $changeDetector The config change detector
     * @param ProcessorFacadeFactory $processorFacadeFactory The factory for processor facade
     * @param DeploymentConfig $deploymentConfig Application deployment configuration
     */
    public function __construct(
        EmulatedAdminhtmlAreaProcessor $emulatedAreaProcessor,
        ChangeDetector $changeDetector,
        ProcessorFacadeFactory $processorFacadeFactory,
        DeploymentConfig $deploymentConfig
    ) {
        $this->emulatedAreaProcessor = $emulatedAreaProcessor;
        $this->changeDetector = $changeDetector;
        $this->processorFacadeFactory = $processorFacadeFactory;
        $this->deploymentConfig = $deploymentConfig;

        parent::__construct();
    }

    /**
     * @inheritdoc
     * @since 101.0.0
     */
    protected function configure()
    {
        $this->setName('config:set')
            ->setDescription('Change system configuration')
            ->setDefinition([
                new InputArgument(
                    static::ARG_PATH,
                    InputArgument::REQUIRED,
                    'Configuration path in format section/group/field_name'
                ),
                new InputArgument(static::ARG_VALUE, InputArgument::REQUIRED, 'Configuration value'),
                new InputOption(
                    static::OPTION_SCOPE,
                    null,
                    InputArgument::OPTIONAL,
                    'Configuration scope (default, website, or store)',
                    ScopeConfigInterface::SCOPE_TYPE_DEFAULT
                ),
                new InputOption(
                    static::OPTION_SCOPE_CODE,
                    null,
                    InputArgument::OPTIONAL,
                    'Scope code (required only if scope is not \'default\')'
                ),
                new InputOption(
                    static::OPTION_LOCK_ENV,
                    'le',
                    InputOption::VALUE_NONE,
                    'Lock value which prevents modification in the Admin (will be saved in app/etc/env.php)'
                ),
                new InputOption(
                    static::OPTION_LOCK_CONFIG,
                    'lc',
                    InputOption::VALUE_NONE,
                    'Lock and share value with other installations, prevents modification in the Admin '
                    . '(will be saved in app/etc/config.php)'
                ),
                new InputOption(
                    static::OPTION_LOCK,
                    'l',
                    InputOption::VALUE_NONE,
                    'Deprecated, use the --' . static::OPTION_LOCK_ENV . ' option instead.'
                ),
            ]);

        parent::configure();
    }

    /**
     * Creates and run appropriate processor, depending on input options.
     *
     * {@inheritdoc}
     * @since 101.0.0
     */
    protected function execute(InputInterface $input, OutputInterface $output)
    {
        if (!$this->deploymentConfig->isAvailable()) {
            $output->writeln(
                '<error>You cannot run this command because the Magento application is not installed.</error>'
            );
            return Cli::RETURN_FAILURE;
        }
        if ($this->changeDetector->hasChanges(System::CONFIG_TYPE)) {
            $output->writeln(
                '<error>'
                . 'This command is unavailable right now. '
                . 'To continue working with it please run app:config:import or setup:upgrade command before.'
                . '</error>'
            );

            return Cli::RETURN_FAILURE;
        }

        try {
            $message = $this->emulatedAreaProcessor->process(function () use ($input) {

                $lock = $input->getOption(static::OPTION_LOCK_ENV)
                    || $input->getOption(static::OPTION_LOCK_CONFIG)
                    || $input->getOption(static::OPTION_LOCK);

                $lockTargetPath = ConfigFilePool::APP_ENV;
                if ($input->getOption(static::OPTION_LOCK_CONFIG)) {
                    $lockTargetPath = ConfigFilePool::APP_CONFIG;
                }

                return $this->processorFacadeFactory->create()->processWithLockTarget(
                    $input->getArgument(static::ARG_PATH),
                    $input->getArgument(static::ARG_VALUE),
                    $input->getOption(static::OPTION_SCOPE),
                    $input->getOption(static::OPTION_SCOPE_CODE),
                    $lock,
                    $lockTargetPath
                );
            });

            $output->writeln('<info>' . $message . '</info>');

            return Cli::RETURN_SUCCESS;
        } catch (\Exception $exception) {
            $output->writeln('<error>' . $exception->getMessage() . '</error>');

            return Cli::RETURN_FAILURE;
        }
    }
}