-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
ExampleCommands.php
69 lines (62 loc) · 2.26 KB
/
ExampleCommands.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
<?php
namespace Drush\Commands;
use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
use Consolidation\OutputFormatters\Options\FormatterOptions;
use Consolidation\AnnotatedCommand\CommandData;
class ExampleCommands extends DrushCommands
{
/**
* Demonstrate output formatters. Default format is 'table'.
*
* @command example:table
* @field-labels
* first: I
* second: II
* third: III
* @default-string-field second
* @usage example-table --format=yaml
* @usage example-table --format=csv
* @usage example-table --fields=first,third
* @usage example-table --fields=III,II
* @aliases tf
* @hidden
*/
public function exampleTable($options = ['format' => 'table']): RowsOfFields
{
$tableData = [
'en' => [ 'first' => 'One', 'second' => 'Two', 'third' => 'Three' ],
'de' => [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei' ],
'jp' => [ 'first' => 'Ichi', 'second' => 'Ni', 'third' => 'San' ],
'es' => [ 'first' => 'Uno', 'second' => 'Dos', 'third' => 'Tres' ],
];
$data = new RowsOfFields($tableData);
// Add a render function to transform cell data when the output
// format is a table, or similar. This allows us to add color
// information to the output without modifying the data cells when
// using yaml or json output formats.
$data->addRendererFunction(
// n.b. There is a fourth parameter $rowData that may be added here.
function ($key, $cellData, FormatterOptions $options, $rowData) {
if ($key == 'first') {
return "<comment>$cellData</>";
}
return $cellData;
}
);
return $data;
}
/**
* Demonstrate an alter hook with an option
*
* @hook alter example-table
* @option french Add a row with French numbers.
* @usage example-table --french
*/
public function alterFormatters($result, CommandData $commandData)
{
if ($commandData->input()->getOption('french')) {
$result['fr'] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ];
}
return $result;
}
}