2 namespace Consolidation\TestUtils\alpha;
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;
14 use Consolidation\TestUtils\ExampleCommandFile as ExampleAliasedClass;
17 * Test file used in the testCommandDiscovery() test.
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
23 class AlphaCommandFile implements CustomEventAwareInterface
25 use CustomEventAwareTrait;
28 * @command always:fail
30 public function alwaysFail()
32 return new CommandError('This command always fails.', 13);
35 public static function ignoredStaticMethod()
37 return 'This method is static; it should not generate a command.';
41 * @command simulated:status
43 public function simulatedStatus()
45 return ['status-code' => 42];
49 * @command example:output
51 public function exampleOutput()
53 return 'Hello, World.';
57 * @command example:cat
59 public function exampleCat($one, $two = '', $options = ['flip' => false])
61 if ($options['flip']) {
62 return "{$two}{$one}";
64 return "{$one}{$two}";
68 * @command example:echo
70 public function exampleEcho(array $args)
72 return ['item-list' => $args];
76 * @command example:message
78 public function exampleMessage()
80 return ['message' => 'Shipwrecked; send bananas.'];
84 * Test command with formatters
86 * @command example:table
87 * @param $unused An unused argument
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.
100 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields Fully-qualified class name
102 public function exampleTable($unused = '', $options = ['format' => 'table', 'fields' => ''])
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' ],
110 return new RowsOfFields($outputData);
114 * Test command with formatters using a short classname in @return
116 * @command example:table2
117 * @param $unused An unused argument
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.
129 * @topics docs-tables
130 * @return RowsOfFields Short class names are converted to fqcns
132 public function exampleTableTwo($unused = '', $options = ['format' => 'table', 'fields' => ''])
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' ],
140 return new RowsOfFields($outputData);
146 * @command example:wrap
151 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
153 public function exampleWrap()
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.',
161 return new RowsOfFields($data);
165 * @hook option example:table
167 public function additionalOptionForExampleTable(Command $command, AnnotationData $annotationData)
172 InputOption::VALUE_NONE,
173 'Option added by @hook option example:table'
178 * Demonstrate an alter hook with an option
180 * @hook alter example:table
181 * @option french Add a row with French numbers.
182 * @usage example:table --french
184 public function alterFormatters($result, CommandData $commandData)
186 if ($commandData->input()->getOption('french')) {
187 $result[] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ];
194 * Test command with formatters using an associative list
196 * @command example:list
198 * sftp_command: SFTP Command
199 * sftp_username: SFTP Username
200 * sftp_host: SFTP Host
201 * sftp_password: SFTP Password
203 * git_command: Git Command
204 * git_username: Git Username
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
222 public function exampleAssociativeList()
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',
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',
243 return new AssociativeList($outputData);
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.
251 public function withoutAnnotations()
257 * @command command:with-one-optional-argument
259 * This command has just one optional argument.
261 * Return a result only if not silent.
263 * @option silent Supress output.
265 public function commandWithOneOptionalArgument($who = 'world', $opts = ['silent|s' => false])
267 if (!$opts['silent']) {
268 return "Hello, $who";
273 * This should be a command, because it is annotated like one.
275 * @command get:serious
277 public function getSerious()
279 return 'very serious';
283 * This should not be a command, because it looks like an accessor and
284 * has no @command annotation.
286 public function getLost()
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).
299 public function useEvent()
301 $myEventHandlers = $this->getCustomEventHandlers('my-event');
303 foreach ($myEventHandlers as $handler) {
304 $result[] = $handler();
307 return implode(',', $result);
311 * @hook on-event my-event
313 public function hookOne()
319 * @hook on-event my-event
321 public function hookTwo()