Dave Jarvis' Repositories

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

abstract class BasePage implements Page {
  private $repositories;
  private $title;

  public function __construct( array $repositories, string $title = '' ) {
    $this->repositories = $repositories;
    $this->title        = $title;
  }

  protected function renderLayout(
    $contentCallback,
    array $currentRepo = []
  ) {
    $siteTitle = Config::SITE_TITLE;
    $pageTitle = $this->title === ''
      ? ''
      : ' - ' . htmlspecialchars( $this->title );

    ?>
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title><?php echo $siteTitle . $pageTitle; ?></title>
  <link rel="stylesheet"
    href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css">
  <link rel="stylesheet" href="/styles/repo.css">
</head>
<body>
<div class="container">
  <header>
    <h1><?php echo Config::SITE_TITLE; ?></h1>

    <?php if( $currentRepo ) { ?>
      <input type="checkbox" id="clone-toggle" class="clone-checkbox">
    <?php } ?>

    <nav class="nav">
      <?php if( $currentRepo ) { ?>
        <a href="<?php echo (new UrlBuilder())->build(); ?>">Home</a>
        <?php $safeName = $currentRepo['safe_name']; ?>
        <a href="<?php echo (new UrlBuilder())
          ->withRepo( $safeName )
          ->withAction( 'tree' )
          ->build(); ?>">Files</a>
        <a href="<?php echo (new UrlBuilder())
          ->withRepo( $safeName )
          ->withAction( 'commits' )
          ->build(); ?>">Commits</a>
        <a href="<?php echo (new UrlBuilder())
          ->withRepo( $safeName )
          ->withAction( 'tags' )
          ->build(); ?>">Tags</a>

        <label for="clone-toggle" class="clone-link">Clone</label>
      <?php } ?>

      <?php if( $currentRepo ) { ?>
      <div class="repo-selector">
        <label>Repository:</label>
        <select onchange="<?php echo (new UrlBuilder())
          ->withSwitcher( 'this.value' )
          ->build(); ?>">
          <?php foreach( $this->repositories as $r ) { ?>
          <option
            value="<?php echo htmlspecialchars( $r['safe_name'] ); ?>"
            <?php
            echo $r['safe_name'] === $currentRepo['safe_name']
              ? 'selected'
              : '';
            ?>>
            <?php echo htmlspecialchars( $r['name'] ); ?>
          </option>
          <?php } ?>
        </select>
      </div>
      <?php } ?>
    </nav>

    <?php if( $currentRepo ) { ?>
      <div class="clone-region">
        <div class="clone-wrapper">
          <?php
            $cloneCmd = 'git clone https://repo.autonoma.ca/repo/' .
              $currentRepo['safe_name'] . '.git';
          ?>
          <span class="clone-sizer"><?php echo htmlspecialchars( $cloneCmd ); ?></span>
          <input type="text" class="clone-input" readonly
            value="<?php echo htmlspecialchars( $cloneCmd ); ?>">
        </div>
      </div>
    <?php } ?>
  </header>

  <?php call_user_func( $contentCallback ); ?>

</div>
</body>
</html>
    <?php
  }

  protected function renderBreadcrumbs( array $repo, array $trail = [] ) {
    $repoUrl = (new UrlBuilder())->withRepo( $repo['safe_name'] )->build();

    $parts = array_merge(
      [
        '<a href="' . $repoUrl . '" class="repo-breadcrumb">' .
          htmlspecialchars( $repo['name'] ) . '</a>'
      ],
      $trail
    );

    echo '<div class="breadcrumb">' . implode( ' / ', $parts ) . '</div>';
  }
}