|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace phpweb\Releases\Util; |
| 4 | + |
| 5 | +use DateInterval; |
| 6 | +use DatePeriod; |
| 7 | +use DateTimeImmutable; |
| 8 | +use phpweb\Releases\VersionData; |
| 9 | +use function array_filter; |
| 10 | +use function count; |
| 11 | +use function htmlspecialchars; |
| 12 | +use function iterator_to_array; |
| 13 | +use function ksort; |
| 14 | +use function version_compare; |
| 15 | + |
| 16 | +class SupportTimelineSvgGenerator |
| 17 | +{ |
| 18 | + private const string ONLY_SHOW_ABOVE_VER = '5.3'; |
| 19 | + |
| 20 | + /** |
| 21 | + * Gets the start of the X asis on the supported versions graph |
| 22 | + */ |
| 23 | + public static function getDefaultMinimumReleaseDate(): DateTimeImmutable |
| 24 | + { |
| 25 | + return new DateTimeImmutable('January 1')->sub(new DateInterval('P3Y')); |
| 26 | + } |
| 27 | + |
| 28 | + /** |
| 29 | + * Gets the end of the X axis on the supported version graph |
| 30 | + */ |
| 31 | + public static function getDefaultMaximumReleaseDate(): DateTimeImmutable |
| 32 | + { |
| 33 | + return new DateTimeImmutable('January 1')->add(new DateInterval('P5Y')); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @return array<string, VersionData> |
| 38 | + */ |
| 39 | + public static function getDefaultReleasesToShow() |
| 40 | + { |
| 41 | + $minDate = self::getDefaultMinimumReleaseDate(); |
| 42 | + $branches = array_filter( |
| 43 | + VersionData::all(), |
| 44 | + fn(VersionData $version) => version_compare($version->label, self::ONLY_SHOW_ABOVE_VER, 'ge') |
| 45 | + && $version->securityEOL |
| 46 | + && $version->securityEOL > $minDate, |
| 47 | + ); |
| 48 | + |
| 49 | + ksort($branches); |
| 50 | + |
| 51 | + return $branches; |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * @param array<string, VersionData> $releases |
| 56 | + */ |
| 57 | + public static function drawSvg( |
| 58 | + array $releases, |
| 59 | + DateTimeImmutable $minDate, |
| 60 | + DateTimeImmutable $maxDate, |
| 61 | + int $rowHeight = 30, |
| 62 | + int $marginLeft = 80, |
| 63 | + int $marginRight = 50, |
| 64 | + int $yearWidth = 120, |
| 65 | + int $headerHeight = 24, |
| 66 | + int $footerHeight = 24, |
| 67 | + ): string { |
| 68 | + $transformX = function (DateTimeImmutable $date) use ($marginLeft, $marginRight, $minDate, $yearWidth) { |
| 69 | + $diff = $date->diff($minDate); |
| 70 | + if (!$diff->invert) { |
| 71 | + return $marginLeft; |
| 72 | + } |
| 73 | + |
| 74 | + return $marginLeft + ($diff->days / (365.24 / $yearWidth)); |
| 75 | + }; |
| 76 | + |
| 77 | + /** @var array<string, int> $topCoordinatesByReleaseLabel */ |
| 78 | + $topCoordinatesByReleaseLabel = []; |
| 79 | + $i = 0; |
| 80 | + foreach ($releases as $branch => $version) { |
| 81 | + $topCoordinatesByReleaseLabel[$branch] = $headerHeight + ($rowHeight * $i++); |
| 82 | + } |
| 83 | + |
| 84 | + $years = iterator_to_array( |
| 85 | + new DatePeriod(start: $minDate, interval: new DateInterval('P1Y'), end: $maxDate), |
| 86 | + ); |
| 87 | + |
| 88 | + $imageWidth = $marginLeft + $marginRight + ((count($years) - 1) * $yearWidth); |
| 89 | + $imageHeight = $headerHeight + $footerHeight + (count($releases) * $rowHeight); |
| 90 | + |
| 91 | + ob_start(); |
| 92 | + ?> |
| 93 | + <svg xmlns="http://www.w3.org/2000/svg" |
| 94 | + viewbox="0 0 <?= $imageWidth . ' ' . $imageHeight ?>" |
| 95 | + width="<?= $imageWidth ?>" height="<?= $imageHeight ?>" |
| 96 | + > |
| 97 | + <style type="text/css"> |
| 98 | + <![CDATA[ |
| 99 | + |
| 100 | + @import url(/fonts/Fira/fira.css); |
| 101 | + |
| 102 | + text { |
| 103 | + fill: #333; |
| 104 | + font-family: var(--font-family-sans-serif); |
| 105 | + font-size: <?php echo (2 / 3) * $headerHeight; ?>px; |
| 106 | + } |
| 107 | + |
| 108 | + g.eol rect, |
| 109 | + .branches rect.eol { |
| 110 | + fill: #f33; |
| 111 | + } |
| 112 | + |
| 113 | + g.eol text { |
| 114 | + fill: white; |
| 115 | + } |
| 116 | + |
| 117 | + g.security rect, |
| 118 | + .branches rect.security { |
| 119 | + fill: #f93; |
| 120 | + } |
| 121 | + |
| 122 | + g.stable rect, |
| 123 | + .branches rect.stable { |
| 124 | + fill: #9c9; |
| 125 | + } |
| 126 | + |
| 127 | + .branch-labels text { |
| 128 | + dominant-baseline: central; |
| 129 | + text-anchor: middle; |
| 130 | + } |
| 131 | + |
| 132 | + .today line { |
| 133 | + stroke: #333; |
| 134 | + stroke-dasharray: 7, 7; |
| 135 | + stroke-width: 3px; |
| 136 | + } |
| 137 | + |
| 138 | + .today text { |
| 139 | + fill: #333; |
| 140 | + text-anchor: middle; |
| 141 | + } |
| 142 | + |
| 143 | + .years line { |
| 144 | + stroke: black; |
| 145 | + } |
| 146 | + |
| 147 | + .years text { |
| 148 | + text-anchor: middle; |
| 149 | + } |
| 150 | + |
| 151 | + ]]> |
| 152 | + </style> |
| 153 | + |
| 154 | + <!-- Branch labels --> |
| 155 | + <g class="branch-labels"> |
| 156 | + <?php foreach ($releases as $branch => $release) { ?> |
| 157 | + <g class="<?= safe($release->supportState->value) ?>"> |
| 158 | + <rect x="0" y="<?= $topCoordinatesByReleaseLabel[$release->label] ?>" width="<?= 0.5 * $marginLeft ?>" height="<?= $rowHeight ?>"/> |
| 159 | + <text x="<?= 0.25 * $marginLeft ?>" y="<?= $topCoordinatesByReleaseLabel[$release->label] + (0.5 * $rowHeight) ?>"> |
| 160 | + <?= htmlspecialchars($branch) ?> |
| 161 | + </text> |
| 162 | + </g> |
| 163 | + <?php } ?> |
| 164 | + </g> |
| 165 | + |
| 166 | + <!-- Branch blocks --> |
| 167 | + <g class="branches"> |
| 168 | + <?php foreach ($releases as $release) { ?> |
| 169 | + <?php |
| 170 | + $x_release = $transformX($release->releaseDate); |
| 171 | + $x_bug = $transformX($release->bugfixEOL); |
| 172 | + $x_eol = $transformX($release->securityEOL); |
| 173 | + $top = $topCoordinatesByReleaseLabel[$release->label]; |
| 174 | + ?> |
| 175 | + <rect class="stable" x="<?= $x_release ?>" y="<?= $top ?>" |
| 176 | + width="<?= $x_bug - $x_release ?>" |
| 177 | + height="<?= $rowHeight ?>" |
| 178 | + /> |
| 179 | + <rect class="security" x="<?= $x_bug ?>" y="<?= $top ?>" |
| 180 | + width="<?= ($x_eol - $x_bug) ?>" |
| 181 | + height="<?= $rowHeight ?>" |
| 182 | + /> |
| 183 | + <?php } ?> |
| 184 | + </g> |
| 185 | + |
| 186 | + <!-- Year lines --> |
| 187 | + <g class="years"> |
| 188 | + <?php foreach ($years as $date) { ?> |
| 189 | + <line x1="<?=$transformX($date) ?>" |
| 190 | + y1="<?= $headerHeight ?>" |
| 191 | + x2="<?= $transformX($date) ?>" |
| 192 | + y2="<?= $headerHeight + (count($releases) * $rowHeight) ?>" |
| 193 | + /> |
| 194 | + <text x="<?= $transformX($date) ?>" |
| 195 | + y="<?= 0.8 * $headerHeight; ?>"> |
| 196 | + <?= $date->format('j M Y') ?> |
| 197 | + </text> |
| 198 | + <?php } ?> |
| 199 | + </g> |
| 200 | + |
| 201 | + <!-- Today --> |
| 202 | + <g class="today"> |
| 203 | + <?php |
| 204 | + $now = new DateTimeImmutable(); |
| 205 | + $x = $transformX($now); |
| 206 | + ?> |
| 207 | + <line x1="<?= $x ?>" y1="<?= $headerHeight ?>" |
| 208 | + x2="<?= $x ?>" |
| 209 | + y2="<?= $headerHeight + (count($releases) * $rowHeight) ?>" |
| 210 | + /> |
| 211 | + <text x="<?= $x ?>" y="<?= $headerHeight + (count($releases) * $rowHeight) + (0.8 * $footerHeight) ?>"> |
| 212 | + <?= safe('Today: ' . $now->format('j M Y')) ?> |
| 213 | + </text> |
| 214 | + </g> |
| 215 | + </svg> |
| 216 | + |
| 217 | + <?php |
| 218 | + return ob_get_clean(); |
| 219 | + } |
| 220 | +} |
0 commit comments