-
Notifications
You must be signed in to change notification settings - Fork 132
/
Copy pathSetupEnvCommand.php
70 lines (63 loc) · 2.21 KB
/
SetupEnvCommand.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
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types = 1);
namespace Magento\FunctionalTestingFramework\Console;
use Magento\FunctionalTestingFramework\Exceptions\TestFrameworkException;
use Magento\FunctionalTestingFramework\Util\Path\FilePathFormatter;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Exception\InvalidOptionException;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Magento\FunctionalTestingFramework\Util\Env\EnvProcessor;
class SetupEnvCommand extends Command
{
private const SUCCESS_EXIT_CODE = 0;
/**
* Env processor manages .env files.
*
* @var \Magento\FunctionalTestingFramework\Util\Env\EnvProcessor
*/
private $envProcessor;
/**
* Configures the current command.
*
* @return void
* @throws TestFrameworkException
*/
protected function configure()
{
$this->setName('setup:env')
->setDescription("Generate .env file.");
$this->envProcessor = new EnvProcessor(FilePathFormatter::format(TESTS_BP) . '.env');
$env = $this->envProcessor->getEnv();
foreach ($env as $key => $value) {
$this->addOption($key, null, InputOption::VALUE_REQUIRED, '', $value);
}
}
/**
* Executes the current command.
*
* @param InputInterface $input
* @param OutputInterface $output
* @return integer
* @throws \Symfony\Component\Console\Exception\LogicException
*/
protected function execute(InputInterface $input, OutputInterface $output): int
{
$config = $this->envProcessor->getEnv();
$userEnv = [];
foreach ($config as $key => $value) {
if ($input->getOption($key) === '') {
throw new InvalidOptionException(sprintf("Parameter $key cannot be empty.", $key));
}
$userEnv[$key] = $input->getOption($key);
}
$this->envProcessor->putEnvFile($userEnv);
$output->writeln(".env configuration successfully applied.");
return self::SUCCESS_EXIT_CODE;
}
}