1a9afef5bfc1ae47fa60a3634ff71fb3b6f8ac80
[yaffs-website] / vendor / consolidation / annotated-command / tests / src / ExampleCommandFile.php
1 <?php
2 namespace Consolidation\TestUtils;
3
4 use Consolidation\AnnotatedCommand\CommandData;
5 use Consolidation\AnnotatedCommand\AnnotationData;
6 use Consolidation\AnnotatedCommand\CommandError;
7 use Symfony\Component\Console\Command\Command;
8 use Symfony\Component\Console\Event\ConsoleCommandEvent;
9 use Symfony\Component\Console\Input\InputInterface;
10 use Symfony\Component\Console\Input\InputOption;
11 use Symfony\Component\Console\Output\OutputInterface;
12
13 /**
14  * Test file used in the Annotation Factory tests.  It is also
15  * discovered in the testCommandDiscovery() test.
16  *
17  * The testCommandDiscovery test search base is the 'src' directory;
18  * any command files located immediately inside the search base are
19  * eligible for discovery, and will be included in the search results.
20  */
21 class ExampleCommandFile
22 {
23     protected $state;
24     protected $output;
25
26     public function __construct($state = '')
27     {
28         $this->state = $state;
29     }
30
31     public function setOutput($output)
32     {
33         $this->output = $output;
34     }
35
36     /**
37      * Import config from a config directory.
38      *
39      * @command config:import
40      * @param $label A config directory label (i.e. a key in \$config_directories array in settings.php).
41      * @interact-config-label
42      * @option preview Format for displaying proposed changes. Recognized values: list, diff.
43      * @option source An arbitrary directory that holds the configuration files. An alternative to label argument
44      * @option partial Allows for partial config imports from the source directory. Only updates and new configs will be processed with this flag (missing configs will not be deleted).
45      * @aliases cim,config-import
46      */
47     public function import($label = null, $options = ['preview' => 'list', 'source' => InputOption::VALUE_REQUIRED, 'partial' => false])
48     {
49     }
50
51     /**
52      * Calculate the fibonacci sequence between two numbers.
53      *
54      * Graphic output will look like
55      *     +----+---+-------------+
56      *     |    |   |             |
57      *     |    |-+-|             |
58      *     |----+-+-+             |
59      *     |        |             |
60      *     |        |             |
61      *     |        |             |
62      *     +--------+-------------+
63      *
64      * @param int $start Number to start from
65      * @param int $steps Number of steps to perform
66      * @param array $opts
67      * @option $graphic Display the sequence graphically using cube
68      *   representation
69      */
70     public function fibonacci($start, $steps, $opts = ['graphic' => false])
71     {
72     }
73
74     /**
75      * Code sniffer.
76      *
77      * Run the PHP Codesniffer on a file or directory.
78      *
79      * @param string $file
80      *    A file or directory to analyze.
81      * @option $autofix Whether to run the automatic fixer or not.
82      * @option $strict Show warnings as well as errors.
83      *    Default is to show only errors.
84      */
85     public function sniff(
86         $file = 'src',
87         array $options = [
88             'autofix' => false,
89             'strict' => false,
90         ]
91     ) {
92         return var_export($options, true);
93     }
94
95     /**
96      * This is the my:cat command
97      *
98      * This command will concatenate two parameters. If the --flip flag
99      * is provided, then the result is the concatenation of two and one.
100      *
101      * @param string $one The first parameter.
102      * @param string $two The other parameter.
103      * @option boolean $flip Whether or not the second parameter should come first in the result.
104      * @aliases c
105      * @usage bet alpha --flip
106      *   Concatenate "alpha" and "bet".
107      * @arbitrary This annotation is here merely as a marker used in testing.
108      */
109     public function myCat($one, $two = '', array $options = ['flip' => false])
110     {
111         if ($options['flip']) {
112             return "{$two}{$one}";
113         }
114         return "{$one}{$two}";
115     }
116
117     /**
118      * @command my:repeat
119      */
120     public function myRepeat($one, $two = '', array $options = ['repeat' => 1])
121     {
122         return str_repeat("{$one}{$two}", $options['repeat']);
123     }
124
125     /**
126      * This is the my:join command
127      *
128      * This command will join its parameters together. It can also reverse and repeat its arguments.
129      *
130      * @command my:join
131      * @usage a b
132      *   Join a and b to produce "a,b"
133      * @usage
134      *   Example with no parameters or options
135      */
136     public function myJoin(array $args, array $options = ['flip' => false, 'repeat' => 1])
137     {
138         if ($options['flip']) {
139             $args = array_reverse($args);
140         }
141         $result = implode('', $args);
142         return str_repeat($result, $options['repeat']);
143     }
144
145     /**
146      * This is a command with no options
147      *
148      * This command will concatenate two parameters.
149      *
150      * @param $one The first parameter.
151      * @param $two The other parameter.
152      * @aliases nope
153      * @usage alpha bet
154      *   Concatenate "alpha" and "bet".
155      */
156     public function commandWithNoOptions($one, $two = 'default')
157     {
158         return "{$one}{$two}";
159     }
160
161     /**
162      * This command work with app's input and output
163      *
164      * @command command:with-io-parameters
165      */
166     public function commandWithIOParameters(InputInterface $input, OutputInterface $output)
167     {
168         return $input->getFirstArgument();
169     }
170
171     /**
172      * This command has no arguments--only options
173      *
174      * Return a result only if not silent.
175      *
176      * @option silent Supress output.
177      */
178     public function commandWithNoArguments(array $opts = ['silent|s' => false])
179     {
180         if (!$opts['silent']) {
181             return "Hello, world";
182         }
183     }
184
185     /**
186      * Shortcut on annotation
187      *
188      * This command defines the option shortcut on the annotation instead of in the options array.
189      *
190      * @param $opts The options
191      * @option silent|s Supress output.
192      */
193     public function shortcutOnAnnotation(array $opts = ['silent' => false])
194     {
195         if (!$opts['silent']) {
196             return "Hello, world";
197         }
198     }
199
200     /**
201      * This is the test:arithmatic command
202      *
203      * This command will add one and two. If the --negate flag
204      * is provided, then the result is negated.
205      *
206      * @command test:arithmatic
207      * @param integer $one The first number to add.
208      * @param integer $two The other number to add.
209      * @option negate Whether or not the result should be negated.
210      * @aliases arithmatic
211      * @usage 2 2 --negate
212      *   Add two plus two and then negate.
213      * @custom
214      * @dup one
215      * @dup two
216      */
217     public function testArithmatic($one, $two = 2, array $options = ['negate' => false, 'unused' => 'bob'])
218     {
219         $result = $one + $two;
220         if ($options['negate']) {
221             $result = -$result;
222         }
223
224         // Integer return codes are exit codes (errors), so
225         // return a the result as a string so that it will be printed.
226         return "$result";
227     }
228
229     /**
230      * This is the test:state command
231      *
232      * This command tests to see if the state of the Commandfile instance
233      */
234     public function testState()
235     {
236         return $this->state;
237     }
238
239     /**
240      * This is the test:passthrough command
241      *
242      * This command takes a variable number of parameters as
243      * an array and returns them as a csv.
244      */
245     public function testPassthrough(array $params)
246     {
247         return implode(',', $params);
248     }
249
250     /**
251      * This command wraps its parameter in []; its alter hook
252      * then wraps the result in <>.
253      */
254     public function testHook($parameter)
255     {
256         return "[$parameter]";
257     }
258
259     /**
260      * Wrap the results of test:hook in <>.
261      *
262      * @hook alter test:hook
263      */
264     public function hookTestHook($result)
265     {
266         return "<$result>";
267     }
268
269     /**
270      * This test is very similar to the preceding test, except
271      * it uses an annotation hook instead of a named-function hook.
272      *
273      * @hookme
274      * @before >
275      * @after <
276      */
277     public function testAnnotationHook($parameter)
278     {
279         return "($parameter)";
280     }
281
282     /**
283      * Wrap the results of test:hook in whatever the @before and @after
284      * annotations contain.
285      *
286      * @hook alter @hookme
287      */
288     public function hookTestAnnotatedHook($result, CommandData $commandData)
289     {
290         $before = $commandData->annotationData()->get('before', '-');
291         $after = $commandData->annotationData()->get('after', '-');
292         return "$before$result$after";
293     }
294
295     /**
296      * Alter the results of the hook with its command name.
297      *
298      * @hook alter @addmycommandname
299      */
300     public function hookAddCommandName($result, CommandData $commandData)
301     {
302         $annotationData = $commandData->annotationData();
303         return "$result from " . $annotationData['command'];
304     }
305
306     /**
307      * Here is a hook with an explicit command annotation that we will alter
308      * with the preceeding hook
309      *
310      * @command alter-me
311      * @addmycommandname
312      */
313     public function alterMe()
314     {
315         return "splendiferous";
316     }
317
318     /**
319      * Here is another hook that has no command annotation that should be
320      * altered with the default value for the command name
321      *
322      * @addmycommandname
323      */
324     public function alterMeToo()
325     {
326         return "fantabulous";
327     }
328
329     /**
330      * @command test:replace-command
331      */
332     public function testReplaceCommand($value)
333     {
334         $this->output->writeln($value);
335     }
336
337     /**
338      * @hook replace-command test:replace-command
339      */
340     public function hookTestReplaceCommandHook($value)
341     {
342         $this->output->writeln("bar");
343     }
344
345     /**
346      * @hook pre-command test:post-command
347      */
348     public function hookTestPreCommandHook(CommandData $commandData)
349     {
350         // Use 'writeln' to detect order that hooks are called
351         $this->output->writeln("foo");
352     }
353
354     /**
355      * @command test:post-command
356      */
357     public function testPostCommand($value)
358     {
359         $this->output->writeln($value);
360     }
361
362     /**
363      * @hook post-command test:post-command
364      */
365     public function hookTestPostCommandHook($result, CommandData $commandData)
366     {
367         // Use 'writeln' to detect order that hooks are called
368         $this->output->writeln("baz");
369     }
370
371     public function testHello($who)
372     {
373         return "Hello, $who.";
374     }
375
376     public function testException($what)
377     {
378         throw new \Exception($what);
379     }
380
381     /**
382      * @hook init test:hello
383      */
384     public function initializeTestHello($input, AnnotationData $annotationData)
385     {
386         $who = $input->getArgument('who');
387         if (!$who) {
388             $input->setArgument('who', 'Huey');
389         }
390     }
391
392     /**
393      * @hook command-event test:hello
394      */
395     public function commandEventTestHello(ConsoleCommandEvent $event)
396     {
397         // Note that Symfony Console will not allow us to alter the
398         // input from this hook, so we'll just print something to
399         // show that this hook was executed.
400         $input = $event->getInput();
401         $who = $input->getArgument('who');
402         $this->output->writeln("Here comes $who!");
403     }
404
405     /**
406      * @hook interact test:hello
407      */
408     public function interactTestHello($input, $output)
409     {
410         $who = $input->getArgument('who');
411         if (!$who) {
412             $input->setArgument('who', 'Goofey');
413         }
414     }
415
416     /**
417      * @hook validate test:hello
418      */
419     public function validateTestHello($commandData)
420     {
421         $args = $commandData->arguments();
422         if ($args['who'] == 'Donald Duck') {
423             return new CommandError("I won't say hello to Donald Duck.");
424         }
425         if ($args['who'] == 'Drumph') {
426             throw new \Exception('Irrational value error.');
427         }
428     }
429
430     /**
431      * Test default values in arguments
432      *
433      * @param string|null $one
434      * @param string|null $two
435      * @return string
436      */
437     public function defaults($one = null, $two = null)
438     {
439         if ($one && $two) {
440             return "$one and $two";
441         }
442         if ($one) {
443             return "only $one";
444         }
445         return "nothing provided";
446     }
447
448     /**
449      * @return string
450      */
451     public function defaultOptionOne(array $options = ['foo' => '1'])
452     {
453         return "Foo is " . $options['foo'];
454     }
455
456     /**
457      * @return string
458      */
459     public function defaultOptionTwo(array $options = ['foo' => '2'])
460     {
461         return "Foo is " . $options['foo'];
462     }
463
464     /**
465      * @return string
466      */
467     public function defaultOptionNone(array $options = ['foo' => InputOption::VALUE_REQUIRED])
468     {
469         return "Foo is " . $options['foo'];
470     }
471
472     /**
473      * @return string
474      */
475     public function defaultOptionalValue(array $options = ['foo' => InputOption::VALUE_OPTIONAL])
476     {
477         return "Foo is " . var_export($options['foo'], true);
478     }
479
480     /**
481      * @return string
482      */
483     public function defaultOptionDefaultsToTrue(array $options = ['foo' => true])
484     {
485         return "Foo is " . var_export($options['foo'], true);
486     }
487
488     /**
489      * This is the test:required-array-option command
490      *
491      * This command will print all the valused of passed option
492      *
493      * @param array $opts
494      * @return string
495      */
496     public function testRequiredArrayOption(array $opts = ['arr|a' => []])
497     {
498         return implode(' ', $opts['arr']);
499     }
500
501     /**
502      * This is the test:array-option command
503      *
504      * This command will print all the valused of passed option
505      *
506      * @param array $opts
507      * @return string
508      */
509     public function testArrayOption(array $opts = ['arr|a' => ['1', '2', '3']])
510     {
511         return implode(' ', $opts['arr']);
512     }
513
514     /**
515      * @command global-options-only
516      */
517     public function globalOptionsOnly($arg, array $options = [])
518     {
519         return "Arg is $arg, options[help] is " . var_export($options['help'], true) . "\n";
520     }
521 }