Dave Jarvis' Repositories

git clone https://repo.autonoma.ca/repo/treetrek.git
<?php
require_once __DIR__ . '/BasePage.php';
require_once __DIR__ . '/../model/UrlBuilder.php';
require_once __DIR__ . '/../model/Commit.php';
require_once __DIR__ . '/../render/HtmlCommitRenderer.php';

class HomePage extends BasePage {
  private array $repositories;
  private Git   $git;

  public function __construct( array $repositories, Git $git ) {
    parent::__construct( $repositories );

    $this->repositories = $repositories;
    $this->git          = $git;
  }

  public function render(): void {
    $this->renderLayout( function() {
      echo '<h2>Repositories</h2>';

      if( empty( $this->repositories ) ) {
        echo '<div class="empty-state">No repositories found.</div>';
      } else {
        echo '<div class="repo-grid">';

        foreach( $this->repositories as $repo ) {
          $this->renderRepoCard( $repo );
        }

        echo '</div>';
      }
    } );
  }

  private function renderRepoCard( array $repo ): void {
    $this->git->setRepository( $repo['path'] );

    $stats = [ 'branches' => 0, 'tags' => 0 ];

    $this->git->eachBranch( function() use( &$stats ) {
      $stats['branches']++;
    } );

    $this->git->eachTag( function() use( &$stats ) {
      $stats['tags']++;
    } );

    $url = (new UrlBuilder())->withRepo( $repo['safe_name'] )->build();

    echo '<a href="' . $url . '" class="repo-card">';
    echo '<h3>' . htmlspecialchars( $repo['name'] ) . '</h3>';
    echo '<p class="repo-meta">';
    echo $stats['branches'] .
         ($stats['branches'] === 1 ? ' branch, ' : ' branches, ') .
         $stats['tags'] .
         ($stats['tags'] === 1 ? ' tag' : ' tags');

    if( $this->git->getMainBranch() ) {
      echo ', ';

      $this->git->history( 'HEAD', 1, function( Commit $c ) use( $repo ) {
        $renderer = new HtmlCommitRenderer( $repo['safe_name'] );

        $c->renderTime( $renderer );
      } );
    }

    echo '</p>';

    $descPath = $repo['path'] . '/description';

    if( file_exists( $descPath ) ) {
      $description = trim( file_get_contents( $descPath ) );

      if( $description !== '' ) {
        echo '<p style="margin-top: 1.5em;">' .
             htmlspecialchars( $description ) . '</p>';
      }
    }

    echo '</a>';
  }
}