Skip to content

refactor(router): extract Router into RouteBuilder, RouteMatcher, and RouteDispatcher#376

Merged
usernane merged 1 commit into
devfrom
refactor/router-extract-classes
May 30, 2026
Merged

refactor(router): extract Router into RouteBuilder, RouteMatcher, and RouteDispatcher#376
usernane merged 1 commit into
devfrom
refactor/router-extract-classes

Conversation

@usernane

Copy link
Copy Markdown
Member

Summary

Extract the 1632-line Router.php into three focused classes: RouteBuilder, RouteMatcher, and RouteDispatcher, while keeping the public static API fully backward compatible.

Motivation

The Router class had grown to 1632 lines mixing route construction, URI matching, and resource dispatching concerns. This makes it hard to maintain and test individual responsibilities. Refs #375.

Changes

  • Added RouteBuilder class: route construction, option validation, sub-route grouping, middleware group assignment
  • Added RouteMatcher class: URI resolution and matching against registered static/variable routes
  • Added RouteDispatcher class: dispatching matched routes to resources (closures, classes, files), middleware execution, redirect handling
  • Refactored Router to delegate to the three new classes while retaining its full public static API

How to Test / Verify

All existing unit tests pass without modification:

composer test10
# Tests: 898, Assertions: 2940, Skipped: 20 (MSSQL connection unavailable)

Breaking Changes and Migration Steps

None. The public API of Router is unchanged. All static methods (addRoute, api, closure, page, route, routes, etc.) work identically.

Checklist

  • I reviewed my own diff before requesting review
  • My commits follow Conventional Commits
  • I added/updated tests (or explained why not) — no new tests needed; all 898 existing tests pass, confirming backward compatibility
  • I updated docs (if needed) Docs Repo — no doc changes needed; public API unchanged
  • I ran lint/cs-fixer (if applicable) (composer fix-cs)
  • I considered backward compatibility — public API fully preserved
  • I considered security — no security-sensitive changes

Related issues

Refs #375

… RouteDispatcher

Extract the 1632-line Router class into three focused classes:

- RouteBuilder: route construction, option validation, sub-route grouping
- RouteMatcher: URI resolution and matching against registered routes
- RouteDispatcher: dispatching matched routes to resources, middleware execution

Router retains its full public static API and delegates internally,
ensuring full backward compatibility.

Refs #375
@usernane usernane merged commit 7e8e865 into dev May 30, 2026
1 check passed
@codecov

codecov Bot commented May 30, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 93.02326% with 3 lines in your changes missing coverage. Please review.
✅ Project coverage is 83.41%. Comparing base (693386e) to head (e6f6e15).
⚠️ Report is 2 commits behind head on dev.

Files with missing lines Patch % Lines
WebFiori/Framework/Router/Router.php 93.02% 3 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff              @@
##                dev     #376      +/-   ##
============================================
+ Coverage     79.26%   83.41%   +4.14%     
+ Complexity     3251     2523     -728     
============================================
  Files           105       86      -19     
  Lines          9642     7789    -1853     
============================================
- Hits           7643     6497    -1146     
+ Misses         1999     1292     -707     
Flag Coverage Δ
php-8.2 ?
php-8.3 83.41% <93.02%> (+0.84%) ⬆️
php-8.4 ?
php-8.5 ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant