1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author Gerard van Helden <[email protected]> |
4
|
|
|
* @copyright Zicht Online <http://zicht.nl> |
5
|
|
|
*/ |
6
|
|
|
namespace Zicht\Tool\Command; |
7
|
|
|
|
8
|
|
|
use Symfony\Component\Console\Input; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Command to evaluate an expression |
14
|
|
|
*/ |
15
|
|
|
class EvalCommand extends BaseCommand |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @{inheritDoc} |
19
|
|
|
*/ |
20
|
1 |
|
protected function configure() |
21
|
|
|
{ |
22
|
1 |
|
$this |
23
|
1 |
|
->setName('z:eval') |
24
|
1 |
|
->addArgument('expression', Input\InputArgument::REQUIRED, 'The expression to evaluate') |
25
|
1 |
|
->addOption('format', '', Input\InputOption::VALUE_REQUIRED, 'Format to output', 'yml') |
26
|
1 |
|
->setHelp('Available output formats are \'json\', \'yml\' or \'php\'') |
27
|
1 |
|
->setDescription('Evaluates an expression within the scope of the container.') |
28
|
|
|
; |
29
|
1 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @{inheritDoc} |
33
|
|
|
*/ |
34
|
|
|
protected function execute(Input\InputInterface $input, OutputInterface $output) |
35
|
|
|
{ |
36
|
|
|
$expr = $input->getArgument('expression'); |
37
|
|
|
$result = $this->getContainer()->evaluate($expr, $code); |
38
|
|
|
|
39
|
|
|
if ($output->getVerbosity() > OutputInterface::VERBOSITY_NORMAL) { |
40
|
|
|
$output->writeln($code); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
switch ($input->getOption('format')) { |
44
|
|
|
case 'json': |
45
|
|
|
$output->writeln(json_encode($result)); |
46
|
|
|
break; |
47
|
|
|
case 'yml': |
48
|
|
|
case 'yaml': |
49
|
|
|
$output->writeln(Yaml::dump($result)); |
50
|
|
|
break; |
51
|
|
|
case 'php': |
52
|
|
|
$output->writeln(var_export($result, true)); |
53
|
|
|
break; |
54
|
|
|
default: |
55
|
|
|
throw new \InvalidArgumentException("Unsupported output format {$input->getOption('format')}"); |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
|