1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of `Composer as a service`. |
5
|
|
|
* |
6
|
|
|
* (c) Pascal Borreli <[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 Ayaline\Bundle\ComposerBundle\Consumer\Step; |
13
|
|
|
|
14
|
|
|
use Sonata\NotificationBundle\Consumer\ConsumerEvent; |
15
|
|
|
use Symfony\Component\Filesystem\Filesystem; |
16
|
|
|
use Symfony\Component\Process\Process; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @author Hubert Moutot <[email protected]> |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractStep implements StepInterface |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* @var \Pusher |
25
|
|
|
*/ |
26
|
|
|
protected $pusher; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var Filesystem |
30
|
|
|
*/ |
31
|
|
|
protected $filesystem; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var string |
35
|
|
|
*/ |
36
|
|
|
protected $rootDir; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var string |
40
|
|
|
*/ |
41
|
|
|
protected $workingTempPath; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @var string |
45
|
|
|
*/ |
46
|
|
|
protected $path; |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* @var string |
50
|
|
|
*/ |
51
|
|
|
protected $composerBinPath; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param \Pusher $pusher |
55
|
|
|
*/ |
56
|
|
|
public function setPusher(\Pusher $pusher) |
57
|
|
|
{ |
58
|
|
|
$this->pusher = $pusher; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @param Filesystem $filesystem |
63
|
|
|
*/ |
64
|
|
|
public function setFilesystem(Filesystem $filesystem) |
65
|
|
|
{ |
66
|
|
|
$this->filesystem = $filesystem; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param string $rootDir |
71
|
|
|
*/ |
72
|
|
|
public function setRootDir($rootDir) |
73
|
|
|
{ |
74
|
|
|
$this->rootDir = $rootDir; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @param string $workingTempPath |
79
|
|
|
*/ |
80
|
|
|
public function setWorkingTempPath($workingTempPath) |
81
|
|
|
{ |
82
|
|
|
$this->workingTempPath = $workingTempPath; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $composerBinPath |
87
|
|
|
*/ |
88
|
|
|
public function setComposerBinPath($composerBinPath) |
89
|
|
|
{ |
90
|
|
|
$this->composerBinPath = $composerBinPath; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Extracts a channel name from a ConsumerEvent. |
95
|
|
|
* |
96
|
|
|
* @param ConsumerEvent $event |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
|
|
*/ |
100
|
|
|
protected function getChannels(ConsumerEvent $event) |
101
|
|
|
{ |
102
|
|
|
$message = $event->getMessage(); |
103
|
|
|
|
104
|
|
|
return [$message->getValue('channelName')]; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Triggers a "consumer:new-step" message on Pusher. |
109
|
|
|
* |
110
|
|
|
* @param ConsumerEvent $event |
111
|
|
|
* @param array $message |
112
|
|
|
*/ |
113
|
|
|
protected function triggerNewStep(ConsumerEvent $event, $message) |
114
|
|
|
{ |
115
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:new-step', $message); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Triggers a "consumer:step-error" message on Pusher. |
120
|
|
|
* |
121
|
|
|
* @param ConsumerEvent $event |
122
|
|
|
* @param array $message |
123
|
|
|
*/ |
124
|
|
|
protected function triggerStepError(ConsumerEvent $event, $message) |
125
|
|
|
{ |
126
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:step-error', $message); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Triggers a "consumer:error" message on Pusher. |
131
|
|
|
* |
132
|
|
|
* @param ConsumerEvent $event |
133
|
|
|
* @param array $message |
134
|
|
|
*/ |
135
|
|
|
protected function triggerError(ConsumerEvent $event, $message) |
136
|
|
|
{ |
137
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:error', $message); |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
/** |
141
|
|
|
* Triggers a "consumer:success" message on Pusher. |
142
|
|
|
* |
143
|
|
|
* @param ConsumerEvent $event |
144
|
|
|
* @param array $message |
145
|
|
|
*/ |
146
|
|
|
protected function triggerSuccess(ConsumerEvent $event, $message) |
147
|
|
|
{ |
148
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:success', $message); |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Triggers a "consumer:composer-output" message on Pusher. |
153
|
|
|
* |
154
|
|
|
* @param ConsumerEvent $event |
155
|
|
|
* @param array $message |
156
|
|
|
*/ |
157
|
|
|
protected function triggerComposerOutput(ConsumerEvent $event, $message) |
158
|
|
|
{ |
159
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:composer-output', $message); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Triggers a "consumer:composer-installed" message on Pusher. |
164
|
|
|
* |
165
|
|
|
* @param ConsumerEvent $event |
166
|
|
|
* @param array $message |
167
|
|
|
*/ |
168
|
|
|
protected function triggerComposerInstalled(ConsumerEvent $event, $message) |
169
|
|
|
{ |
170
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:composer-installed', $message); |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Triggers a "consumer:vulnerabilities" message on Pusher. |
175
|
|
|
* |
176
|
|
|
* @param ConsumerEvent $event |
177
|
|
|
* @param array $message |
178
|
|
|
*/ |
179
|
|
|
protected function triggerVulnerabilities(ConsumerEvent $event, $message) |
180
|
|
|
{ |
181
|
|
|
$this->pusher->trigger($this->getChannels($event), 'consumer:vulnerabilities', $message); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Runs a process. |
186
|
|
|
* |
187
|
|
|
* @param string $commandline the command line to execute |
188
|
|
|
* @param string $workingDirectory the current working directory |
189
|
|
|
* @param string $output the output |
190
|
|
|
* |
191
|
|
|
* @return Process the resulting Process |
192
|
|
|
*/ |
193
|
|
|
protected function runProcess($commandline, $workingDirectory, &$output) |
194
|
|
|
{ |
195
|
|
|
$callback = function ($type, $data) use (&$output) { |
196
|
|
|
if ('' == $data = trim($data)) { |
197
|
|
|
return; |
198
|
|
|
} |
199
|
|
|
if ($type == 'err') { |
200
|
|
|
$output .= $data.PHP_EOL; |
201
|
|
|
} else { |
202
|
|
|
$output = $data.PHP_EOL; |
203
|
|
|
} |
204
|
|
|
}; |
205
|
|
|
|
206
|
|
|
$process = new Process($commandline); |
207
|
|
|
$process->setWorkingDirectory($workingDirectory); |
208
|
|
|
$process->setTimeout(300); |
209
|
|
|
$process->run($callback); |
210
|
|
|
|
211
|
|
|
return $process; |
212
|
|
|
} |
213
|
|
|
} |
214
|
|
|
|