|
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
|
|
|
|
|
12
|
|
|
class Display extends Command |
|
13
|
|
|
{ |
|
14
|
|
|
const |
|
15
|
|
|
ENV_DEV = 'dev', |
|
16
|
|
|
NO_FILTERING = 'karma-nofiltering'; |
|
17
|
|
|
|
|
18
|
39 |
|
protected function configure() |
|
19
|
|
|
{ |
|
20
|
39 |
|
parent::configure(); |
|
21
|
|
|
|
|
22
|
39 |
|
$this |
|
23
|
39 |
|
->setName('display') |
|
24
|
39 |
|
->setDescription('Display environment variable set') |
|
25
|
|
|
|
|
26
|
39 |
|
->addOption('env', 'e', InputOption::VALUE_REQUIRED, 'Target environment', self::ENV_DEV) |
|
27
|
39 |
|
->addOption('value', 'f', InputOption::VALUE_REQUIRED, 'Display only variable with this value', self::NO_FILTERING) |
|
28
|
39 |
|
->addOption('json', null, InputOption::VALUE_NONE, 'Display output in json format') |
|
29
|
|
|
; |
|
30
|
39 |
|
} |
|
31
|
|
|
|
|
32
|
4 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
|
33
|
|
|
{ |
|
34
|
4 |
|
$json = $input->getOption('json'); |
|
35
|
|
|
|
|
36
|
4 |
|
if($json) |
|
37
|
4 |
|
{ |
|
38
|
|
|
$output->setVerbosity(OutputInterface::VERBOSITY_QUIET); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
parent::execute($input, $output); |
|
42
|
|
|
|
|
43
|
4 |
|
$environment = $input->getOption('env'); |
|
44
|
|
|
|
|
45
|
4 |
|
$this->output->writeln(sprintf( |
|
46
|
4 |
|
"<info>Display <comment>%s</comment> values</info>\n", |
|
47
|
|
|
$environment |
|
48
|
4 |
|
)); |
|
49
|
|
|
|
|
50
|
4 |
|
$reader = $this->app['configuration']; |
|
51
|
4 |
|
$reader->setDefaultEnvironment($input->getOption('env')); |
|
52
|
|
|
|
|
53
|
4 |
|
$this->displayValues($reader, $input->getOption('value'), $json); |
|
54
|
4 |
|
} |
|
55
|
|
|
|
|
56
|
4 |
|
private function displayValues(Configuration $reader, $filter = self::NO_FILTERING, $json = false) |
|
57
|
4 |
|
{ |
|
58
|
4 |
|
$values = new \ArrayIterator($reader->getAllValuesForEnvironment()); |
|
59
|
|
|
|
|
60
|
4 |
|
if($filter !== self::NO_FILTERING) |
|
61
|
4 |
|
{ |
|
62
|
1 |
|
$values = new ValueFilterIterator($filter, $values); |
|
63
|
1 |
|
} |
|
64
|
|
|
|
|
65
|
4 |
|
$this->output->writeln(''); |
|
66
|
|
|
|
|
67
|
4 |
|
$values->ksort(); |
|
|
|
|
|
|
68
|
|
|
|
|
69
|
|
|
if($json) |
|
70
|
4 |
|
{ |
|
71
|
|
|
$this->output->setVerbosity(OutputInterface::VERBOSITY_NORMAL); |
|
72
|
|
|
|
|
73
|
|
|
return $this->displayAsJson($values); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
4 |
|
return $this->displayAsCliOutput($values); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
4 |
|
private function displayAsCliOutput(\Iterator $values) |
|
80
|
|
|
{ |
|
81
|
4 |
|
foreach($values as $variable => $value) |
|
82
|
|
|
{ |
|
83
|
4 |
|
$this->output->writeln(sprintf( |
|
84
|
4 |
|
'<fg=cyan>%s</fg=cyan> = %s', |
|
85
|
4 |
|
$variable, |
|
86
|
4 |
|
$this->formatValue($value) |
|
87
|
4 |
|
)); |
|
88
|
4 |
|
} |
|
89
|
4 |
|
} |
|
90
|
|
|
|
|
91
|
|
|
private function displayAsJson(\Iterator $values) |
|
92
|
|
|
{ |
|
93
|
|
|
$this->output->writeln( |
|
94
|
|
|
json_encode(iterator_to_array($values)) |
|
95
|
|
|
); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: