1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Bowerphp. |
5
|
|
|
* |
6
|
|
|
* (c) Massimiliano Arione <[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 Bowerphp\Console; |
13
|
|
|
|
14
|
|
|
use Bowerphp\Command; |
15
|
|
|
use Bowerphp\Util\ErrorHandler; |
16
|
|
|
use Symfony\Component\Console\Application as BaseApplication; |
17
|
|
|
use Symfony\Component\Console\Input\ArrayInput; |
18
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
19
|
|
|
use Symfony\Component\Console\Input\InputOption; |
20
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* The console application that handles the commands |
24
|
|
|
* Inspired by Composer https://github.com/composer/composer |
25
|
|
|
*/ |
26
|
|
|
class Application extends BaseApplication |
|
|
|
|
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var Bowerphp |
30
|
|
|
*/ |
31
|
|
|
protected $bowerphp; |
32
|
|
|
|
33
|
|
|
private static $logo = ' ____ __ |
34
|
|
|
/ __ )____ _ _____ _________ / /_ ____ |
35
|
|
|
/ __ / __ \ | /| / / _ \/ ___/ __ \/ __ \/ __ \ |
36
|
|
|
/ /_/ / /_/ / |/ |/ / __/ / / /_/ / / / / /_/ / |
37
|
|
|
/_____/\____/|__/|__/\___/_/ / .___/_/ /_/ .___/ |
38
|
|
|
/_/ /_/ |
39
|
|
|
'; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* Constructor |
43
|
|
|
*/ |
44
|
|
|
public function __construct() |
45
|
|
|
{ |
46
|
|
|
ErrorHandler::register(); |
47
|
|
|
parent::__construct('Bowerphp', '0.5 Powered by BeeLab (bee-lab.net)'); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function doRun(InputInterface $input, OutputInterface $output) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
if ($input->hasParameterOption('--profile')) { |
56
|
|
|
$startTime = microtime(true); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if ($input->hasParameterOption('--token')&&($token = $input->getParameterOption(['--token', '-t']))) { |
60
|
|
|
putenv("BOWERPHP_TOKEN=$token"); |
61
|
|
|
$GLOBALS['BOWERPHP_TOKEN'] = $token; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($newWorkDir = $this->getNewWorkingDir($input)) { |
65
|
|
|
$oldWorkingDir = getcwd(); |
66
|
|
|
chdir($newWorkDir); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$result = $this->symfonyDoRun($input, $output); |
70
|
|
|
|
71
|
|
|
if (isset($oldWorkingDir)) { |
72
|
|
|
chdir($oldWorkingDir); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
if (isset($startTime)) { |
76
|
|
|
$output->writeln('<info>Memory usage: ' . round(memory_get_usage() / 1024 / 1024, 2) . 'MB (peak: ' . round(memory_get_peak_usage() / 1024 / 1024, 2) . 'MB), time: ' . round(microtime(true) - $startTime, 2) . 's'); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $result; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* {@inheritdoc} |
84
|
|
|
*/ |
85
|
|
|
public function getHelp() |
86
|
|
|
{ |
87
|
|
|
return self::$logo . parent::getHelp(); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* {@inheritdoc} |
92
|
|
|
*/ |
93
|
|
|
protected function getDefaultCommands() |
94
|
|
|
{ |
95
|
|
|
return [ |
|
|
|
|
96
|
|
|
new Command\HelpCommand(), |
97
|
|
|
new Command\CommandListCommand(), |
98
|
|
|
new Command\HomeCommand(), |
99
|
|
|
new Command\InfoCommand(), |
100
|
|
|
new Command\InitCommand(), |
101
|
|
|
new Command\InstallCommand(), |
102
|
|
|
new Command\ListCommand(), |
103
|
|
|
new Command\LookupCommand(), |
104
|
|
|
new Command\SearchCommand(), |
105
|
|
|
new Command\UpdateCommand(), |
106
|
|
|
new Command\UninstallCommand(), |
107
|
|
|
]; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* {@inheritdoc} |
112
|
|
|
*/ |
113
|
|
|
protected function getDefaultInputDefinition() |
114
|
|
|
{ |
115
|
|
|
$definition = parent::getDefaultInputDefinition(); |
116
|
|
|
$definition->addOption(new InputOption('--token', '-t', InputOption::VALUE_OPTIONAL, 'Pass github token as parameter')); |
117
|
|
|
$definition->addOption(new InputOption('--profile', null, InputOption::VALUE_NONE, 'Display timing and memory usage information')); |
118
|
|
|
$definition->addOption(new InputOption('--working-dir', '-d', InputOption::VALUE_REQUIRED, 'If specified, use the given directory as working directory.')); |
119
|
|
|
|
120
|
|
|
return $definition; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* {@inheritdoc} |
125
|
|
|
*/ |
126
|
|
|
protected function getDefaultHelperSet() |
127
|
|
|
{ |
128
|
|
|
$helperSet = parent::getDefaultHelperSet(); |
129
|
|
|
$helperSet->set(new \Bowerphp\Command\Helper\QuestionHelper()); |
130
|
|
|
|
131
|
|
|
return $helperSet; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param InputInterface $input |
136
|
|
|
* @throws \RuntimeException |
137
|
|
|
*/ |
138
|
|
|
private function getNewWorkingDir(InputInterface $input) |
139
|
|
|
{ |
140
|
|
|
$workingDir = $input->getParameterOption(['--working-dir', '-d']); |
141
|
|
|
if (false !== $workingDir && !is_dir($workingDir)) { |
142
|
|
|
throw new \RuntimeException('Invalid working directory specified.'); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
return $workingDir; |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* Copy of original Symfony doRun, to allow a default command |
150
|
|
|
* |
151
|
|
|
* @param InputInterface $input An Input instance |
152
|
|
|
* @param OutputInterface $output An Output instance |
153
|
|
|
* @param string $default Default command to execute |
154
|
|
|
* |
155
|
|
|
* @return int 0 if everything went fine, or an error code |
156
|
|
|
* @codeCoverageIgnore |
157
|
|
|
*/ |
158
|
|
|
private function symfonyDoRun(InputInterface $input, OutputInterface $output, $default = 'list-commands') |
159
|
|
|
{ |
160
|
|
|
if (true === $input->hasParameterOption(['--version', '-V'])) { |
161
|
|
|
$output->writeln($this->getLongVersion()); |
162
|
|
|
|
163
|
|
|
return 0; |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
$name = $this->getCommandName($input); |
167
|
|
|
|
168
|
|
|
if (true === $input->hasParameterOption(['--help', '-h'])) { |
169
|
|
|
if (!$name) { |
170
|
|
|
$name = 'help'; |
171
|
|
|
$input = new ArrayInput(['command' => 'help']); |
172
|
|
|
} else { |
173
|
|
|
$this->wantHelps = true; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
177
|
|
|
if (!$name) { |
178
|
|
|
$name = $default; |
179
|
|
|
$input = new ArrayInput(['command' => $default]); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
// the command name MUST be the first element of the input |
183
|
|
|
$command = $this->find($name); |
184
|
|
|
|
185
|
|
|
$this->runningCommand = $command; |
186
|
|
|
$exitCode = $this->doRunCommand($command, $input, $output); |
187
|
|
|
$this->runningCommand = null; |
188
|
|
|
|
189
|
|
|
return $exitCode; |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|