deployphp /
deployer
| 1 | <?php |
||
| 2 | |||
| 3 | declare(strict_types=1); |
||
| 4 | |||
| 5 | namespace Deployer\Component\PharUpdate; |
||
| 6 | |||
| 7 | use Deployer\Component\PharUpdate\Version\Comparator; |
||
| 8 | use Deployer\Component\PharUpdate\Version\Parser; |
||
| 9 | use Deployer\Component\PharUpdate\Version\Version; |
||
| 10 | |||
| 11 | /** |
||
| 12 | * Manages the contents of an updates manifest file. |
||
| 13 | * |
||
| 14 | * @author Kevin Herrera <[email protected]> |
||
| 15 | */ |
||
| 16 | class Manifest |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * The list of updates in the manifest. |
||
| 20 | * |
||
| 21 | * @var Update[] |
||
| 22 | */ |
||
| 23 | private $updates; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * Sets the list of updates from the manifest. |
||
| 27 | * |
||
| 28 | * @param Update[] $updates The updates. |
||
| 29 | */ |
||
| 30 | public function __construct(array $updates = []) |
||
| 31 | { |
||
| 32 | $this->updates = $updates; |
||
| 33 | } |
||
| 34 | |||
| 35 | /** |
||
| 36 | * Finds the most recent update and returns it. |
||
| 37 | * |
||
| 38 | * @param Version $version The current version. |
||
| 39 | * @param boolean $major Lock to major version? |
||
| 40 | * @param boolean $pre Allow pre-releases? |
||
| 41 | */ |
||
| 42 | public function findRecent(Version $version, bool $major = false, bool $pre = false): ?Update |
||
| 43 | { |
||
| 44 | /** @var Update|null */ |
||
| 45 | $current = null; |
||
| 46 | $major = $major ? $version->getMajor() : null; |
||
| 47 | |||
| 48 | foreach ($this->updates as $update) { |
||
| 49 | if ($major && ($major !== $update->getVersion()->getMajor())) { |
||
|
0 ignored issues
–
show
|
|||
| 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(): array |
||
| 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 | public static function load(string $json): self |
||
| 86 | { |
||
| 87 | return self::create(json_decode($json)); |
||
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * Loads the manifest from a JSON encoded file. |
||
| 92 | * |
||
| 93 | * @param string $file The JSON encoded file. |
||
| 94 | */ |
||
| 95 | public static function loadFile(string $file): self |
||
| 96 | { |
||
| 97 | return self::create(json_decode(file_get_contents($file))); |
||
| 98 | } |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Validates the data, processes it, and returns a new instance of Manifest. |
||
| 102 | * |
||
| 103 | * @param array $decoded The decoded JSON data. |
||
| 104 | * |
||
| 105 | * @return static The new instance. |
||
| 106 | */ |
||
| 107 | private static function create(array $decoded): self |
||
| 108 | { |
||
| 109 | $updates = []; |
||
| 110 | |||
| 111 | foreach ($decoded as $update) { |
||
| 112 | $updates[] = new Update( |
||
| 113 | $update->name, |
||
| 114 | $update->sha1, |
||
| 115 | $update->url, |
||
| 116 | Parser::toVersion($update->version), |
||
| 117 | $update->publicKey ?? null, |
||
| 118 | ); |
||
| 119 | } |
||
| 120 | |||
| 121 | usort( |
||
| 122 | $updates, |
||
| 123 | function (Update $a, Update $b) { |
||
| 124 | return Comparator::isGreaterThan( |
||
| 125 | $a->getVersion(), |
||
| 126 | $b->getVersion(), |
||
| 127 | ) ? 1 : 0; |
||
| 128 | }, |
||
| 129 | ); |
||
| 130 | |||
| 131 | return new static($updates); |
||
| 132 | } |
||
| 133 | } |
||
| 134 |
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
integervalues, zero is a special case, in particular the following results might be unexpected: