forked from contao/contao
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SetDotEnvCommand.php
63 lines (50 loc) · 1.58 KB
/
SetDotEnvCommand.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
<?php
declare(strict_types=1);
/*
* This file is part of Contao.
*
* (c) Leo Feyer
*
* @license LGPL-3.0-or-later
*/
namespace Contao\ManagerBundle\ContaoManager\ApiCommand;
use Contao\ManagerBundle\Api\Application;
use Contao\ManagerBundle\Dotenv\DotenvDumper;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Filesystem\Path;
/**
* @internal
*/
class SetDotEnvCommand extends Command
{
protected static $defaultName = 'dot-env:set';
protected static $defaultDescription = 'Writes a parameter to the .env file.';
private string $projectDir;
public function __construct(Application $application)
{
parent::__construct();
$this->projectDir = $application->getProjectDir();
}
protected function configure(): void
{
$this
->addArgument('key', InputArgument::REQUIRED, 'The variable name')
->addArgument('value', InputArgument::REQUIRED, 'The new value')
;
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$path = Path::join($this->projectDir, '.env');
$dumper = new DotenvDumper($path.'.local');
$dumper->setParameter($input->getArgument('key'), $input->getArgument('value'));
$dumper->dump();
if (!file_exists($path)) {
(new Filesystem())->touch($path);
}
return 0;
}
}