Dave Jarvis' Repositories

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

Consolidates timestamp rendering

AuthorDave Jarvis <email>
Date2026-02-23 12:12:09 GMT-0800
Commit6eec8ef71a11b8495a430452beca1b013e360271
Parent53fde02
render/HtmlCommitRenderer.php
<?php
require_once __DIR__ . '/CommitRenderer.php';
+require_once __DIR__ . '/Renderer.php';
require_once __DIR__ . '/../model/UrlBuilder.php';
-class HtmlCommitRenderer implements CommitRenderer {
+class HtmlCommitRenderer extends Renderer implements CommitRenderer {
private string $repoSafeName;
' &bull; ' . date( 'Y-m-d', $date ) . '</span>';
echo '</div>';
- }
-
- public function renderTime( int $timestamp ): void {
- $tokens = [
- 31536000 => 'year', 2592000 => 'month', 604800 => 'week',
- 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second'
- ];
- $diff = $timestamp ? time() - $timestamp : null;
- $result = 'never';
-
- if( $diff && $diff >= 5 ) {
- foreach( $tokens as $unit => $text ) {
- if( $diff < $unit ) continue;
- $num = floor( $diff / $unit );
- $result = $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
- break;
- }
- } elseif( $diff ) {
- $result = 'just now';
- }
-
- echo $result;
}
}
render/HtmlFileRenderer.php
require_once __DIR__ . '/FileRenderer.php';
require_once __DIR__ . '/Highlighter.php';
+require_once __DIR__ . '/Renderer.php';
require_once __DIR__ . '/../model/UrlBuilder.php';
-class HtmlFileRenderer implements FileRenderer {
+class HtmlFileRenderer extends Renderer implements FileRenderer {
private string $repoSafeName;
private string $currentPath;
private string $currentRef;
- public function __construct( string $repoSafeName, string $currentPath = '', string $currentRef = 'HEAD' ) {
+ public function __construct(
+ string $repoSafeName,
+ string $currentPath = '',
+ string $currentRef = 'HEAD'
+ ) {
$this->repoSafeName = $repoSafeName;
- $this->currentPath = trim( $currentPath, '/' );
- $this->currentRef = $currentRef;
+ $this->currentPath = trim( $currentPath, '/' );
+ $this->currentRef = $currentRef;
}
public function renderListEntry(
string $name,
string $sha,
string $mode,
string $iconClass,
- int $timestamp,
- int $size
+ int $timestamp,
+ int $size
): void {
- $fullPath = ($this->currentPath === '' ? '' : $this->currentPath . '/') . $name;
+ $fullPath = ($this->currentPath === '' ? '' : $this->currentPath . '/') .
+ $name;
- $isDir = ($mode === '40000' || $mode === '040000');
+ $isDir = $mode === '40000' || $mode === '040000';
$action = $isDir ? 'tree' : 'blob';
echo '<tr>';
- echo '<td class="file-icon-cell"><i class="fas ' . $iconClass . '"></i></td>';
- echo '<td class="file-name-cell"><a href="' . $url . '">' . htmlspecialchars( $name ) . '</a></td>';
+ echo '<td class="file-icon-cell"><i class="fas ' . $iconClass .
+ '"></i></td>';
+ echo '<td class="file-name-cell"><a href="' . $url . '">' .
+ htmlspecialchars( $name ) . '</a></td>';
echo '<td class="file-mode-cell">' . $this->formatMode( $mode ) . '</td>';
- echo '<td class="file-size-cell">' . ($size > 0 ? $this->formatSize( $size ) : '') . '</td>';
+ echo '<td class="file-size-cell">' .
+ ($size > 0 ? $this->formatSize( $size ) : '') . '</td>';
echo '</tr>';
}
- public function renderMedia( File $file, string $url, string $mediaType ): bool {
+ public function renderMedia(
+ File $file,
+ string $url,
+ string $mediaType
+ ): bool {
+ $result = false;
+
if( $file->isImage() ) {
- echo '<div class="blob-content blob-content-image"><img src="' . $url . '"></div>';
- return true;
+ echo '<div class="blob-content blob-content-image">' .
+ '<img src="' . $url . '"></div>';
+ $result = true;
} elseif( $file->isVideo() ) {
- echo '<div class="blob-content blob-content-video"><video controls><source src="' . $url . '" type="' . $mediaType . '"></video></div>';
- return true;
+ echo '<div class="blob-content blob-content-video">' .
+ '<video controls><source src="' . $url . '" type="' .
+ $mediaType . '"></video></div>';
+ $result = true;
} elseif( $file->isAudio() ) {
- echo '<div class="blob-content blob-content-audio"><audio controls><source src="' . $url . '" type="' . $mediaType . '"></audio></div>';
- return true;
+ echo '<div class="blob-content blob-content-audio">' .
+ '<audio controls><source src="' . $url . '" type="' .
+ $mediaType . '"></audio></div>';
+ $result = true;
}
- return false;
+
+ return $result;
}
public function renderSize( int $bytes ): void {
echo $this->formatSize( $bytes );
- }
-
- public function highlight( string $filename, string $content, string $mediaType ): string {
- return (new Highlighter($filename, $content, $mediaType))->render();
}
-
- public function renderTime( int $timestamp ): void {
- $tokens = [
- 31536000 => 'year', 2592000 => 'month', 604800 => 'week',
- 86400 => 'day', 3600 => 'hour', 60 => 'minute', 1 => 'second'
- ];
- $diff = $timestamp ? time() - $timestamp : null;
- $result = 'never';
-
- if( $diff && $diff >= 5 ) {
- foreach( $tokens as $unit => $text ) {
- if( $diff < $unit ) continue;
- $num = floor( $diff / $unit );
- $result = $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
- break;
- }
- } elseif( $diff ) {
- $result = 'just now';
- }
- echo $result;
+ public function highlight(
+ string $filename,
+ string $content,
+ string $mediaType
+ ): string {
+ return (new Highlighter( $filename, $content, $mediaType ))->render();
}
private function formatSize( int $bytes ): string {
- $units = [ 'B', 'KB', 'MB', 'GB', 'TB' ];
- $i = 0;
+ $units = ['B', 'KB', 'MB', 'GB', 'TB'];
+ $i = 0;
while( $bytes >= 1024 && $i < count( $units ) - 1 ) {
- $bytes /= 1024; $i++;
+ $bytes /= 1024;
+ $i++;
}
return ($bytes === 0 ? 0 : round( $bytes )) . ' ' . $units[$i];
}
private function formatMode( string $mode ): string {
- switch( $mode ) {
- case '100644': return 'w';
- case '100755': return 'x';
- case '040000': return 'd';
- case '120000': return 'l';
- case '160000': return 'm';
- default: return '?';
- }
+ return match( $mode ) {
+ '100644' => 'w',
+ '100755' => 'x',
+ '040000' => 'd',
+ '120000' => 'l',
+ '160000' => 'm',
+ default => '?'
+ };
}
}
render/HtmlTagRenderer.php
<?php
require_once __DIR__ . '/TagRenderer.php';
+require_once __DIR__ . '/Renderer.php';
require_once __DIR__ . '/../model/UrlBuilder.php';
-class HtmlTagRenderer implements TagRenderer {
+class HtmlTagRenderer extends Renderer implements TagRenderer {
private string $repoSafeName;
htmlspecialchars( $author ) . '</td>';
echo '<td class="tag-time">';
+
$this->renderTime( $timestamp );
+
echo '</td>';
echo '<td class="tag-hash">';
echo '<a href="' . $diffUrl . '" class="commit-hash">' .
substr( $sha, 0, 7 ) . '</a>';
echo '</td>';
echo '</tr>';
- }
-
- public function renderTime( int $timestamp ): void {
- $output = 'never';
-
- if( $timestamp !== 0 ) {
- $diff = time() - $timestamp;
-
- $output = $diff < 5 ? 'just now' : '';
-
- if( $output === '' ) {
- $tokens = [
- 31536000 => 'year',
- 2592000 => 'month',
- 604800 => 'week',
- 86400 => 'day',
- 3600 => 'hour',
- 60 => 'minute',
- 1 => 'second'
- ];
-
- foreach( $tokens as $unit => $text ) {
- if( $diff >= $unit ) {
- $num = floor( $diff / $unit );
- $output = $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
- break;
- }
- }
- }
- }
-
- echo $output;
}
}
render/Renderer.php
+<?php
+abstract class Renderer {
+ public function renderTime( int $timestamp ): void {
+ $result = 'never';
+
+ if( $timestamp !== 0 ) {
+ $diff = time() - $timestamp;
+
+ if( $diff < 5 ) {
+ $result = 'just now';
+ } else {
+ $tokens = [
+ 31536000 => 'year',
+ 2592000 => 'month',
+ 604800 => 'week',
+ 86400 => 'day',
+ 3600 => 'hour',
+ 60 => 'minute',
+ 1 => 'second'
+ ];
+
+ foreach( $tokens as $unit => $text ) {
+ if( $diff >= $unit ) {
+ $num = floor( $diff / $unit );
+ $result = $num . ' ' . $text . ($num > 1 ? 's' : '') . ' ago';
+
+ break;
+ }
+ }
+ }
+ }
+
+ echo $result;
+ }
+}
Delta99 lines added, 110 lines removed, 11-line decrease