| | +<?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 ) . |
| | + ' • ' . \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; |
| | + } |
| | + } |
| | +} |