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