Dave Jarvis' Repositories

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

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

  public function __construct( array $repositories, Git $git ) {
    parent::__construct( $repositories );
    $this->repositories = $repositories;
    $this->git          = $git;
  }

  public function render() {
    $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( $repo ) {
    $this->git->setRepository( $repo['path'] );

    $main  = $this->git->getMainBranch();
    $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">';

    $branchLabel = $stats['branches'] === 1 ? 'branch' : 'branches';
    $tagLabel    = $stats['tags'] === 1 ? 'tag' : 'tags';

    echo $stats['branches'] . ' ' . $branchLabel . ', ' .
         $stats['tags'] . ' ' . $tagLabel;

    if( $main ) {
      echo ', ';

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

    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>';
  }
}