1
|
|
|
<?php |
2
|
|
|
/* (c) Anton Medvedev <[email protected]> |
3
|
|
|
* |
4
|
|
|
* For the full copyright and license information, please view the LICENSE |
5
|
|
|
* file that was distributed with this source code. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace Deployer\Console; |
9
|
|
|
|
10
|
|
|
use Deployer\Component\PharUpdate\Console\Command as PharUpdateCommand; |
11
|
|
|
use Deployer\Component\PharUpdate\Console\Helper as PharUpdateHelper; |
12
|
|
|
use Deployer\Deployer; |
13
|
|
|
use Symfony\Component\Console\Application as Console; |
14
|
|
|
use Symfony\Component\Console\Command\Command; |
15
|
|
|
use Symfony\Component\Console\Input\InputDefinition; |
16
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
17
|
|
|
use Symfony\Component\Console\Input\InputOption; |
18
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
19
|
|
|
|
20
|
|
|
class Application extends Console |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* Input definition for user specific arguments and options. |
24
|
|
|
* |
25
|
|
|
* @var InputDefinition |
26
|
|
|
*/ |
27
|
|
|
private $userDefinition; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var callable |
31
|
|
|
*/ |
32
|
|
|
private $catchIO; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var callable |
36
|
|
|
*/ |
37
|
|
|
private $after; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* {@inheritdoc} |
41
|
|
|
*/ |
42
|
12 |
|
protected function getDefaultInputDefinition() |
43
|
|
|
{ |
44
|
12 |
|
$inputDefinition = parent::getDefaultInputDefinition(); |
45
|
|
|
|
46
|
12 |
|
$inputDefinition->addOption( |
47
|
12 |
|
new InputOption('--file', '-f', InputOption::VALUE_OPTIONAL, 'Specify Deployer file') |
48
|
|
|
); |
49
|
|
|
|
50
|
12 |
|
return $inputDefinition; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* {@inheritdoc} |
55
|
|
|
*/ |
56
|
12 |
|
protected function getDefaultCommands() |
57
|
|
|
{ |
58
|
12 |
|
$commands = parent::getDefaultCommands(); |
59
|
|
|
|
60
|
12 |
|
if ($this->isPharArchive()) { |
61
|
|
|
$commands[] = $this->selfUpdateCommand(); |
62
|
|
|
} |
63
|
|
|
|
64
|
12 |
|
return $commands; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* {@inheritdoc} |
69
|
|
|
*/ |
70
|
|
|
private function selfUpdateCommand() |
71
|
|
|
{ |
72
|
|
|
$selfUpdate = new PharUpdateCommand('self-update'); |
73
|
|
|
$selfUpdate->setDescription('Updates deployer.phar to the latest version'); |
74
|
|
|
$selfUpdate->setManifestUri('https://deployer.org/manifest.json'); |
75
|
|
|
return $selfUpdate; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* {@inheritdoc} |
80
|
|
|
*/ |
81
|
12 |
|
protected function getDefaultHelperSet() |
82
|
|
|
{ |
83
|
12 |
|
$helperSet = parent::getDefaultHelperSet(); |
84
|
|
|
|
85
|
12 |
|
if ($this->isPharArchive()) { |
86
|
|
|
$helperSet->set(new PharUpdateHelper()); |
87
|
|
|
} |
88
|
12 |
|
return $helperSet; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return InputDefinition |
93
|
|
|
*/ |
94
|
12 |
|
public function getUserDefinition() |
95
|
|
|
{ |
96
|
12 |
|
if (null === $this->userDefinition) { |
97
|
12 |
|
$this->userDefinition = new InputDefinition(); |
98
|
|
|
} |
99
|
|
|
|
100
|
12 |
|
return $this->userDefinition; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Add user definition arguments and options to definition. |
105
|
|
|
*/ |
106
|
12 |
|
public function addUserArgumentsAndOptions() |
107
|
|
|
{ |
108
|
12 |
|
$this->getDefinition()->addArguments($this->getUserDefinition()->getArguments()); |
109
|
12 |
|
$this->getDefinition()->addOptions($this->getUserDefinition()->getOptions()); |
110
|
12 |
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @return bool |
114
|
|
|
*/ |
115
|
12 |
|
public function isPharArchive() |
116
|
|
|
{ |
117
|
12 |
|
return 'phar:' === substr(__FILE__, 0, 5); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* {@inheritdoc} |
122
|
|
|
*/ |
123
|
11 |
|
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
124
|
|
|
{ |
125
|
11 |
|
$exception = null; |
126
|
11 |
|
$exitCode = 0; |
127
|
|
|
|
128
|
11 |
|
if (!empty($this->catchIO)) { |
129
|
11 |
|
list($input, $output) = call_user_func($this->catchIO, $input, $output); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
try { |
133
|
11 |
|
$exitCode = parent::doRunCommand($command, $input, $output); |
134
|
1 |
|
} catch (\Exception $x) { |
135
|
1 |
|
$exception = $x; |
136
|
|
|
} catch (\Throwable $x) { |
137
|
|
|
$exception = $x; |
138
|
|
|
} |
139
|
|
|
|
140
|
11 |
|
if (!empty($this->after)) { |
141
|
|
|
call_user_func($this->after, new CommandEvent($command, $input, $output, $exception, $exitCode)); |
142
|
|
|
} |
143
|
|
|
|
144
|
11 |
|
if ($exception !== null) { |
145
|
1 |
|
throw $exception; |
146
|
|
|
} |
147
|
|
|
|
148
|
10 |
|
return $exitCode; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* @param $callable |
153
|
|
|
*/ |
154
|
12 |
|
public function catchIO($callable) |
155
|
|
|
{ |
156
|
12 |
|
$this->catchIO = $callable; |
157
|
12 |
|
} |
158
|
|
|
|
159
|
|
|
/** |
160
|
|
|
* @param $callable |
161
|
|
|
*/ |
162
|
12 |
|
public function afterRun($callable) |
163
|
|
|
{ |
164
|
12 |
|
$this->after = $callable; |
165
|
12 |
|
} |
166
|
|
|
} |
167
|
|
|
|