Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git

Extracts commit concept into class

AuthorDave Jarvis <email>
Date2026-02-22 16:19:33 GMT-0800
Commita8ea8a8e8105f88ef5f5e3fe7d12a4dae6edd47c
Parentcf436ff
Delta77 lines added, 0 lines removed, 77-line increase
render/CommitRenderer.php
+<?php
+interface CommitRenderer {
+ public function renderRow(
+ string $sha,
+ string $message,
+ string $author,
+ int $date
+ ): void;
+ public function renderTime( int $timestamp ): void;
+}
render/HtmlCommitRenderer.php
+<?php
+require_once __DIR__ . '/CommitRenderer.php';
+require_once __DIR__ . '/../UrlBuilder.php';
+
+class HtmlCommitRenderer implements CommitRenderer {
+ private string $repoSafeName;
+
+ public function __construct( string $repoSafeName ) {
+ $this->repoSafeName = $repoSafeName;
+ }
+
+ public function renderRow(
+ string $sha,
+ string $message,
+ string $author,
+ int $date
+ ): void {
+ $msg = \htmlspecialchars( \explode( "\n", $message )[0] );
+ $url = (new UrlBuilder())
+ ->withRepo( $this->repoSafeName )
+ ->withAction( 'commit' )
+ ->withHash( $sha )
+ ->build();
+
+ echo '<div class="commit-row">';
+ echo '<a href="' . $url . '" class="sha">' .
+ \substr( $sha, 0, 7 ) . '</a>';
+ echo '<span class="message">' . $msg . '</span>';
+ echo '<span class="meta">' . \htmlspecialchars( $author ) .
+ ' &bull; ' . \date( 'Y-m-d', $date ) . '</span>';
+ echo '</div>';
+ }
+
+ public function renderTime( int $timestamp ): void {
+ if( !$timestamp ) {
+ echo 'never';
+ return;
+ }
+
+ $diff = \time() - $timestamp;
+
+ if( $diff < 5 ) {
+ echo 'just now';
+ return;
+ }
+
+ $tokens = [
+ 31536000 => 'year',
+ 2592000 => 'month',
+ 604800 => 'week',
+ 86400 => 'day',
+ 3600 => 'hour',
+ 60 => 'minute',
+ 1 => 'second'
+ ];
+
+ foreach( $tokens as $unit => $text ) {
+ if( $diff < $unit ) {
+ continue;
+ }
+
+ $num = \floor( $diff / $unit );
+ echo $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
+ return;
+ }
+ }
+}