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;
14 * Test file used in the testCommandDiscovery() test.
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
20 class AlphaCommandFile implements CustomEventAwareInterface
22 use CustomEventAwareTrait;
25 * @command always:fail
27 public function alwaysFail()
29 return new CommandError('This command always fails.', 13);
33 * @command simulated:status
35 public function simulatedStatus()
37 return ['status-code' => 42];
41 * @command example:output
43 public function exampleOutput()
45 return 'Hello, World.';
49 * @command example:cat
51 public function exampleCat($one, $two = '', $options = ['flip' => false])
53 if ($options['flip']) {
54 return "{$two}{$one}";
56 return "{$one}{$two}";
60 * @command example:echo
62 public function exampleEcho(array $args)
64 return ['item-list' => $args];
68 * @command example:message
70 public function exampleMessage()
72 return ['message' => 'Shipwrecked; send bananas.'];
76 * Test command with formatters
78 * @command example:table
79 * @param $unused An unused argument
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.
92 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
94 public function exampleTable($unused = '', $options = ['format' => 'table', 'fields' => ''])
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' ],
102 return new RowsOfFields($outputData);
108 * @command example:wrap
113 * @return \Consolidation\OutputFormatters\StructuredData\RowsOfFields
115 public function exampleWrap()
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.',
123 return new RowsOfFields($data);
127 * @hook option example:table
129 public function additionalOptionForExampleTable($command, $annotationData)
134 InputOption::VALUE_NONE,
135 'Option added by @hook option example:table'
140 * Demonstrate an alter hook with an option
142 * @hook alter example:table
143 * @option french Add a row with French numbers.
144 * @usage example:table --french
146 public function alterFormatters($result, CommandData $commandData)
148 if ($commandData->input()->getOption('french')) {
149 $result[] = [ 'first' => 'Un', 'second' => 'Deux', 'third' => 'Trois' ];
156 * Test command with formatters using an associative list
158 * @command example:list
160 * sftp_command: SFTP Command
161 * sftp_username: SFTP Username
162 * sftp_host: SFTP Host
163 * sftp_password: SFTP Password
165 * git_command: Git Command
166 * git_username: Git Username
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
184 public function exampleAssociativeList()
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',
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',
205 return new AssociativeList($outputData);
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.
213 public function withoutAnnotations()
219 * @command command:with-one-optional-argument
221 * This command has just one optional argument.
223 * Return a result only if not silent.
225 * @option silent Supress output.
227 public function commandWithOneOptionalArgument($who = 'world', $opts = ['silent|s' => false])
229 if (!$opts['silent']) {
230 return "Hello, $who";
235 * This should be a command, because it is annotated like one.
237 * @command get:serious
239 public function getSerious()
241 return 'very serious';
245 * This should not be a command, because it looks like an accessor and
246 * has no @command annotation.
248 public function getLost()
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).
261 public function useEvent()
263 $myEventHandlers = $this->getCustomEventHandlers('my-event');
265 foreach ($myEventHandlers as $handler) {
266 $result[] = $handler();
269 return implode(',', $result);
273 * @hook on-event my-event
275 public function hookOne()
281 * @hook on-event my-event
283 public function hookTwo()