Dave Jarvis' Repositories

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

Eliminates conditionals in router

AuthorDave Jarvis <email>
Date2026-02-11 23:32:04 GMT-0800
Commit59861d6c2307252f4f29f5141f17e56ab36e5619
Parent2983854
Router.php
<?php
-require_once __DIR__ . '/Views.php';
+require_once __DIR__ . '/File.php';
require_once __DIR__ . '/RepositoryList.php';
require_once __DIR__ . '/git/Git.php';
require_once __DIR__ . '/git/GitDiff.php';
+require_once __DIR__ . '/render/FileRenderer.php';
+
+require_once __DIR__ . '/pages/BasePage.php';
+require_once __DIR__ . '/pages/CommitsPage.php';
require_once __DIR__ . '/pages/DiffPage.php';
+require_once __DIR__ . '/pages/HomePage.php';
+require_once __DIR__ . '/pages/FilePage.php';
+require_once __DIR__ . '/pages/RawPage.php';
require_once __DIR__ . '/pages/TagsPage.php';
class Router {
private $repositories = [];
private $git;
- public function __construct(string $reposPath) {
- $this->git = new Git($reposPath);
+ public function __construct( string $reposPath ) {
+ $this->git = new Git( $reposPath );
- $list = new RepositoryList($reposPath);
- $list->eachRepository(function($repo) {
- $this->repositories[] = $repo;
- });
+ $list = new RepositoryList( $reposPath );
+
+ $list->eachRepository( function( $repo ) {
+ $this->repos[] = $repo;
+ } );
}
public function route(): Page {
$reqRepo = $_GET['repo'] ?? '';
- $action = $_GET['action'] ?? 'home';
- $hash = $this->sanitizePath($_GET['hash'] ?? '');
+ $action = $_GET['action'] ?? 'file';
+ $hash = $this->sanitizePath( $_GET['hash'] ?? '' );
- $currentRepo = null;
- $decoded = urldecode($reqRepo);
+ $currRepo = null;
+ $decoded = urldecode( $reqRepo );
- foreach ($this->repositories as $repo) {
- if ($repo['safe_name'] === $reqRepo || $repo['name'] === $decoded) {
- $currentRepo = $repo;
+ foreach( $this->repos as $repo ) {
+ if( $repo['safe_name'] === $reqRepo ||
+ $repo['name'] === $decoded ) {
+ $currRepo = $repo;
break;
}
- }
-
- if (!$currentRepo) {
- return new HomePage($this->repositories, $this->git);
- }
-
- $this->git->setRepository($currentRepo['path']);
-
- if ($action === 'raw') {
- return new RawPage($this->git, $hash);
}
- if ($action === 'commit') {
- return new DiffPage($this->repositories, $currentRepo, $this->git, $hash);
+ if( $currRepo ) {
+ $this->git->setRepository( $currRepo['path'] );
}
- if ($action === 'commits') {
- return new CommitsPage($this->repositories, $currentRepo, $this->git, $hash);
- }
+ $routes = [
+ 'home' => fn() => new HomePage( $this->repos, $this->git ),
+ 'file' => fn() => new FilePage( $this->repos, $currRepo, $this->git, $hash ),
+ 'raw' => fn() => new RawPage( $this->git, $hash ),
+ 'commit' => fn() => new DiffPage( $this->repos, $currRepo, $this->git, $hash ),
+ 'commits' => fn() => new CommitsPage( $this->repos, $currRepo, $this->git, $hash ),
+ 'tags' => fn() => new TagsPage( $this->repos, $currRepo, $this->git ),
+ ];
- if ($action === 'tags') {
- return new TagsPage($this->repositories, $currentRepo, $this->git);
- }
+ $action = !$currRepo ? 'home' : $action;
- return new FilePage($this->repositories, $currentRepo, $this->git, $hash);
+ return ($routes[$action] ?? $routes['file'])();
}
- private function sanitizePath($path) {
- $path = str_replace(['..', '\\', "\0"], ['', '/', ''], $path);
- return preg_replace('/[^a-zA-Z0-9_\-\.\/]/', '', $path);
+ private function sanitizePath( $path ) {
+ $path = str_replace( [ '..', '\\', "\0" ], [ '', '/', '' ], $path );
+
+ return preg_replace( '/[^a-zA-Z0-9_\-\.\/]/', '', $path );
}
}
Delta39 lines added, 36 lines removed, 3-line increase