Skip to content

Commit 98a8d05

Browse files
committed
Include state detection in VersionData
1 parent adb8f89 commit 98a8d05

3 files changed

Lines changed: 70 additions & 38 deletions

File tree

releases/states.php

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3,32 +3,22 @@
33
# Please use /releases/branches.php instead.
44
# This API *may* be removed at an indeterminate point in the future.
55

6-
$_SERVER['BASE_PAGE'] = 'releases/active.php';
6+
use phpweb\Releases\VersionData;
77

8+
$_SERVER['BASE_PAGE'] = 'releases/active.php';
89
include_once __DIR__ . '/../include/prepend.inc';
9-
include_once $_SERVER['DOCUMENT_ROOT'] . '/include/branches.inc';
1010

1111
header('Content-Type: application/json; charset=UTF-8');
1212

1313
$states = [];
1414

15-
function formatDate($date = null) {
16-
return $date !== null ? $date->format('c') : null;
15+
foreach (VersionData::all() as $release) {
16+
$states[$release->majorVersion][$release->label] = [
17+
'state' => $release->supportState->value,
18+
'initial_release' => $release->releaseDate?->format('c'),
19+
'active_support_end' => $release->bugfixEOL?->format('c'),
20+
'security_support_end' => $release->securityEOL?->format('c'),
21+
];
1722
}
1823

19-
foreach (get_all_branches() as $major => $releases) {
20-
$states[$major] = [];
21-
foreach ($releases as $branch => $release) {
22-
$states[$major][$branch] = [
23-
'state' => get_branch_support_state($branch),
24-
'initial_release' => formatDate(get_branch_release_date($branch)),
25-
'active_support_end' => formatDate(get_branch_bug_eol_date($branch)),
26-
'security_support_end' => formatDate(get_branch_security_eol_date($branch)),
27-
];
28-
}
29-
krsort($states[$major]);
30-
}
31-
32-
krsort($states);
33-
3424
echo json_encode($states);

src/Releases/SupportState.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace phpweb\Releases;
4+
5+
enum SupportState: string
6+
{
7+
case Future = 'future';
8+
case Stable = 'stable';
9+
case Security = 'security';
10+
case Eol = 'eol';
11+
case Unknown = 'unknown';
12+
}

src/Releases/VersionData.php

Lines changed: 49 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,21 @@
22

33
namespace phpweb\Releases;
44

5+
use DateTime;
56
use DateTimeImmutable;
67
use phpweb\Build\VarCache;
78
use function array_key_last;
89
use function array_reverse;
910
use function get_active_branches;
10-
use function get_all_branches;
1111
use function get_branch_bug_eol_date;
12+
use function get_branch_release_date;
1213
use function get_branch_security_eol_date;
1314

15+
/* have to force these files to be included */
16+
require_once __DIR__ . '/../../include/version.inc';
17+
require_once __DIR__ . '/../../include/releases.inc';
18+
require_once __DIR__ . '/../../include/branches.inc';
19+
1420
final class VersionData
1521
{
1622
/** @var array<string, VersionData> */
@@ -22,8 +28,12 @@ final class VersionData
2228
public readonly int $majorVersion;
2329
public readonly int $minorVersion;
2430

31+
/*
32+
* Cached values that have expensive lookups
33+
*/
2534
private ?DateTimeImmutable $_bugfixEOL = null;
2635
private ?DateTimeImmutable $_securityEOL = null;
36+
private ?DateTimeImmutable $_releaseDate = null;
2737
private ?array $_pointReleases = null;
2838
private ?array $_highlightSnippets = null;
2939

@@ -64,34 +74,29 @@ public static function getSupportedVersions(bool $includeRecentEOL = true): arra
6474
public static function all(): array
6575
{
6676
if (self::$_all === null) {
67-
foreach (get_all_branches() as $branch) {
68-
self::$_all[$branch] = self::Create($branch['version']);
77+
foreach (array_keys(ReleaseRawDataLoader::allReleaseDataByVersionByLabel()) as $releaseId) {
78+
self::$_all[$releaseId] = self::Create($releaseId);
6979
}
7080
}
7181

7282
return self::$_all;
7383
}
7484

75-
public ?DateTimeImmutable $bugfixEOL {
76-
get {
77-
if ($this->_bugfixEOL === null) {
78-
$eol = get_branch_bug_eol_date($this->label);
79-
$this->_bugfixEOL = $eol ? DateTimeImmutable::createFromInterface($eol) : null;
80-
}
85+
private static function castDate(?DateTime $date): ?DateTimeImmutable
86+
{
87+
return $date ? DateTimeImmutable::createFromInterface($date) : null;
88+
}
8189

82-
return $this->_bugfixEOL;
83-
}
90+
public ?DateTimeImmutable $releaseDate {
91+
get => $this->_releaseDate ??= self::castDate(get_branch_release_date($this->label));
8492
}
8593

86-
public ?DateTimeImmutable $securityEOL {
87-
get {
88-
if ($this->_securityEOL === null) {
89-
$eol = get_branch_security_eol_date($this->label);
90-
$this->_securityEOL = $eol ? DateTimeImmutable::createFromInterface($eol) : null;
91-
}
94+
public ?DateTimeImmutable $bugfixEOL {
95+
get => $this->_bugfixEOL ??= self::castDate(get_branch_bug_eol_date($this->label));
96+
}
9297

93-
return $this->_securityEOL;
94-
}
98+
public ?DateTimeImmutable $securityEOL {
99+
get => $this->_securityEOL ??= self::castDate(get_branch_security_eol_date($this->label));
95100
}
96101

97102
public string $logoUrl {
@@ -174,4 +179,29 @@ public function getMajorFeatureDescriptions(): array
174179
public string $downloadUri {
175180
get => '/downloads.php?version=' . $this->label;
176181
}
182+
183+
public SupportState $supportState {
184+
get {
185+
/* version 3 doesn't really exist in the data, so we assign it EOL */
186+
if ($this->majorVersion <= 3) {
187+
return SupportState::Eol;
188+
}
189+
190+
$now = new DateTime();
191+
192+
if ($this->securityEOL && $now >= $this->securityEOL) {
193+
return SupportState::Eol;
194+
}
195+
196+
if ($this->bugfixEOL && $now >= $this->bugfixEOL) {
197+
return SupportState::Security;
198+
}
199+
200+
if ($this->releaseDate && $now >= $this->releaseDate) {
201+
return SupportState::Stable;
202+
}
203+
204+
return SupportState::Future;
205+
}
206+
}
177207
}

0 commit comments

Comments
 (0)