1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace GarethEllis\Tldr\Console; |
4
|
|
|
|
5
|
|
|
use GarethEllis\Tldr\Console\Command\TldrCommand; |
6
|
|
|
use Symfony\Component\Console\Application; |
7
|
|
|
use Symfony\Component\Console\Input\ArgvInput; |
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
10
|
|
|
|
11
|
|
|
class TldrApplication extends Application |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* Gets the name of the command based on input. |
15
|
|
|
* |
16
|
|
|
* @param InputInterface $input The input interface |
17
|
|
|
* |
18
|
|
|
* @return string The command name |
19
|
|
|
*/ |
20
|
|
|
protected function getCommandName(InputInterface $input) |
21
|
|
|
{ |
22
|
|
|
// This should return the name of your command. |
23
|
|
|
return 'tldr'; |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Gets the default commands that should always be available. |
28
|
|
|
* |
29
|
|
|
* @return array An array of default Command instances |
30
|
|
|
*/ |
31
|
|
|
protected function getDefaultCommands() |
32
|
|
|
{ |
33
|
|
|
// Keep the core default commands to have the HelpCommand |
34
|
|
|
// which is used when using the --help option |
35
|
|
|
$defaultCommands = parent::getDefaultCommands(); |
36
|
|
|
$defaultCommands[] = new TldrCommand(); |
37
|
|
|
return $defaultCommands; |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Overridden so that the application doesn't expect the command |
42
|
|
|
* name to be the first argument. |
43
|
|
|
*/ |
44
|
|
|
public function getDefinition() |
45
|
|
|
{ |
46
|
|
|
$inputDefinition = parent::getDefinition(); |
47
|
|
|
// clear out the normal first argument, which is the command name |
48
|
|
|
$inputDefinition->setArguments(); |
49
|
|
|
|
50
|
|
|
return $inputDefinition; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Override parent method so that --help options is used when app is called with no arguments or options |
55
|
|
|
* |
56
|
|
|
* @param InputInterface|null $input |
57
|
|
|
* @param OutputInterface|null $output |
58
|
|
|
* @return int |
59
|
|
|
* @throws \Exception |
60
|
|
|
*/ |
61
|
|
|
public function run(InputInterface $input = null, OutputInterface $output = null) |
|
|
|
|
62
|
|
|
{ |
63
|
|
|
if ($input === null) { |
64
|
|
|
if (count($_SERVER["argv"]) <= 1) { |
65
|
|
|
$args = array_merge($_SERVER["argv"], ["--help"]); |
66
|
|
|
$input = new ArgvInput($args); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
return parent::run($input, $output); |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: