1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the webmozart/console package. |
5
|
|
|
* |
6
|
|
|
* (c) Bernhard Schussek <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Webmozart\Console\Config; |
13
|
|
|
|
14
|
|
|
use Webmozart\Console\Api\Application\Application; |
15
|
|
|
use Webmozart\Console\Api\Args\Format\Argument; |
16
|
|
|
use Webmozart\Console\Api\Args\Format\Option; |
17
|
|
|
use Webmozart\Console\Api\Args\RawArgs; |
18
|
|
|
use Webmozart\Console\Api\Config\ApplicationConfig; |
19
|
|
|
use Webmozart\Console\Api\Event\ConsoleEvents; |
20
|
|
|
use Webmozart\Console\Api\Event\PreHandleEvent; |
21
|
|
|
use Webmozart\Console\Api\Event\PreResolveEvent; |
22
|
|
|
use Webmozart\Console\Api\IO\Input; |
23
|
|
|
use Webmozart\Console\Api\IO\InputStream; |
24
|
|
|
use Webmozart\Console\Api\IO\IO; |
25
|
|
|
use Webmozart\Console\Api\IO\Output; |
26
|
|
|
use Webmozart\Console\Api\IO\OutputStream; |
27
|
|
|
use Webmozart\Console\Api\Resolver\ResolvedCommand; |
28
|
|
|
use Webmozart\Console\Formatter\AnsiFormatter; |
29
|
|
|
use Webmozart\Console\Formatter\PlainFormatter; |
30
|
|
|
use Webmozart\Console\Handler\Help\HelpHandler; |
31
|
|
|
use Webmozart\Console\IO\ConsoleIO; |
32
|
|
|
use Webmozart\Console\IO\InputStream\StandardInputStream; |
33
|
|
|
use Webmozart\Console\IO\OutputStream\ErrorOutputStream; |
34
|
|
|
use Webmozart\Console\IO\OutputStream\StandardOutputStream; |
35
|
|
|
use Webmozart\Console\UI\Component\NameVersion; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* The default application configuration. |
39
|
|
|
* |
40
|
|
|
* @since 1.0 |
41
|
|
|
* |
42
|
|
|
* @author Bernhard Schussek <[email protected]> |
43
|
|
|
*/ |
44
|
|
|
class DefaultApplicationConfig extends ApplicationConfig |
45
|
|
|
{ |
46
|
|
|
/** |
47
|
|
|
* {@inheritdoc} |
48
|
|
|
*/ |
49
|
78 |
|
protected function configure() |
50
|
|
|
{ |
51
|
|
|
$this |
52
|
78 |
|
->setIOFactory(array($this, 'createIO')) |
53
|
78 |
|
->addEventListener(ConsoleEvents::PRE_RESOLVE, array($this, 'resolveHelpCommand')) |
54
|
78 |
|
->addEventListener(ConsoleEvents::PRE_HANDLE, array($this, 'printVersion')) |
55
|
|
|
|
56
|
78 |
|
->addOption('help', 'h', Option::NO_VALUE, 'Display help about the command') |
57
|
78 |
|
->addOption('quiet', 'q', Option::NO_VALUE, 'Do not output any message') |
58
|
78 |
|
->addOption('verbose', 'v', Option::OPTIONAL_VALUE, 'Increase the verbosity of messages: "-v" for normal output, "-vv" for more verbose output and "-vvv" for debug', null, 'level') |
59
|
78 |
|
->addOption('version', 'V', Option::NO_VALUE, 'Display this application version') |
60
|
78 |
|
->addOption('ansi', null, Option::NO_VALUE, 'Force ANSI output') |
61
|
78 |
|
->addOption('no-ansi', null, Option::NO_VALUE, 'Disable ANSI output') |
62
|
78 |
|
->addOption('no-interaction', 'n', Option::NO_VALUE, 'Do not ask any interactive question') |
63
|
|
|
|
64
|
78 |
|
->beginCommand('help') |
65
|
78 |
|
->markDefault() |
66
|
78 |
|
->setDescription('Display the manual of a command') |
67
|
78 |
|
->addArgument('command', Argument::OPTIONAL, 'The command name') |
68
|
78 |
|
->addOption('man', 'm', Option::NO_VALUE, 'Output the help as man page') |
69
|
78 |
|
->addOption('ascii-doc', null, Option::NO_VALUE, 'Output the help as AsciiDoc document') |
70
|
78 |
|
->addOption('text', 't', Option::NO_VALUE, 'Output the help as plain text') |
71
|
78 |
|
->addOption('xml', 'x', Option::NO_VALUE, 'Output the help as XML') |
72
|
78 |
|
->addOption('json', 'j', Option::NO_VALUE, 'Output the help as JSON') |
73
|
|
|
->setHandler(function () { return new HelpHandler(); }) |
74
|
78 |
|
->end() |
75
|
|
|
; |
76
|
78 |
|
} |
77
|
|
|
|
78
|
31 |
|
public function createIO(Application $application, RawArgs $args, InputStream $inputStream = null, OutputStream $outputStream = null, OutputStream $errorStream = null) |
79
|
|
|
{ |
80
|
31 |
|
$inputStream = $inputStream ?: new StandardInputStream(); |
81
|
31 |
|
$outputStream = $outputStream ?: new StandardOutputStream(); |
82
|
31 |
|
$errorStream = $errorStream ?: new ErrorOutputStream(); |
83
|
31 |
|
$styleSet = $application->getConfig()->getStyleSet(); |
84
|
|
|
|
85
|
31 |
|
if ($args->hasToken('--no-ansi')) { |
86
|
4 |
|
$outputFormatter = $errorFormatter = new PlainFormatter($styleSet); |
87
|
27 |
|
} elseif ($args->hasToken('--ansi')) { |
88
|
1 |
|
$outputFormatter = $errorFormatter = new AnsiFormatter($styleSet); |
89
|
|
|
} else { |
90
|
26 |
|
$outputFormatter = $outputStream->supportsAnsi() ? new AnsiFormatter($styleSet) : new PlainFormatter($styleSet); |
91
|
26 |
|
$errorFormatter = $errorStream->supportsAnsi() ? new AnsiFormatter($styleSet) : new PlainFormatter($styleSet); |
92
|
|
|
} |
93
|
|
|
|
94
|
31 |
|
$io = new ConsoleIO( |
95
|
31 |
|
new Input($inputStream), |
96
|
31 |
|
new Output($outputStream, $outputFormatter), |
97
|
31 |
|
new Output($errorStream, $errorFormatter) |
98
|
|
|
); |
99
|
|
|
|
100
|
31 |
|
if ($args->hasToken('-vvv') || $this->isDebug()) { |
101
|
2 |
|
$io->setVerbosity(IO::DEBUG); |
102
|
29 |
|
} elseif ($args->hasToken('-vv')) { |
103
|
1 |
|
$io->setVerbosity(IO::VERY_VERBOSE); |
104
|
28 |
|
} elseif ($args->hasToken('-v')) { |
105
|
1 |
|
$io->setVerbosity(IO::VERBOSE); |
106
|
|
|
} |
107
|
|
|
|
108
|
31 |
|
if ($args->hasToken('--quiet') || $args->hasToken('-q')) { |
109
|
2 |
|
$io->setQuiet(true); |
110
|
|
|
} |
111
|
|
|
|
112
|
31 |
|
if ($args->hasToken('--no-interaction') || $args->hasToken('-n')) { |
113
|
2 |
|
$io->setInteractive(false); |
114
|
|
|
} |
115
|
|
|
|
116
|
31 |
|
return $io; |
117
|
|
|
} |
118
|
|
|
|
119
|
31 |
|
public function resolveHelpCommand(PreResolveEvent $event) |
120
|
|
|
{ |
121
|
31 |
|
$args = $event->getRawArgs(); |
122
|
|
|
|
123
|
31 |
|
if ($args->hasToken('-h') || $args->hasToken('--help')) { |
124
|
8 |
|
$command = $event->getApplication()->getCommand('help'); |
125
|
|
|
|
126
|
|
|
// Enable lenient args parsing |
127
|
8 |
|
$parsedArgs = $command->parseArgs($args, true); |
128
|
|
|
|
129
|
8 |
|
$event->setResolvedCommand(new ResolvedCommand($command, $parsedArgs)); |
130
|
8 |
|
$event->stopPropagation(); |
131
|
|
|
} |
132
|
31 |
|
} |
133
|
|
|
|
134
|
31 |
|
public function printVersion(PreHandleEvent $event) |
135
|
|
|
{ |
136
|
31 |
|
if ($event->getArgs()->isOptionSet('version')) { |
137
|
4 |
|
$version = new NameVersion($event->getCommand()->getApplication()->getConfig()); |
138
|
4 |
|
$version->render($event->getIO()); |
139
|
|
|
|
140
|
4 |
|
$event->setHandled(true); |
141
|
|
|
} |
142
|
31 |
|
} |
143
|
|
|
} |
144
|
|
|
|