| | 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 => '?' |
| | + }; |
| | } |
| | } |