1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Describes a installed composer package version. |
4
|
|
|
* |
5
|
|
|
* This is a DataObject to allow saving the information in the db to use the information in various ways. |
6
|
|
|
*/ |
7
|
|
|
class ComposerPackageVersion extends DataObject |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
/** |
10
|
|
|
* @var array |
11
|
|
|
*/ |
12
|
|
|
private static $db = array( |
|
|
|
|
13
|
|
|
'Scope' => "Enum('Global,Project','Project')", |
14
|
|
|
'Package' => 'Varchar(255)', |
15
|
|
|
'Version' => 'Varchar(255)', |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var array |
20
|
|
|
*/ |
21
|
|
|
private static $summary_fields = array( |
|
|
|
|
22
|
|
|
'Scope', |
23
|
|
|
'Package', |
24
|
|
|
'Version', |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* requireDefaultRecords() gets abused to update the information on dev/build. |
29
|
|
|
*/ |
30
|
|
|
public function requireDefaultRecords() |
31
|
|
|
{ |
32
|
|
|
parent::requireDefaultRecords(); |
33
|
|
|
|
34
|
|
|
// delete all records of this object |
35
|
|
|
foreach (self::get() as $package) { |
36
|
|
|
$package->delete(); |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
// generate the records new |
40
|
|
|
$globalPackages = $this->getPackageVersions(true); |
41
|
|
|
$packages = $this->getPackageVersions(); |
42
|
|
|
|
43
|
|
|
// write all the package information |
44
|
|
|
foreach (array_merge($globalPackages, $packages) as $package) { |
45
|
|
|
$instance = self::create($package); |
46
|
|
|
$instance->write(); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* returns a prepared list of the packages. |
52
|
|
|
* |
53
|
|
|
* @param bool $global (default: false) |
54
|
|
|
* |
55
|
|
|
* @return array |
56
|
|
|
*/ |
57
|
|
|
public function getPackageVersions($global = false) |
58
|
|
|
{ |
59
|
|
|
// different commands for project specific and global packages |
60
|
|
|
if ($global) { |
61
|
|
|
exec('php ../vendor/composer/composer/bin/composer global show --installed 2> /dev/null', $packages); |
62
|
|
|
} else { |
63
|
|
|
exec('php ../vendor/composer/composer/bin/composer show --installed --working-dir=.. 2> /dev/null', $packages); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
// prepare the array |
67
|
|
|
return array_map(function ($package) use (&$global) { |
68
|
|
|
// some regex to parse the package string |
69
|
|
|
$elements = preg_split('/\s+/', $package); |
70
|
|
|
|
71
|
|
|
// prep a new array. |
72
|
|
|
$match = array( |
73
|
|
|
'Scope' => ($global) ? 'Global' : 'Project', |
74
|
|
|
'Package' => $elements[0], |
75
|
|
|
); |
76
|
|
|
|
77
|
|
|
// ensure to keep the hash if it has been defined |
78
|
|
|
if (true |
79
|
|
|
&& array_key_exists(2, $elements) |
80
|
|
|
&& strlen($elements[2]) == 7 |
81
|
|
|
&& strtolower($elements[2]) == $elements[2] |
82
|
|
|
) { |
83
|
|
|
$match['Version'] = $elements[1].' '.$elements[2]; |
84
|
|
|
} else { |
85
|
|
|
$match['Version'] = $elements[1]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
// return |
89
|
|
|
return $match; |
90
|
|
|
}, $packages); |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.