/
UpdatePackageInfo.php
122 lines (108 loc) · 3.38 KB
/
UpdatePackageInfo.php
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
<?php
namespace BringYourOwnIdeas\Maintenance\Tasks;
use BuildTask;
use BringYourOwnIdeas\Maintenance\Util\ComposerLoader;
use Injector;
use Package;
use SQLDelete;
/**
* Parses a composer lock file in order to cache information about the installation.
*/
class UpdatePackageInfo extends BuildTask
{
/**
* @var array Injector configuration
* @config
*/
private static $dependencies = [
'ComposerLoader' => '%$BringYourOwnIdeas\\Maintenance\\Util\\ComposerLoader'
];
/**
* The "types" of composer libraries that will be processed. Anything without these types will be ignored.
*
* @config
* @var array
*/
private static $allowed_types = [
'silverstripe-module',
'silverstripe-vendormodule',
];
/**
* @var ComposerLoader
*/
protected $composerLoader;
/**
* Fetch the composer loader
*
* @return ComposerLoader
*/
public function getComposerLoader()
{
return $this->composerLoader;
}
/**
* set composer loader - provided for use with Injector {@see Injector}
*
* @param ComposerLoader $composerLoader
*
* @return UpdatePackageInfo $this
*/
public function setComposerLoader($composerLoader)
{
$this->composerLoader = $composerLoader;
return $this;
}
public function getTitle()
{
return _t(__CLASS__ . '.TITLE', 'Refresh installed package info');
}
public function getDescription()
{
return _t(
__CLASS__ . '.DESCRIPTION',
'Repopulates installation summary, listing installed modules'.
' and information associated with each.'
);
}
/**
* Update database cached information about this site.
*
* @param SS_HTTPRequest $request unused, can be null (must match signature of parent function).
*/
public function run($request)
{
$packages = $this->getPackageInfo($this->getComposerLoader()->getLock()->packages);
// Extensions to the process that add data may rely on external services.
// There may be a communication issue between the site and the external service,
// so if there are 'none' we should assume this is untrue and _not_ proceed
// to remove everything. Stale information is better than no information.
if ($packages) {
// There is no onBeforeDelete for Package
SQLDelete::create(Package::class)->execute();
foreach ($packages as $package) {
Package::create()->update($package)->write();
}
}
}
/**
* Fetch information about the installed packages.
*
* @param array $packageList list of packages as objects, formatted as one finds in a composer.lock
*
* @return array indexed array of package information, represented as associative arrays.
*/
public function getPackageInfo($packageList)
{
$formatInfo = function ($package) {
// Convert object to array, with Capitalised keys
$package = get_object_vars($package);
return array_combine(
array_map('ucfirst', array_keys($package)),
$package
);
};
$packageList = array_map($formatInfo, $packageList);
$this->extend('updatePackageInfo', $packageList);
return $packageList;
}
}