forked from phpbb/phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmanager.php
More file actions
332 lines (281 loc) · 8.27 KB
/
manager.php
File metadata and controls
332 lines (281 loc) · 8.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
<?php
/**
*
* This file is part of the phpBB Forum Software package.
*
* @copyright (c) phpBB Limited <https://www.phpbb.com>
* @license GNU General Public License, version 2 (GPL-2.0)
*
* For full copyright and license information, please see
* the docs/CREDITS.txt file.
*
*/
namespace phpbb\composer;
use Composer\IO\IOInterface;
use phpbb\cache\driver\driver_interface;
use phpbb\composer\exception\runtime_exception;
/**
* Class to manage packages through composer.
*/
class manager implements manager_interface
{
/**
* @var installer Composer packages installer
*/
protected $installer;
/**
* @var driver_interface Cache instance
*/
protected $cache;
/**
* @var string Type of packages (phpbb-packages per example)
*/
protected $package_type;
/**
* @var string Prefix used for the exception's language string
*/
protected $exception_prefix;
/**
* @var array|null Caches the managed packages list (for the current type)
*/
private $managed_packages;
/**
* @var array|null Caches the managed packages list (for all phpBB types)
*/
private $all_managed_packages;
/**
* @var array|null Caches the available packages list
*/
private $available_packages;
/**
* @param installer $installer Installer object
* @param driver_interface $cache Cache object
* @param string $package_type Composer type of managed packages
* @param string $exception_prefix Exception prefix to use
*/
public function __construct(installer $installer, driver_interface $cache, $package_type, $exception_prefix)
{
$this->installer = $installer;
$this->cache = $cache;
$this->package_type = $package_type;
$this->exception_prefix = $exception_prefix;
}
/**
* {@inheritdoc}
*/
public function install(array $packages, IOInterface $io = null)
{
$packages = $this->normalize_version($packages);
$already_managed = array_intersect(array_keys($this->get_managed_packages()), array_keys($packages));
if (count($already_managed) !== 0)
{
throw new runtime_exception($this->exception_prefix, 'ALREADY_INSTALLED', [implode('|', $already_managed)]);
}
$this->pre_install($packages, $io);
$managed_packages = array_merge($this->get_all_managed_packages(), $packages);
ksort($managed_packages);
$this->installer->install($managed_packages, [], $io);
$this->post_install($packages, $io);
$this->managed_packages = null;
}
/**
* Hook called before installing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface|null $io IO object used for the output
*/
protected function pre_install(array $packages, IOInterface $io = null)
{
}
/**
* Hook called after installing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface|null $io IO object used for the output
*/
protected function post_install(array $packages, IOInterface $io = null)
{
}
/**
* {@inheritdoc}
*/
public function update(array $packages, IOInterface $io = null)
{
$packages = $this->normalize_version($packages);
$not_managed = array_diff_key($packages, $this->get_managed_packages());
if (count($not_managed) !== 0)
{
throw new runtime_exception($this->exception_prefix, 'NOT_MANAGED', [implode('|', array_keys($not_managed))]);
}
$this->pre_update($packages, $io);
$managed_packages = array_merge($this->get_all_managed_packages(), $packages);
ksort($managed_packages);
$this->installer->install($managed_packages, array_keys($packages), $io);
$this->post_update($packages, $io);
}
/**
* Hook called before updating the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface|null $io IO object used for the output
*/
protected function pre_update(array $packages, IOInterface $io = null)
{
}
/**
* Hook called after updating the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface|null $io IO object used for the output
*/
protected function post_update(array $packages, IOInterface $io = null)
{
}
/**
* {@inheritdoc}
*/
public function remove(array $packages, IOInterface $io = null)
{
$packages = $this->normalize_version($packages);
$not_managed = array_diff_key($packages, $this->get_managed_packages());
if (count($not_managed) !== 0)
{
throw new runtime_exception($this->exception_prefix, 'NOT_MANAGED', [implode('|', array_keys($not_managed))]);
}
$this->pre_remove($packages, $io);
$managed_packages = array_diff_key($this->get_all_managed_packages(), $packages);
ksort($managed_packages);
$this->installer->install($managed_packages, array_keys($packages), $io);
$this->post_remove($packages, $io);
$this->managed_packages = null;
}
/**
* Hook called before removing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface|null $io IO object used for the output
*/
protected function pre_remove(array $packages, IOInterface $io = null)
{
}
/**
* Hook called after removing the packages
*
* @param array $packages Packages to update.
* Each entry may be a name or an array associating a version constraint to a name
* @param IOInterface|null $io IO object used for the output
*/
protected function post_remove(array $packages, IOInterface $io = null)
{
}
/**
* {@inheritdoc}
*/
public function is_managed($package)
{
return array_key_exists($package, $this->get_managed_packages());
}
/**
* {@inheritdoc}
*/
public function get_managed_packages()
{
if ($this->managed_packages === null)
{
$this->managed_packages = $this->installer->get_installed_packages($this->package_type);
}
return $this->managed_packages;
}
/**
* {@inheritdoc}
*/
public function get_all_managed_packages()
{
if ($this->all_managed_packages === null)
{
$this->all_managed_packages = $this->installer->get_installed_packages(explode(',', installer::PHPBB_TYPES));
}
return $this->all_managed_packages;
}
/**
* {@inheritdoc}
*/
public function get_available_packages()
{
if ($this->available_packages === null)
{
$this->available_packages = $this->cache->get('_composer_' . $this->package_type . '_available');
if (!$this->available_packages)
{
$this->available_packages = $this->installer->get_available_packages($this->package_type);
$this->cache->put('_composer_' . $this->package_type . '_available', $this->available_packages, 24*60*60);
}
}
return $this->available_packages;
}
/**
* {@inheritdoc}
*/
public function reset_cache()
{
$this->cache->destroy('_composer_' . $this->package_type . '_available');
$this->available_packages = null;
$this->managed_packages = null;
$this->all_managed_packages = null;
}
/**
* {@inheritdoc}
*/
public function start_managing($package, $io)
{
throw new \phpbb\exception\runtime_exception('COMPOSER_UNSUPPORTED_OPERATION', (array) $this->package_type);
}
/**
* {@inheritdoc}
*/
public function check_requirements()
{
return $this->installer->check_requirements();
}
/**
* Normalize a packages/version array. Every entry can have 3 different forms:
* - $package => $version
* - $indice => $package:$version
* - $indice => $package
* They are converted to he form:
* - $package => $version ($version is set to '*' for the third form)
*
* @param array $packages
*
* @return array
*/
protected function normalize_version(array $packages)
{
$normalized_packages = [];
foreach ($packages as $package => $version)
{
if (is_numeric($package))
{
if (strpos($version, ':') !== false)
{
$parts = explode(':', $version);
$normalized_packages[$parts[0]] = $parts[1];
}
else
{
$normalized_packages[$version] = '*';
}
}
else
{
$normalized_packages[$package] = $version;
}
}
return $normalized_packages;
}
}