1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Deployer\Component\PharUpdate; |
4
|
|
|
|
5
|
|
|
use Deployer\Component\PharUpdate\Version\Comparator; |
6
|
|
|
use Deployer\Component\PharUpdate\Version\Parser; |
7
|
|
|
use Deployer\Component\PharUpdate\Version\Version; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Manages the contents of an updates manifest file. |
11
|
|
|
* |
12
|
|
|
* @author Kevin Herrera <[email protected]> |
13
|
|
|
*/ |
14
|
|
|
class Manifest |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* The list of updates in the manifest. |
18
|
|
|
* |
19
|
|
|
* @var Update[] |
20
|
|
|
*/ |
21
|
|
|
private $updates; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Sets the list of updates from the manifest. |
25
|
|
|
* |
26
|
|
|
* @param Update[] $updates The updates. |
27
|
|
|
*/ |
28
|
|
|
public function __construct(array $updates = array()) |
29
|
|
|
{ |
30
|
|
|
$this->updates = $updates; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Finds the most recent update and returns it. |
35
|
|
|
* |
36
|
|
|
* @param Version $version The current version. |
37
|
|
|
* @param boolean $major Lock to major version? |
38
|
|
|
* @param boolean $pre Allow pre-releases? |
39
|
|
|
* |
40
|
|
|
* @return Update The update. |
41
|
|
|
*/ |
42
|
|
|
public function findRecent(Version $version, $major = false, $pre = false) |
43
|
|
|
{ |
44
|
|
|
/** @var $current Update */ |
45
|
|
|
$current = null; |
46
|
|
|
$major = $major ? $version->getMajor() : null; |
47
|
|
|
|
48
|
|
|
foreach ($this->updates as $update) { |
49
|
|
|
if ($major && ($major !== $update->getVersion()->getMajor())) { |
|
|
|
|
50
|
|
|
continue; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ((false === $pre) |
54
|
|
|
&& !$update->getVersion()->isStable()) { |
55
|
|
|
continue; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
$test = $current ? $current->getVersion() : $version; |
59
|
|
|
|
60
|
|
|
if (false === $update->isNewer($test)) { |
61
|
|
|
continue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
$current = $update; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
return $current; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Returns the list of updates in the manifest. |
72
|
|
|
* |
73
|
|
|
* @return Update[] The updates. |
74
|
|
|
*/ |
75
|
|
|
public function getUpdates() |
76
|
|
|
{ |
77
|
|
|
return $this->updates; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Loads the manifest from a JSON encoded string. |
82
|
|
|
* |
83
|
|
|
* @param string $json The JSON encoded string. |
84
|
|
|
* |
85
|
|
|
* @return Manifest The manifest. |
86
|
|
|
*/ |
87
|
|
|
public static function load($json) |
88
|
|
|
{ |
89
|
|
|
return self::create(json_decode($json)); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Loads the manifest from a JSON encoded file. |
94
|
|
|
* |
95
|
|
|
* @param string $file The JSON encoded file. |
96
|
|
|
* |
97
|
|
|
* @return Manifest The manifest. |
98
|
|
|
*/ |
99
|
|
|
public static function loadFile($file) |
100
|
|
|
{ |
101
|
|
|
return self::create(json_decode(file_get_contents($file))); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* Validates the data, processes it, and returns a new instance of Manifest. |
106
|
|
|
* |
107
|
|
|
* @param array $decoded The decoded JSON data. |
108
|
|
|
* |
109
|
|
|
* @return Manifest The new instance. |
110
|
|
|
*/ |
111
|
|
|
private static function create($decoded) |
112
|
|
|
{ |
113
|
|
|
$updates = array(); |
114
|
|
|
|
115
|
|
|
foreach ($decoded as $update) { |
116
|
|
|
$updates[] = new Update( |
117
|
|
|
$update->name, |
118
|
|
|
$update->sha1, |
119
|
|
|
$update->url, |
120
|
|
|
Parser::toVersion($update->version), |
121
|
|
|
isset($update->publicKey) ? $update->publicKey : null |
122
|
|
|
); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
usort( |
126
|
|
|
$updates, |
127
|
|
|
function (Update $a, Update $b) { |
128
|
|
|
return Comparator::isGreaterThan( |
129
|
|
|
$a->getVersion(), |
130
|
|
|
$b->getVersion() |
131
|
|
|
); |
132
|
|
|
} |
133
|
|
|
); |
134
|
|
|
|
135
|
|
|
return new static($updates); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: