b5d06eca63717d4f0b3b4cf85e109472a16e240b
[yaffs-website] / vendor / consolidation / annotated-command / tests / src / alpha / AlphaCommandFile.php
1 <?php
2 namespace Consolidation\TestUtils\alpha;
3
4 use Consolidation\AnnotatedCommand\CommandError;
5 use Consolidation\OutputFormatters\StructuredData\RowsOfFields;
6 use Consolidation\OutputFormatters\StructuredData\AssociativeList;
7 use Consolidation\AnnotatedCommand\AnnotationData;
8 use Symfony\Component\Console\Input\InputOption;
9 use Consolidation\AnnotatedCommand\CommandData;
10 use Consolidation\AnnotatedCommand\Events\CustomEventAwareInterface;
11 use Consolidation\AnnotatedCommand\Events\CustomEventAwareTrait;
12
13 /**
14  * Test file used in the testCommandDiscovery() test.
15  *
16  * This commandfile is found by the test.  The test search base is the
17  * 'src' directory, and 'alpha' is one of the search directories available
18  * for searching.
19  */
20 class AlphaCommandFile implements CustomEventAwareInterface
21 {
22     use CustomEventAwareTrait;
23
24     /**
25      * @command always:fail
26      */
27     public function alwaysFail()
28     {
29         return new CommandError('This command always fails.', 13);
30     }
31
32     /**
33      * @command simulated:status
34      */
35     public function simulatedStatus()
36     {
37         return ['status-code' => 42];
38     }
39
40     /**
41      * @command example:output
42      */
43     public function exampleOutput()
44     {
45         return 'Hello, World.';
46     }
47
48     /**
49      * @command example:cat
50      */
51     public function exampleCat($one, $two = '', $options = ['flip' => false])
52     {
53         if ($options['flip']) {
54             return "{$two}{$one}";
55         }
56         return "{$one}{$two}";
57     }
58
59     /**
60      * @command example:echo
61      */
62     public function exampleEcho(array $args)
63     {
64         return ['item-list' => $args];
65     }
66
67     /**
68      * @command example:message
69      */
70     public function exampleMessage()
71     {
72         return ['message' => 'Shipwrecked; send bananas.'];
73     }
74
75     /**
76      * Test command with formatters
77      *
78      * @command example:table
79      * @param $unused An unused argument
80      * @field-labels
81      *   first: I
82      *   second: II
83      *   third: III
84      * @usage example:table --format=yml
85      *   Show the example table in yml format.
86      * @usage example:table --fields=first,third
87      *   Show only the first and third fields in the table.
88      * @usage example:table --fields=II,III
89      *   Note that either the field ID or the visible field label may be used.
90      * @aliases extab
91      * @topics docs-tables
92      * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
93      */
94     public function exampleTable($unused = '', $options = ['format' => 'table', 'fields' => ''])
95     {
96         $outputData = [
97             [ 'first' => 'One',  'second' => 'Two',  'third' => 'Three' ],
98             [ 'first' => 'Eins', 'second' => 'Zwei', 'third' => 'Drei'  ],
99             [ 'first' => 'Ichi', 'second' => 'Ni',   'third' => 'San'   ],
100             [ 'first' => 'Uno',  'second' => 'Dos',  'third' => 'Tres'  ],
101         ];
102         return new RowsOfFields($outputData);
103     }
104
105     /**
106      * Test word wrapping
107      *
108      * @command example:wrap
109      * @field-labels
110      *   first: First
111      *   second: Second
112      *
113      * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
114      */
115     public function exampleWrap()
116     {
117         $data = [
118             [
119                 'first' => 'This is a really long cell that contains a lot of data. When it is rendered, it should be wrapped across multiple lines.',
120                 'second' => 'This is the second column of the same table. It is also very long, and should be wrapped across multiple lines, just like the first column.',
121             ]
122         ];
123         return new RowsOfFields($data);
124     }
125
126     /**
127      * @hook option example:table
128      */
129     public function additionalOptionForExampleTable($command, $annotationData)
130     {
131         $command->addOption(
132             'dynamic',
133             '',
134             InputOption::VALUE_NONE,
135             'Option added by @hook option example:table'
136         );
137     }
138
139     /**
140      * Demonstrate an alter hook with an option
141      *
142      * @hook alter example:table
143      * @option french Add a row with French numbers.
144      * @usage example:table --french
145      */
146     public function alterFormatters($result, CommandData $commandData)
147     {
148         if ($commandData->input()->getOption('french')) {
149             $result[] = [ 'first' => 'Un',  'second' => 'Deux',  'third' => 'Trois'  ];
150         }
151
152         return $result;
153     }
154
155     /**
156      * Test command with formatters using an associative list
157      *
158      * @command example:list
159      * @field-labels
160      *   sftp_command: SFTP Command
161      *   sftp_username: SFTP Username
162      *   sftp_host: SFTP Host
163      *   sftp_password: SFTP Password
164      *   sftp_url: SFTP URL
165      *   git_command: Git Command
166      *   git_username: Git Username
167      *   git_host: Git Host
168      *   git_port: Git Port
169      *   git_url: Git URL
170      *   mysql_command: MySQL Command
171      *   mysql_username: MySQL Username
172      *   mysql_host: MySQL Host
173      *   mysql_password: MySQL Password
174      *   mysql_url: MySQL URL
175      *   mysql_port: MySQL Port
176      *   mysql_database: MySQL Database
177      *   redis_command: Redis Command
178      *   redis_port: Redis Port
179      *   redis_url: Redis URL
180      *   redis_password: Redis Password
181      * @default-fields *_command
182      * @return \Consolidation\OutputFormatters\StructuredData\AssociativeList
183      */
184     public function exampleAssociativeList()
185     {
186         $outputData = [
187             'sftp_command' => 'sftp -o Port=2222 dev@appserver.dev.drush.in',
188             'sftp_username' => 'dev',
189             'sftp_host' => 'appserver.dev.drush.in',
190             'sftp_password' => 'Use your account password',
191             'sftp_url' => 'sftp://dev@appserver.dev.drush.in:2222',
192             'git_command' => 'git clone ssh://codeserver.dev@codeserver.dev.drush.in:2222/~/repository.git wp-update',
193             'git_username' => 'codeserver.dev',
194             'git_host' => 'codeserver.dev.drush.in',
195             'git_port' => 2222,
196             'git_url' => 'ssh://codeserver.dev@codeserver.dev.drush.in:2222/~/repository.git',
197             'mysql_command' => 'mysql -u pantheon -p4b33cb -h dbserver.dev.drush.in -P 16191 pantheon',
198             'mysql_username' => 'pantheon',
199             'mysql_host' => 'dbserver.dev.drush.in',
200             'mysql_password' => '4b33cb',
201             'mysql_url' => 'mysql://pantheon:4b33cb@dbserver.dev.drush.in:16191/pantheon',
202             'mysql_port' => 16191,
203             'mysql_database' => 'pantheon',
204         ];
205         return new AssociativeList($outputData);
206     }
207
208     /**
209      * This command has no annotations; this means that it will not be
210      * found when createCommandsFromClass() is called with
211      * '$includeAllPublicMethods' set to false.
212      */
213     public function withoutAnnotations()
214     {
215         return 'ok';
216     }
217
218     /**
219      * @command command:with-one-optional-argument
220      *
221      * This command has just one optional argument.
222      *
223      * Return a result only if not silent.
224      *
225      * @option silent Supress output.
226      */
227     public function commandWithOneOptionalArgument($who = 'world', $opts = ['silent|s' => false])
228     {
229         if (!$opts['silent']) {
230             return "Hello, $who";
231         }
232     }
233
234     /**
235      * This should be a command, because it is annotated like one.
236      *
237      * @command get:serious
238      */
239     public function getSerious()
240     {
241         return 'very serious';
242     }
243
244     /**
245      * This should not be a command, because it looks like an accessor and
246      * has no @command annotation.
247      */
248     public function getLost()
249     {
250         return 'very lost';
251     }
252
253     /**
254      * This command uses a custom event 'my-event' to collect data.  Note that
255      * the event handlers will not be found unless the hook manager is
256      * injected into this command handler object via `setHookManager()`
257      * (defined in CustomEventAwareTrait).
258      *
259      * @command use:event
260      */
261     public function useEvent()
262     {
263         $myEventHandlers = $this->getCustomEventHandlers('my-event');
264         $result = [];
265         foreach ($myEventHandlers as $handler) {
266             $result[] = $handler();
267         }
268         sort($result);
269         return implode(',', $result);
270     }
271
272     /**
273      * @hook on-event my-event
274      */
275     public function hookOne()
276     {
277         return 'one';
278     }
279
280     /**
281      * @hook on-event my-event
282      */
283     public function hookTwo()
284     {
285         return 'two';
286     }
287 }