Dave Jarvis' Repositories

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

Moves UrlBuilder into reusable location

Author Dave Jarvis <email>
Date 2026-02-15 19:09:56 GMT-0800
Commit 5ab23c13d3de3998212299502f5d935cac3331dc
Parent 5bb5e6e
Delta 75 lines added, 66 lines removed, 9-line increase
UrlBuilder.php
+<?php
+class UrlBuilder {
+ private $repo;
+ private $action;
+ private $hash;
+ private $name;
+ private $switcher;
+
+ public function withRepo( $repo ) {
+ $this->repo = $repo;
+ return $this;
+ }
+
+ public function withAction( $action ) {
+ $this->action = $action;
+ return $this;
+ }
+
+ public function withHash( $hash ) {
+ $this->hash = $hash;
+ return $this;
+ }
+
+ public function withName( $name ) {
+ $this->name = $name;
+ return $this;
+ }
+
+ public function withSwitcher( $jsValue ) {
+ $this->switcher = $jsValue;
+ return $this;
+ }
+
+ public function build() {
+ if( $this->switcher ) {
+ $url = "window.location.href='?repo=' + encodeURIComponent(" .
+ $this->switcher . ")";
+ } else {
+ $params = [];
+
+ if( $this->repo ) {
+ $params['repo'] = $this->repo;
+ }
+
+ if( $this->action ) {
+ $params['action'] = $this->action;
+ }
+
+ if( $this->hash ) {
+ $params['hash'] = $this->hash;
+ }
+
+ if( $this->name ) {
+ $params['name'] = $this->name;
+ }
+
+ $url = empty( $params ) ? '?' : '?' . http_build_query( $params );
+ }
+
+ return $url;
+ }
+}
pages/BasePage.php
<?php
require_once __DIR__ . '/../File.php';
+require_once __DIR__ . '/../UrlBuilder.php';
require_once __DIR__ . '/Page.php';
-require_once __DIR__ . '/UrlBuilder.php';
abstract class BasePage implements Page {
pages/CommitsPage.php
<?php
require_once __DIR__ . '/BasePage.php';
+require_once __DIR__ . '/../UrlBuilder.php';
class CommitsPage extends BasePage {
pages/DiffPage.php
<?php
require_once __DIR__ . '/BasePage.php';
+require_once __DIR__ . '/../UrlBuilder.php';
require_once __DIR__ . '/../git/GitDiff.php';
pages/FilePage.php
<?php
require_once __DIR__ . '/BasePage.php';
+require_once __DIR__ . '/../UrlBuilder.php';
require_once __DIR__ . '/../render/HtmlFileRenderer.php';
pages/HomePage.php
<?php
require_once __DIR__ . '/BasePage.php';
+require_once __DIR__ . '/../UrlBuilder.php';
class HomePage extends BasePage {
pages/UrlBuilder.php
-<?php
-class UrlBuilder {
- private $repo;
- private $action;
- private $hash;
- private $name;
- private $switcher;
-
- public function withRepo( $repo ) {
- $this->repo = $repo;
- return $this;
- }
-
- public function withAction( $action ) {
- $this->action = $action;
- return $this;
- }
-
- public function withHash( $hash ) {
- $this->hash = $hash;
- return $this;
- }
-
- public function withName( $name ) {
- $this->name = $name;
- return $this;
- }
-
- public function withSwitcher( $jsValue ) {
- $this->switcher = $jsValue;
- return $this;
- }
-
- public function build() {
- if( $this->switcher ) {
- $url = "window.location.href='?repo=' + encodeURIComponent(" .
- $this->switcher . ")";
- } else {
- $params = [];
-
- if( $this->repo ) {
- $params['repo'] = $this->repo;
- }
-
- if( $this->action ) {
- $params['action'] = $this->action;
- }
-
- if( $this->hash ) {
- $params['hash'] = $this->hash;
- }
-
- if( $this->name ) {
- $params['name'] = $this->name;
- }
-
- $url = empty( $params ) ? '?' : '?' . http_build_query( $params );
- }
-
- return $url;
- }
-}
render/HtmlFileRenderer.php
require_once __DIR__ . '/FileRenderer.php';
require_once __DIR__ . '/Highlighter.php';
+require_once __DIR__ . '/../UrlBuilder.php';
class HtmlFileRenderer implements FileRenderer {
$name;
- $url = '?repo=' . urlencode( $this->repoSafeName ) .
- '&hash=' . $sha .
- '&name=' . urlencode( $fullPath );
+ // 2. Refactor: Use UrlBuilder instead of manual string concatenation
+ $url = (new UrlBuilder())
+ ->withRepo( $this->repoSafeName )
+ ->withHash( $sha )
+ ->withName( $fullPath )
+ ->build();
echo '<tr>';
}
}
+