-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathAddPathToWhitelist.php
127 lines (113 loc) · 4.04 KB
/
AddPathToWhitelist.php
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 2021 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
declare(strict_types=1);
namespace Magento\ConfigurationDataExporter\Console\Command;
use Magento\Framework\Console\Cli;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\ConfigurationDataExporter\Model\Whitelist\EnvironmentProvider;
use Magento\Framework\Config\File\ConfigFilePool;
/**
* Command provides possibility to add configuration paths to whitelist in env.php
*/
class AddPathToWhitelist extends \Symfony\Component\Console\Command\Command
{
const COMMAND_NAME = 'commerce-data-export:config:add-paths-to-whitelist';
const INPUT_OPTION_PATHS = 'paths';
/**
* @var \Magento\Framework\App\DeploymentConfig
*/
private $deploymentConfig;
/**
* @var \Magento\Framework\App\DeploymentConfig\Writer
*/
private $writer;
/**
* @param \Magento\Framework\App\DeploymentConfig $deploymentConfig
* @param \Magento\Framework\App\DeploymentConfig\Writer $writer
*/
public function __construct(
\Magento\Framework\App\DeploymentConfig $deploymentConfig,
\Magento\Framework\App\DeploymentConfig\Writer $writer
) {
parent::__construct();
$this->deploymentConfig = $deploymentConfig;
$this->writer = $writer;
}
/**
* @inheritdoc
*/
protected function configure()
{
$this->addArgument(
self::INPUT_OPTION_PATHS,
InputArgument::IS_ARRAY,
'Space-separated list of configuration paths to whitelist'
);
$this->setName(self::COMMAND_NAME)
->setDescription('Add configuration paths to export whitelist');
parent::configure();
}
/**
* Add configuration paths to whitelist
*
* @param InputInterface $input
* @param OutputInterface $output
* @return int
*/
protected function execute(InputInterface $input, OutputInterface $output)
{
$paths = $input->getArgument(self::INPUT_OPTION_PATHS);
if (empty($paths)) {
$output->writeln('<error>No configuration path provided</error>');
return Cli::RETURN_FAILURE;
}
try {
$whitelist = $this->preparePathToSave($paths);
$this->writer->saveConfig($whitelist, true);
} catch (\Throwable $e) {
$output->writeln(sprintf('<error>No configuration path provided: %s</error>', $e->getMessage()));
return Cli::RETURN_FAILURE;
}
$output->writeln('<info>Paths successfully added to configuration whitelist</info>');
return Cli::RETURN_SUCCESS;
}
/**
* Prepare whitelist path to save in deployment config file.
*
* @param array $paths
* @return \array[][][]
* @throws \Magento\Framework\Exception\FileSystemException
* @throws \Magento\Framework\Exception\RuntimeException
*/
private function preparePathToSave(array $paths): array
{
$whitelist = $this->deploymentConfig->get(EnvironmentProvider::WHITELIST_CONFIG_KEY, []);
$paths = array_unique($paths);
$paths = array_filter($paths, function ($path) use ($whitelist) {
return !in_array($path, $whitelist);
});
return [
ConfigFilePool::APP_ENV => [
'commerce-data-export' => [
'configuration' => [
'path-whitelist' => array_merge($whitelist, $paths)
]
]
]
];
}
}