1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Karma\Command; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
use Symfony\Component\Console\Input\InputOption; |
8
|
|
|
use Karma\Configuration; |
9
|
|
|
use Karma\Command; |
10
|
|
|
use Karma\Configuration\ValueFilterIterator; |
11
|
|
|
use Karma\Hydrator; |
12
|
|
|
|
13
|
|
|
class GenerateSecuredSkeleton extends Command |
14
|
|
|
{ |
15
|
|
|
const |
16
|
|
|
ENV_PROD= 'prod'; |
|
|
|
|
17
|
|
|
|
18
|
|
|
protected function configure() |
19
|
|
|
{ |
20
|
|
|
parent::configure(); |
21
|
|
|
|
22
|
|
|
$this |
23
|
|
|
->setName('generate:secure:skeleton') |
24
|
|
|
->setDescription('Generate the secured.conf file') |
25
|
|
|
|
26
|
|
|
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_PROD) |
27
|
|
|
->addOption('force', 'f', InputOption::VALUE_NONE, 'Overwrite existing file if present') |
28
|
|
|
->addOption('output', 'o', InputOption::VALUE_OPTIONAL, 'Target path for generated secured.conf file') |
29
|
|
|
; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
33
|
|
|
{ |
34
|
|
|
parent::execute($input, $output); |
35
|
|
|
|
36
|
|
|
$environment = $input->getOption('env'); |
37
|
|
|
$outputPath = $input->getOption('output'); |
38
|
|
|
$overwrite = $input->getOption('force'); |
39
|
|
|
|
40
|
|
|
$this->output->writeln(sprintf( |
41
|
|
|
"<info>Generate secured variables for <comment>%s</comment> environment</info>\n", |
42
|
|
|
$environment |
43
|
|
|
)); |
44
|
|
|
|
45
|
|
|
$reader = $this->app['configuration']; |
46
|
|
|
$reader->setDefaultEnvironment($input->getOption('env')); |
47
|
|
|
|
48
|
|
|
$securedVariables = $this->generateSecuredVariables($reader); |
49
|
|
|
|
50
|
|
|
if(!empty($outputPath)) |
51
|
|
|
{ |
52
|
|
|
$this->app['configuration.fileSystem']->write($outputPath, $securedVariables, $overwrite); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
$this->output->writeln(''); |
56
|
|
|
$this->output->writeln('***'); |
57
|
|
|
|
58
|
|
|
$this->output->writeln($securedVariables); |
59
|
|
|
|
60
|
|
|
$this->output->writeln('***'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
private function generateSecuredVariables(Configuration $reader) |
64
|
|
|
{ |
65
|
|
|
$values = new ValueFilterIterator( |
66
|
|
|
Configuration::NOT_FOUND, |
67
|
|
|
new \ArrayIterator($reader->getAllValuesForEnvironment()) |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
$values->ksort(); |
|
|
|
|
71
|
|
|
|
72
|
|
|
$securedConf = "[variables]"; |
73
|
|
|
|
74
|
|
|
foreach($values as $variable => $value) |
75
|
|
|
{ |
76
|
|
|
$variablePattern = <<<PATTERN |
77
|
|
|
|
78
|
|
|
%s: |
79
|
|
|
%s = %s |
80
|
|
|
PATTERN; |
81
|
|
|
|
82
|
|
|
$securedConf .= sprintf( |
83
|
|
|
$variablePattern, |
84
|
|
|
$variable, |
85
|
|
|
$reader->getDefaultEnvironment(), |
86
|
|
|
Hydrator::TODO_VALUE |
87
|
|
|
); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $securedConf; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
This check looks for improperly formatted assignments.
Every assignment must have exactly one space before and one space after the equals operator.
To illustrate:
will have no issues, while
will report issues in lines 1 and 2.