Version 1
[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      * This is the my:cat command
38      *
39      * This command will concatenate two parameters. If the --flip flag
40      * is provided, then the result is the concatenation of two and one.
41      *
42      * @param string $one The first parameter.
43      * @param string $two The other parameter.
44      * @option boolean $flip Whether or not the second parameter should come first in the result.
45      * @aliases c
46      * @usage bet alpha --flip
47      *   Concatenate "alpha" and "bet".
48      * @arbitrary This annotation is here merely as a marker used in testing.
49      */
50     public function myCat($one, $two = '', $options = ['flip' => false])
51     {
52         if ($options['flip']) {
53             return "{$two}{$one}";
54         }
55         return "{$one}{$two}";
56     }
57
58     /**
59      * @command my:repeat
60      */
61     public function myRepeat($one, $two = '', $options = ['repeat' => 1])
62     {
63         return str_repeat("{$one}{$two}", $options['repeat']);
64     }
65
66     /**
67      * This is the my:join command
68      *
69      * This command will join its parameters together. It can also reverse and repeat its arguments.
70      *
71      * @command my:join
72      * @usage a b
73      *   Join a and b to produce "a,b"
74      * @usage
75      *   Example with no parameters or options
76      */
77     public function myJoin(array $args, $options = ['flip' => false, 'repeat' => 1])
78     {
79         if ($options['flip']) {
80             $args = array_reverse($args);
81         }
82         $result = implode('', $args);
83         return str_repeat($result, $options['repeat']);
84     }
85
86     /**
87      * This is a command with no options
88      *
89      * This command will concatenate two parameters.
90      *
91      * @param $one The first parameter.
92      * @param $two The other parameter.
93      * @aliases nope
94      * @usage alpha bet
95      *   Concatenate "alpha" and "bet".
96      */
97     public function commandWithNoOptions($one, $two = 'default')
98     {
99         return "{$one}{$two}";
100     }
101
102     /**
103      * This command has no arguments--only options
104      *
105      * Return a result only if not silent.
106      *
107      * @option silent Supress output.
108      */
109     public function commandWithNoArguments($opts = ['silent|s' => false])
110     {
111         if (!$opts['silent']) {
112             return "Hello, world";
113         }
114     }
115
116     /**
117      * Shortcut on annotation
118      *
119      * This command defines the option shortcut on the annotation instead of in the options array.
120      *
121      * @param $opts The options
122      * @option silent|s Supress output.
123      */
124     public function shortcutOnAnnotation($opts = ['silent' => false])
125     {
126         if (!$opts['silent']) {
127             return "Hello, world";
128         }
129     }
130
131     /**
132      * This is the test:arithmatic command
133      *
134      * This command will add one and two. If the --negate flag
135      * is provided, then the result is negated.
136      *
137      * @command test:arithmatic
138      * @param integer $one The first number to add.
139      * @param integer $two The other number to add.
140      * @option negate Whether or not the result should be negated.
141      * @aliases arithmatic
142      * @usage 2 2 --negate
143      *   Add two plus two and then negate.
144      * @custom
145      * @dup one
146      * @dup two
147      */
148     public function testArithmatic($one, $two = 2, $options = ['negate' => false, 'unused' => 'bob'])
149     {
150         $result = $one + $two;
151         if ($options['negate']) {
152             $result = -$result;
153         }
154
155         // Integer return codes are exit codes (errors), so
156         // return a the result as a string so that it will be printed.
157         return "$result";
158     }
159
160     /**
161      * This is the test:state command
162      *
163      * This command tests to see if the state of the Commandfile instance
164      */
165     public function testState()
166     {
167         return $this->state;
168     }
169
170     /**
171      * This is the test:passthrough command
172      *
173      * This command takes a variable number of parameters as
174      * an array and returns them as a csv.
175      */
176     public function testPassthrough(array $params)
177     {
178         return implode(',', $params);
179     }
180
181     /**
182      * This command wraps its parameter in []; its alter hook
183      * then wraps the result in <>.
184      */
185     public function testHook($parameter)
186     {
187         return "[$parameter]";
188     }
189
190     /**
191      * Wrap the results of test:hook in <>.
192      *
193      * @hook alter test:hook
194      */
195     public function hookTestHook($result)
196     {
197         return "<$result>";
198     }
199
200     /**
201      * This test is very similar to the preceding test, except
202      * it uses an annotation hook instead of a named-function hook.
203      *
204      * @hookme
205      * @before >
206      * @after <
207      */
208     public function testAnnotationHook($parameter)
209     {
210         return "($parameter)";
211     }
212
213     /**
214      * Wrap the results of test:hook in whatever the @before and @after
215      * annotations contain.
216      *
217      * @hook alter @hookme
218      */
219     public function hookTestAnnotatedHook($result, CommandData $commandData)
220     {
221         $before = $commandData->annotationData()->get('before', '-');
222         $after = $commandData->annotationData()->get('after', '-');
223         return "$before$result$after";
224     }
225
226     /**
227      * Alter the results of the hook with its command name.
228      *
229      * @hook alter @addmycommandname
230      */
231     public function hookAddCommandName($result, CommandData $commandData)
232     {
233         $annotationData = $commandData->annotationData();
234         return "$result from " . $annotationData['command'];
235     }
236
237     /**
238      * Here is a hook with an explicit command annotation that we will alter
239      * with the preceeding hook
240      *
241      * @command alter-me
242      * @addmycommandname
243      */
244     public function alterMe()
245     {
246         return "splendiferous";
247     }
248
249     /**
250      * Here is another hook that has no command annotation that should be
251      * altered with the default value for the command name
252      *
253      * @addmycommandname
254      */
255     public function alterMeToo()
256     {
257         return "fantabulous";
258     }
259
260     /**
261      * @command test:replace-command
262      */
263     public function testReplaceCommand($value)
264     {
265         $this->output->writeln($value);
266     }
267
268     /**
269      * @hook replace-command test:replace-command
270      */
271     public function hookTestReplaceCommandHook($value)
272     {
273         $this->output->writeln("bar");
274     }
275
276     /**
277      * @hook pre-command test:post-command
278      */
279     public function hookTestPreCommandHook(CommandData $commandData)
280     {
281         // Use 'writeln' to detect order that hooks are called
282         $this->output->writeln("foo");
283     }
284
285     /**
286      * @command test:post-command
287      */
288     public function testPostCommand($value)
289     {
290         $this->output->writeln($value);
291     }
292
293     /**
294      * @hook post-command test:post-command
295      */
296     public function hookTestPostCommandHook($result, CommandData $commandData)
297     {
298         // Use 'writeln' to detect order that hooks are called
299         $this->output->writeln("baz");
300     }
301
302     public function testHello($who)
303     {
304         return "Hello, $who.";
305     }
306
307     public function testException($what)
308     {
309         throw new \Exception($what);
310     }
311
312     /**
313      * @hook init test:hello
314      */
315     public function initializeTestHello($input, AnnotationData $annotationData)
316     {
317         $who = $input->getArgument('who');
318         if (!$who) {
319             $input->setArgument('who', 'Huey');
320         }
321     }
322
323     /**
324      * @hook command-event test:hello
325      */
326     public function commandEventTestHello(ConsoleCommandEvent $event)
327     {
328         // Note that Symfony Console will not allow us to alter the
329         // input from this hook, so we'll just print something to
330         // show that this hook was executed.
331         $input = $event->getInput();
332         $who = $input->getArgument('who');
333         $this->output->writeln("Here comes $who!");
334     }
335
336     /**
337      * @hook interact test:hello
338      */
339     public function interactTestHello($input, $output)
340     {
341         $who = $input->getArgument('who');
342         if (!$who) {
343             $input->setArgument('who', 'Goofey');
344         }
345     }
346
347     /**
348      * @hook validate test:hello
349      */
350     public function validateTestHello($commandData)
351     {
352         $args = $commandData->arguments();
353         if ($args['who'] == 'Donald Duck') {
354             return new CommandError("I won't say hello to Donald Duck.");
355         }
356         if ($args['who'] == 'Drumph') {
357             throw new \Exception('Irrational value error.');
358         }
359     }
360
361     /**
362      * Test default values in arguments
363      *
364      * @param string|null $one
365      * @param string|null $two
366      * @return string
367      */
368     public function defaults($one = null, $two = null)
369     {
370         if ($one && $two) {
371             return "$one and $two";
372         }
373         if ($one) {
374             return "only $one";
375         }
376         return "nothing provided";
377     }
378
379     /**
380      * @return string
381      */
382     public function defaultOptionOne($options = ['foo' => '1'])
383     {
384         return "Foo is " . $options['foo'];
385     }
386
387     /**
388      * @return string
389      */
390     public function defaultOptionTwo($options = ['foo' => '2'])
391     {
392         return "Foo is " . $options['foo'];
393     }
394
395     /**
396      * @return string
397      */
398     public function defaultOptionNone($options = ['foo' => InputOption::VALUE_REQUIRED])
399     {
400         return "Foo is " . $options['foo'];
401     }
402
403     /**
404      * This is the test:required-array-option command
405      *
406      * This command will print all the valused of passed option
407      *
408      * @param array $opts
409      * @return string
410      */
411     public function testRequiredArrayOption($opts = ['arr|a' => []])
412     {
413         return implode(' ', $opts['arr']);
414     }
415
416     /**
417      * This is the test:array-option command
418      *
419      * This command will print all the valused of passed option
420      *
421      * @param array $opts
422      * @return string
423      */
424     public function testArrayOption($opts = ['arr|a' => ['1', '2', '3']])
425     {
426         return implode(' ', $opts['arr']);
427     }
428 }