Skip to content

Add regex routing support with constraints and shortcuts#5

Merged
CAFernandes merged 19 commits into
mainfrom
1-feature-rfc-regex-routing-support-for-pivotphp
Jul 8, 2025
Merged

Add regex routing support with constraints and shortcuts#5
CAFernandes merged 19 commits into
mainfrom
1-feature-rfc-regex-routing-support-for-pivotphp

Conversation

@CAFernandes
Copy link
Copy Markdown
Member

  • Introduced regex-based route constraints in the routing system.
  • Added support for inline constraints, constraint shortcuts, and full regex patterns.
  • Implemented built-in shortcuts for common patterns (e.g., int, slug, uuid).
  • Enhanced RouteCache to compile patterns with regex constraints and validate them for security.
  • Added tests for various routing scenarios, including backward compatibility and performance.
  • Documented the new regex routing feature in a dedicated markdown file.

Pull Request Template

📋 Descrição

Descreva resumidamente as mudanças feitas neste PR.

🎯 Tipo de Mudança

  • 🐛 Bug fix (mudança que corrige um bug)
  • ✨ Nova feature (mudança que adiciona funcionalidade)
  • 💥 Breaking change (mudança que causa incompatibilidade)
  • 📚 Documentação (mudanças apenas na documentação)
  • 🧹 Refactoring (mudanças que não corrigem bug nem adicionam feature)
  • ⚡ Performance (mudanças que melhoram a performance)
  • 🧪 Testes (adição ou correção de testes)

🧪 Como foi testado?

Descreva os testes que você executou para verificar suas mudanças.

  • Testes unitários passando
  • Testes funcionais passando
  • Testado manualmente
  • Exemplos funcionando

📝 Checklist

  • Meu código segue as diretrizes de estilo do projeto
  • Eu fiz uma auto-revisão do meu código
  • Comentei meu código, especialmente em partes difíceis de entender
  • Fiz mudanças correspondentes na documentação
  • Minhas mudanças não geram novos warnings
  • Adicionei testes que provam que minha correção é eficaz ou que minha feature funciona
  • Testes unitários novos e existentes passam localmente com minhas mudanças

🔗 Issues Relacionadas

Fixes #1

- Introduced regex-based route constraints in the routing system.
- Added support for inline constraints, constraint shortcuts, and full regex patterns.
- Implemented built-in shortcuts for common patterns (e.g., int, slug, uuid).
- Enhanced RouteCache to compile patterns with regex constraints and validate them for security.
- Added tests for various routing scenarios, including backward compatibility and performance.
- Documented the new regex routing feature in a dedicated markdown file.
Copilot AI review requested due to automatic review settings July 8, 2025 15:21
@CAFernandes CAFernandes linked an issue Jul 8, 2025 that may be closed by this pull request

This comment was marked as outdated.

@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 17:32

This comment was marked as outdated.

@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 19:14

This comment was marked as outdated.

@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 19:42

This comment was marked as outdated.

@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 20:19

This comment was marked as outdated.

Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 20:22

This comment was marked as outdated.

…elds

- Update regex pattern to process each {} block individually (non-greedy)
- Fix RouteCache::warmup() to use consistent field names (pattern instead of compiled_pattern)
- Add has_parameters field to cached routes for proper dynamic route identification
- Add comprehensive tests for non-greedy regex behavior
- Improve anchor removal logic to preserve functional $ anchors
@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 21:46

This comment was marked as outdated.

- Extract cache retrieval logic into getFromCache()
- Create isStaticPath() for static route detection
- Split regex block processing into processRegexBlocks() and processRegexBlock()
- Extract anchor removal logic into removeRegexAnchors()
- Create countCaptureGroups() for capture group counting
- Split parameter registration into registerAnonymousParameters()
- Extract named parameter processing into processNamedParameters() and processNamedParameter()
- Create finalizePattern() for pattern finalization
- Extract dot escaping into escapeDots()
- Split caching logic into cacheStaticRoute() and cacheDynamicRoute()

This refactoring improves code readability, testability, and maintainability
while maintaining all existing functionality and performance characteristics.
…ation

- Introduced advanced route validation using regex in the routing system.
- Added shortcuts for common regex patterns (int, slug, uuid, date, etc.).
- Updated documentation to include examples of regex constraints and best practices.
- Improved performance metrics and benchmarks in the release notes.
- Created a new release overview for v1.0.1 highlighting new features and improvements.
- Updated performance analysis and report to reflect the latest optimizations.
- Enhanced unit tests for routing to ensure integrity and correctness of regex patterns.
@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 22:24

This comment was marked as outdated.

@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 22:48
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds full regex routing support by extending the routing system with inline constraints, full-regex blocks, and predefined shortcuts, while ensuring security against ReDoS. It updates the Router and RouteCache implementations, adds extensive unit and integration tests, and refreshes documentation and validation scripts.

Reviewed Changes

Copilot reviewed 33 out of 36 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/Routing/Router.php Precompiles route patterns and delegates matching to unified methods
src/Routing/RouteCache.php Refactors compilePattern into helper methods with safety checks
tests/Unit/Routing/*.php New tests covering pattern compilation, matching, and safety
tests/Integration/Routing/*.php Integration tests for end-to-end regex routing scenarios
docs/technical/routing/router.md Documents new constraint and regex-block syntax
scripts/validate_*.{php,sh} Updates to reflect renamed documentation paths
CHANGELOG.md Adds new v1.0.1 entry for regex routing feature

Comment thread src/Routing/Router.php
Comment thread src/Routing/Router.php Outdated
Comment thread src/Routing/RouteCache.php
CAFernandes and others added 2 commits July 8, 2025 19:56
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 23:11

This comment was marked as outdated.

@CAFernandes CAFernandes requested a review from Copilot July 8, 2025 23:27
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR enhances the routing system by introducing full regex support—including inline constraints, predefined shortcuts, and brace-delimited regex blocks—and ensures safe, cached compilation. It updates the router to store compiled patterns and parameters, adds extensive tests for correctness, and documents the new features.

  • Introduce regex-based constraints (:param<…>) and full regex blocks ({^…$}) in RouteCache::compilePattern.
  • Refactor Router::add and matching logic to include compiled patterns, parameter metadata, and ReDoS protection.
  • Add comprehensive unit and integration tests and update documentation/scripts to reflect directory renames and feature additions.

Reviewed Changes

Copilot reviewed 34 out of 37 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/Routing/Router.php Integrated pattern compilation into route registration and optimized matching logic
src/Routing/RouteCache.php Implemented regex block processing, parameter constraints, safety checks, and caching
tests/Unit/Routing/RouteCacheRegexTest.php New unit tests covering regex constraints, shortcuts, and security protections
tests/Integration/Routing/RegexRoutingIntegrationTest.php Integration tests for end-to-end regex routing scenarios
docs/regex-routing.md User guide for regex-based routing, constraints, and best practices
scripts/validate_project.php Updated validation logic for renamed directories and removed benchmarks
Comments suppressed due to low confidence (1)

src/Routing/Router.php:392

  • InvalidArgumentException is referenced without a leading backslash or an import, causing a potential ClassNotFoundException. Either prefix it with \ or add use InvalidArgumentException; at the top.
            throw new InvalidArgumentException("Invalid regex pattern: $pattern");

Comment thread src/Routing/Router.php
@CAFernandes CAFernandes merged commit 7f2686a into main Jul 8, 2025
5 checks passed
@CAFernandes CAFernandes deleted the 1-feature-rfc-regex-routing-support-for-pivotphp branch July 8, 2025 23:32
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.

[FEATURE] RFC: Regex Routing Support for PivotPHP

2 participants