1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SSpkS\Package; |
4
|
|
|
|
5
|
|
|
use \SSpkS\Device\DeviceList; |
6
|
|
|
use \SSpkS\Package\Package; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* SPK PackageFinder class |
10
|
|
|
*/ |
11
|
|
|
class PackageFilter |
12
|
|
|
{ |
13
|
|
|
private $config; |
14
|
|
|
private $pkgList; |
15
|
|
|
/** @var bool|string[] $filterArch Array of allowed architectures, or FALSE to ignore. */ |
16
|
|
|
private $filterArch = false; |
17
|
|
|
/** @var bool|string $filterFwVersion Target firmware version, or FALSE to ignore. */ |
18
|
|
|
private $filterFwVersion = false; |
19
|
|
|
/** @var bool|string $filterChannel Channel 'stable' or 'beta', or FALSE to ignore. */ |
20
|
|
|
private $filterChannel = false; |
21
|
|
|
/** @var bool $filterOldVersions TRUE to return unique packages with latest version only. */ |
22
|
|
|
private $filterOldVersions = false; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param \SSpkS\Config $config Config object |
26
|
|
|
* @param \SSpkS\Package\Package[] $pkgList List of Package objects to filter |
27
|
|
|
*/ |
28
|
5 |
|
public function __construct(\SSpkS\Config $config, array $pkgList) |
29
|
|
|
{ |
30
|
5 |
|
$this->config = $config; |
31
|
5 |
|
$this->pkgList = $pkgList; |
32
|
5 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* Sets the architecture to filter for. |
36
|
|
|
* |
37
|
|
|
* @param string $arch Architecture. |
38
|
|
|
*/ |
39
|
1 |
|
public function setArchitectureFilter($arch) |
40
|
|
|
{ |
41
|
|
|
// Specific corner case |
42
|
1 |
|
if ($arch == '88f6282') { |
43
|
|
|
$arch = '88f6281'; |
44
|
|
|
} |
45
|
|
|
|
46
|
1 |
|
$dl = new DeviceList($this->config); |
47
|
1 |
|
$family = $dl->getFamily($arch); |
48
|
1 |
|
$this->filterArch = array_unique(array('noarch', $arch, $family)); |
49
|
1 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Sets the firmware version to filter for. |
53
|
|
|
* |
54
|
|
|
* @param string|bool $version Firmware version in dotted notation ('1.2.3456') or FALSE to ignore. |
55
|
|
|
*/ |
56
|
1 |
|
public function setFirmwareVersionFilter($version) |
57
|
|
|
{ |
58
|
1 |
|
$this->filterFwVersion = $version; |
59
|
1 |
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Sets the channel to filter for. |
63
|
|
|
* |
64
|
|
|
* @param string $channel Channel ('stable' or 'beta') |
65
|
|
|
*/ |
66
|
1 |
|
public function setChannelFilter($channel) |
67
|
|
|
{ |
68
|
1 |
|
$this->filterChannel = $channel; |
69
|
1 |
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Enables or disables omitting older versions of the same package from the result set. |
73
|
|
|
* |
74
|
|
|
* @param bool $status TRUE to enable the filter, FALSE to disable. |
75
|
|
|
*/ |
76
|
1 |
|
public function setOldVersionFilter($status) |
77
|
|
|
{ |
78
|
1 |
|
$this->filterOldVersions = $status; |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* If filter is enabled, checks if architecture of $package is compatible to requested one. |
83
|
|
|
* |
84
|
|
|
* @param \SSpkS\Package\Package $package Package to test. |
85
|
|
|
* @return bool TRUE if matching, or FALSE. |
86
|
|
|
*/ |
87
|
5 |
|
public function isMatchingArchitecture($package) |
88
|
|
|
{ |
89
|
5 |
|
if ($this->filterArch === false) { |
90
|
4 |
|
return true; |
91
|
|
|
} |
92
|
1 |
|
$matches = array_intersect($this->filterArch, $package->arch); |
|
|
|
|
93
|
1 |
|
return (count($matches) > 0); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* If filter is enabled, checks if minimal firmware required of $package is |
98
|
|
|
* smaller or equal to system firmware. |
99
|
|
|
* |
100
|
|
|
* @param \SSpkS\Package\Package $package Package to test. |
101
|
|
|
* @return bool TRUE if matching, or FALSE. |
102
|
|
|
*/ |
103
|
5 |
|
public function isMatchingFirmwareVersion($package) |
104
|
|
|
{ |
105
|
5 |
|
if ($this->filterFwVersion === false) { |
106
|
4 |
|
return true; |
107
|
|
|
} |
108
|
1 |
|
return (version_compare($package->firmware, $this->filterFwVersion, '<=')); |
|
|
|
|
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* If filter is enabled, checks if channel of $package matches requested one. |
113
|
|
|
* 'beta' will show ALL packages, also those from 'stable'. |
114
|
|
|
* |
115
|
|
|
* @param \SSpkS\Package\Package $package Package to test. |
116
|
|
|
* @return bool TRUE if matching, or FALSE. |
117
|
|
|
*/ |
118
|
5 |
|
public function isMatchingChannel($package) |
119
|
|
|
{ |
120
|
5 |
|
if ($this->filterChannel === false) { |
121
|
4 |
|
return true; |
122
|
|
|
} |
123
|
1 |
|
if ($this->filterChannel == 'stable' && $package->isBeta() === false) { |
124
|
1 |
|
return true; |
125
|
1 |
|
} elseif ($this->filterChannel == 'beta') { |
126
|
1 |
|
return true; |
127
|
|
|
} |
128
|
1 |
|
return false; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Removes older versions of same package from $pkgList. |
133
|
|
|
* |
134
|
|
|
* @param \SSpkS\Package\Package[] $pkgList List of packages |
135
|
|
|
* @return \SSpkS\Package\Package[] List of unique packages |
136
|
|
|
*/ |
137
|
1 |
|
public function removeObsoleteVersions($pkgList) |
138
|
|
|
{ |
139
|
1 |
|
$uniqueList = array(); |
140
|
1 |
|
foreach ($pkgList as $package) { |
141
|
1 |
|
$pkgId = $package->package; |
142
|
1 |
|
if (isset($uniqueList[$pkgId]) && version_compare($uniqueList[$pkgId]->version, $package->version, '>=')) { |
143
|
1 |
|
continue; |
144
|
|
|
} |
145
|
1 |
|
$uniqueList[$pkgId] = $package; |
146
|
1 |
|
} |
147
|
1 |
|
return array_values($uniqueList); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Returns the list of packages matching the currently set filters. |
152
|
|
|
* |
153
|
|
|
* @return \SSpkS\Package\Package[] List of Package objects matching filters. |
154
|
|
|
*/ |
155
|
5 |
|
public function getFilteredPackageList() |
156
|
|
|
{ |
157
|
5 |
|
$filteredPackages = array(); |
158
|
5 |
|
foreach ($this->pkgList as $package) { |
159
|
5 |
|
if (!$this->isMatchingArchitecture($package)) { |
160
|
1 |
|
continue; |
161
|
|
|
} |
162
|
5 |
|
if (!$this->isMatchingFirmwareVersion($package)) { |
163
|
1 |
|
continue; |
164
|
|
|
} |
165
|
5 |
|
if (!$this->isMatchingChannel($package)) { |
166
|
1 |
|
continue; |
167
|
|
|
} |
168
|
5 |
|
$filteredPackages[] = $package; |
169
|
5 |
|
} |
170
|
5 |
|
if ($this->filterOldVersions) { |
171
|
|
|
// remove older versions of duplicate packages from $filteredPackages |
172
|
1 |
|
$filteredPackages = $this->removeObsoleteVersions($filteredPackages); |
173
|
1 |
|
} |
174
|
5 |
|
return $filteredPackages; |
175
|
|
|
} |
176
|
|
|
} |
177
|
|
|
|