|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Consolidation\Filter\Cli; |
|
4
|
|
|
|
|
5
|
|
|
use Consolidation\AnnotatedCommand\CommandData; |
|
6
|
|
|
use Consolidation\Filter\LogicalOpFactory; |
|
7
|
|
|
use Consolidation\Filter\FilterOutputData; |
|
8
|
|
|
use Symfony\Component\Yaml\Yaml; |
|
9
|
|
|
|
|
10
|
|
|
class OpCommands extends \Robo\Tasks |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* Test the expression parser |
|
14
|
|
|
* |
|
15
|
|
|
* @command parse |
|
16
|
|
|
* @return array |
|
17
|
|
|
*/ |
|
18
|
|
|
public function parse($expr, $options = ['format' => 'yaml', 'dump' => false]) |
|
19
|
|
|
{ |
|
20
|
|
|
$factory = LogicalOpFactory::get(); |
|
21
|
|
|
$op = $factory->evaluate($expr); |
|
22
|
|
|
|
|
23
|
|
|
$result = (string)$op; |
|
24
|
|
|
|
|
25
|
|
|
if ($options['dump']) { |
|
26
|
|
|
$result = var_export($op, true) . "\n$result"; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
return $result; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Convert a command from one format to another, potentially with filtering. |
|
34
|
|
|
* |
|
35
|
|
|
* @command edit |
|
36
|
|
|
* @aliases ed |
|
37
|
|
|
* @filter-default-field color |
|
38
|
|
|
* @filter-output |
|
39
|
|
|
* @return array |
|
40
|
|
|
*/ |
|
41
|
|
|
public function edit($data, $options = ['format' => 'yaml', 'in' => 'auto']) |
|
42
|
|
|
{ |
|
43
|
|
|
return $this->read($data, $options['in']); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Read the data provided to this command. |
|
48
|
|
|
*/ |
|
49
|
|
|
protected function read($data, $in) |
|
50
|
|
|
{ |
|
51
|
|
|
// If our input spec is '-' then read from stdin |
|
52
|
|
|
if ($data == '-') { |
|
53
|
|
|
$data = 'php://stdin'; |
|
54
|
|
|
} |
|
55
|
|
|
// If our input spec is a file or a url then read it. Otherwise |
|
56
|
|
|
// we'll presume the data was provided directly on the command line. |
|
57
|
|
|
if (file_exists($data) || preg_match('#^[a-z]*://#', $data)) { |
|
58
|
|
|
$data = file_get_contents($data); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
return $this->parseData($data, $in); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* Convert our provided input data to a php array. |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function parseData($data, $in) |
|
68
|
|
|
{ |
|
69
|
|
|
$in = $this->inferInputFormat($data, $in); |
|
70
|
|
|
|
|
71
|
|
|
switch ($in) { |
|
72
|
|
|
case 'json': |
|
73
|
|
|
return json_decode($data, true); |
|
74
|
|
|
|
|
75
|
|
|
case 'yaml': |
|
76
|
|
|
default: |
|
77
|
|
|
return (array) Yaml::parse($data); |
|
78
|
|
|
} |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* If the data type is 'auto', then try to infer what data type |
|
83
|
|
|
* we should parse as based on the contents of the data. |
|
84
|
|
|
*/ |
|
85
|
|
|
protected function inferInputFormat($data, $in) |
|
86
|
|
|
{ |
|
87
|
|
|
// If the user explicitly set the data type then use that |
|
88
|
|
|
if ($in != 'auto') { |
|
89
|
|
|
return $in; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
// If data begins with '{' then presume it is json |
|
93
|
|
|
if ($data[0] == '{') { |
|
94
|
|
|
return 'json'; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// We don't know what the data type should be. |
|
98
|
|
|
return $in; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|