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-output |
38
|
|
|
* @return array |
39
|
|
|
*/ |
40
|
|
|
public function edit($data, $options = ['format' => 'yaml', 'in' => 'auto']) |
41
|
|
|
{ |
42
|
|
|
return $this->read($data, $options['in']); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @hook alter @filter-output |
47
|
|
|
* @option $filter Filter output based on provided expression |
48
|
|
|
* @default $filter '' |
49
|
|
|
*/ |
50
|
|
|
public function filterOutput($result, CommandData $commandData) |
51
|
|
|
{ |
52
|
|
|
$expr = $commandData->input()->getOption('filter'); |
53
|
|
|
if (!empty($expr)) { |
54
|
|
|
$factory = LogicalOpFactory::get(); |
55
|
|
|
$op = $factory->evaluate($expr); |
56
|
|
|
$filter = new FilterOutputData(); |
57
|
|
|
$result = $filter->filter($result, $op); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return $result; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Read the data provided to this command. |
65
|
|
|
*/ |
66
|
|
|
protected function read($data, $in) |
67
|
|
|
{ |
68
|
|
|
// If our input spec is '-' then read from stdin |
69
|
|
|
if ($data == '-') { |
70
|
|
|
$data = 'php://stdin'; |
71
|
|
|
} |
72
|
|
|
// If our input spec is a file or a url then read it. Otherwise |
73
|
|
|
// we'll presume the data was provided directly on the command line. |
74
|
|
|
if (file_exists($data) || preg_match('#^[a-z]*://#', $data)) { |
75
|
|
|
$data = file_get_contents($data); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return $this->parseData($data, $in); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Convert our provided input data to a php array. |
83
|
|
|
*/ |
84
|
|
|
protected function parseData($data, $in) |
85
|
|
|
{ |
86
|
|
|
$in = $this->inferInputFormat($data, $in); |
87
|
|
|
|
88
|
|
|
switch ($in) { |
89
|
|
|
case 'json': |
90
|
|
|
return json_decode($data, true); |
91
|
|
|
|
92
|
|
|
case 'yaml': |
93
|
|
|
default: |
94
|
|
|
return (array) Yaml::parse($data); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* If the data type is 'auto', then try to infer what data type |
100
|
|
|
* we should parse as based on the contents of the data. |
101
|
|
|
*/ |
102
|
|
|
protected function inferInputFormat($data, $in) |
103
|
|
|
{ |
104
|
|
|
// If the user explicitly set the data type then use that |
105
|
|
|
if ($in != 'auto') { |
106
|
|
|
return $in; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
// If data begins with '{' then presume it is json |
110
|
|
|
if ($data[0] == '{') { |
111
|
|
|
return 'json'; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// We don't know what the data type should be. |
115
|
|
|
return $in; |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|