|
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
|
|
|
protected function getDefaultInputDefinition() |
|
43
|
|
|
{ |
|
44
|
|
|
$inputDefinition = parent::getDefaultInputDefinition(); |
|
45
|
|
|
|
|
46
|
|
|
$inputDefinition->addOption( |
|
47
|
|
|
new InputOption('--file', '-f', InputOption::VALUE_OPTIONAL, 'Specify Deployer file') |
|
48
|
|
|
); |
|
49
|
|
|
|
|
50
|
|
|
return $inputDefinition; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* {@inheritdoc} |
|
55
|
|
|
*/ |
|
56
|
|
|
protected function getDefaultCommands() |
|
57
|
|
|
{ |
|
58
|
|
|
$commands = parent::getDefaultCommands(); |
|
59
|
|
|
|
|
60
|
|
|
if ($this->isPharArchive()) { |
|
61
|
|
|
$commands[] = $this->selfUpdateCommand(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
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
|
|
|
protected function getDefaultHelperSet() |
|
82
|
|
|
{ |
|
83
|
|
|
$helperSet = parent::getDefaultHelperSet(); |
|
84
|
|
|
|
|
85
|
|
|
if ($this->isPharArchive()) { |
|
86
|
|
|
$helperSet->set(new PharUpdateHelper()); |
|
87
|
|
|
} |
|
88
|
|
|
return $helperSet; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return InputDefinition |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getUserDefinition() |
|
95
|
|
|
{ |
|
96
|
|
|
if (null === $this->userDefinition) { |
|
97
|
|
|
$this->userDefinition = new InputDefinition(); |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $this->userDefinition; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Add user definition arguments and options to definition. |
|
105
|
|
|
*/ |
|
106
|
|
|
public function addUserArgumentsAndOptions() |
|
107
|
|
|
{ |
|
108
|
|
|
$this->getDefinition()->addArguments($this->getUserDefinition()->getArguments()); |
|
109
|
|
|
$this->getDefinition()->addOptions($this->getUserDefinition()->getOptions()); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @return bool |
|
114
|
|
|
*/ |
|
115
|
|
|
public function isPharArchive() |
|
116
|
|
|
{ |
|
117
|
|
|
return 'phar:' === substr(__FILE__, 0, 5); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
/** |
|
121
|
|
|
* {@inheritdoc} |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function doRunCommand(Command $command, InputInterface $input, OutputInterface $output) |
|
124
|
|
|
{ |
|
125
|
|
|
$exception = null; |
|
126
|
|
|
$exitCode = 0; |
|
127
|
|
|
|
|
128
|
|
|
if (!empty($this->catchIO)) { |
|
129
|
|
|
list($input, $output) = call_user_func($this->catchIO, $input, $output); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
try { |
|
133
|
|
|
$exitCode = parent::doRunCommand($command, $input, $output); |
|
134
|
|
|
} catch (\Exception $x) { |
|
135
|
|
|
$exception = $x; |
|
136
|
|
|
} catch (\Throwable $x) { |
|
137
|
|
|
$exception = $x; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
if (!empty($this->after)) { |
|
141
|
|
|
call_user_func($this->after, new CommandEvent($command, $input, $output, $exception, $exitCode)); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ($exception !== null) { |
|
145
|
|
|
throw $exception; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
|
|
return $exitCode; |
|
149
|
|
|
} |
|
150
|
|
|
|
|
151
|
|
|
/** |
|
152
|
|
|
* @param $callable |
|
153
|
|
|
*/ |
|
154
|
|
|
public function catchIO($callable) |
|
155
|
|
|
{ |
|
156
|
|
|
$this->catchIO = $callable; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* @param $callable |
|
161
|
|
|
*/ |
|
162
|
|
|
public function afterRun($callable) |
|
163
|
|
|
{ |
|
164
|
|
|
$this->after = $callable; |
|
165
|
|
|
} |
|
166
|
|
|
} |
|
167
|
|
|
|