1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Drush Cerbere command line tools. |
5
|
|
|
* Copyright (C) 2015 - Sebastien Malot <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or modify |
8
|
|
|
* it under the terms of the GNU General Public License as published by |
9
|
|
|
* the Free Software Foundation; either version 2 of the License, or |
10
|
|
|
* (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License along |
18
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc., |
19
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace Cerbere\Action; |
23
|
|
|
|
24
|
|
|
use Cerbere\Event\CerbereDoActionEvent; |
25
|
|
|
use Cerbere\Event\CerbereDoneActionEvent; |
26
|
|
|
use Cerbere\Event\CerbereEvents; |
27
|
|
|
use Cerbere\Event\CerbereReportActionEvent; |
28
|
|
|
use Cerbere\Model\Project; |
29
|
|
|
use Cerbere\Model\Release; |
30
|
|
|
use Cerbere\Model\ReleaseHistory; |
31
|
|
|
use Doctrine\Common\Cache\CacheProvider; |
32
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher; |
33
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
34
|
|
|
use Symfony\Component\EventDispatcher\EventSubscriberInterface; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Class Update |
38
|
|
|
* @package Cerbere\Action |
39
|
|
|
*/ |
40
|
|
|
class Update implements ActionInterface |
41
|
|
|
{ |
42
|
|
|
/** |
43
|
|
|
* @var CacheProvider |
44
|
|
|
*/ |
45
|
|
|
protected $cache; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @var EventDispatcherInterface |
49
|
|
|
*/ |
50
|
|
|
protected $dispatcher; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Update constructor. |
54
|
|
|
*/ |
55
|
|
|
public function __construct() |
56
|
|
|
{ |
57
|
|
|
|
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @param EventSubscriberInterface $listener |
62
|
|
|
*/ |
63
|
|
|
public function addLoggerListener(EventSubscriberInterface $listener) |
64
|
|
|
{ |
65
|
|
|
$this->getDispatcher()->addSubscriber($listener); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Gets the dispatcher used by this library to dispatch events. |
70
|
|
|
* |
71
|
|
|
* @return EventDispatcherInterface |
72
|
|
|
*/ |
73
|
|
|
public function getDispatcher() |
74
|
|
|
{ |
75
|
|
|
if (!isset($this->dispatcher)) { |
76
|
|
|
$this->dispatcher = new EventDispatcher(); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
return $this->dispatcher; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Sets the dispatcher used by this library to dispatch events. |
84
|
|
|
* |
85
|
|
|
* @param EventDispatcherInterface $dispatcher |
86
|
|
|
* The Symfony event dispatcher object. |
87
|
|
|
*/ |
88
|
|
|
public function setDispatcher(EventDispatcherInterface $dispatcher) |
89
|
|
|
{ |
90
|
|
|
$this->dispatcher = $dispatcher; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @return void |
95
|
|
|
*/ |
96
|
|
|
public function prepare() |
97
|
|
|
{ |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* @param Project[] $projects |
102
|
|
|
* @param array $options |
103
|
|
|
* |
104
|
|
|
* @return array |
105
|
|
|
*/ |
106
|
|
|
public function process(array $projects, $options = array()) |
107
|
|
|
{ |
108
|
|
|
$options += array('cache' => true, 'level' => 'all', 'flat' => false); |
109
|
|
|
$reports = array(); |
110
|
|
|
|
111
|
|
|
$release_history = new ReleaseHistory($this->cache); |
112
|
|
|
|
113
|
|
|
/** @var Project $project */ |
114
|
|
|
foreach ($projects as $project) { |
115
|
|
|
$event = new CerbereDoActionEvent($this, $project); |
116
|
|
|
$this->getDispatcher()->dispatch(CerbereEvents::CERBERE_DO_ACTION, $event); |
117
|
|
|
|
118
|
|
|
$release_history->prepare($project, $options['cache']); |
119
|
|
|
$release_history->compare($project); |
120
|
|
|
|
121
|
|
|
switch ($options['level']) { |
122
|
|
|
case 'security': |
123
|
|
|
$level = ReleaseHistory::UPDATE_NOT_SECURE; |
124
|
|
|
break; |
125
|
|
|
case 'unsupported': |
126
|
|
|
$level = ReleaseHistory::UPDATE_NOT_SUPPORTED; |
127
|
|
|
break; |
128
|
|
|
case 'update': |
129
|
|
|
$level = ReleaseHistory::UPDATE_NOT_CURRENT; |
130
|
|
|
break; |
131
|
|
|
default: |
132
|
|
|
$level = ReleaseHistory::UPDATE_CURRENT; |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
if ($project->getStatus() <= $level) { |
136
|
|
|
$report = $this->generateReport($project, $release_history, $options['flat']); |
137
|
|
|
|
138
|
|
|
$event = new CerbereReportActionEvent($this, $project, $report, $options); |
139
|
|
|
$this->getDispatcher()->dispatch(CerbereEvents::CERBERE_REPORT_ACTION, $event); |
140
|
|
|
$report = $event->getReport(); |
141
|
|
|
|
142
|
|
|
$reports[$project->getProject()] = $report; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
$event = new CerbereDoneActionEvent($this, $project); |
146
|
|
|
$this->getDispatcher()->dispatch(CerbereEvents::CERBERE_DONE_ACTION, $event); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
return $reports; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param Project $project |
154
|
|
|
* @param ReleaseHistory $release_history |
155
|
|
|
* @param boolean $flat |
156
|
|
|
* @return array |
157
|
|
|
*/ |
158
|
|
|
protected function generateReport(Project $project, ReleaseHistory $release_history, $flat = false) |
159
|
|
|
{ |
160
|
|
|
$report = array( |
161
|
|
|
'project' => $project->getProject(), |
162
|
|
|
'type' => $project->getProjectType(), |
163
|
|
|
'version' => $project->getVersion(), |
164
|
|
|
'version_date' => $project->getDatestamp(), |
165
|
|
|
'recommended' => null, |
166
|
|
|
'dev' => null, |
167
|
|
|
'also_available' => array(), |
168
|
|
|
'status' => $project->getStatus(), |
169
|
|
|
'status_label' => ReleaseHistory::getStatusLabel($project->getStatus()), |
170
|
|
|
'reason' => '', |
171
|
|
|
); |
172
|
|
|
|
173
|
|
|
if ($flat) { |
174
|
|
|
$report['recommended'] = $project->getRecommended(); |
175
|
|
|
$report['dev'] = $project->getDevVersion(); |
176
|
|
|
$report['also_available'] = implode(',', $project->getAlsoAvailable()); |
177
|
|
|
} else { |
178
|
|
|
if ($release = $release_history->getRelease($project->getRecommended())) { |
179
|
|
|
$report['recommended'] = $this->getReportFromRelease($release); |
180
|
|
|
} |
181
|
|
|
|
182
|
|
|
if ($release = $release_history->getRelease($project->getDevVersion())) { |
183
|
|
|
$report['dev'] = $this->getReportFromRelease($release); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
foreach ($project->getAlsoAvailable() as $version) { |
187
|
|
|
if ($release = $project->getRelease($version)) { |
188
|
|
|
$report['also_available'][] = $this->getReportFromRelease($release); |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
if ($reason = $project->getReason()) { |
194
|
|
|
$report['reason'] = $reason; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
return $report; |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
/** |
201
|
|
|
* @param Release $release |
202
|
|
|
* @return array |
203
|
|
|
*/ |
204
|
|
|
protected function getReportFromRelease(Release $release) |
205
|
|
|
{ |
206
|
|
|
return array( |
207
|
|
|
'version' => $release->getVersion(), |
208
|
|
|
'datestamp' => $release->getDatestamp(), |
209
|
|
|
'release_link' => $release->getReleaseLink(), |
210
|
|
|
'download_link' => $release->getDownloadLink(), |
211
|
|
|
'filesize' => $release->getFilesize(), |
212
|
|
|
); |
213
|
|
|
} |
214
|
|
|
|
215
|
|
|
/** |
216
|
|
|
* @return CacheProvider |
217
|
|
|
*/ |
218
|
|
|
public function getCache() |
219
|
|
|
{ |
220
|
|
|
return $this->cache; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
/** |
224
|
|
|
* @param CacheProvider $cache |
225
|
|
|
*/ |
226
|
|
|
public function setCache(CacheProvider $cache) |
227
|
|
|
{ |
228
|
|
|
$this->cache = $cache; |
229
|
|
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public function getCode() |
235
|
|
|
{ |
236
|
|
|
return 'update'; |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
|