Skip to content

Add attributes support#196

Open
rustamwin wants to merge 48 commits intomasterfrom
add-attributes
Open

Add attributes support#196
rustamwin wants to merge 48 commits intomasterfrom
add-attributes

Conversation

@rustamwin
Copy link
Copy Markdown
Member

@rustamwin rustamwin commented Mar 28, 2023

Q A
Is bugfix? ✔️/❌
New feature? ✔️
Breaks BC? ✔️
Fixed issues comma-separated list of tickets # fixed by the PR, if any

ℹ️ yiisoft/demo#589

@what-the-diff
Copy link
Copy Markdown

what-the-diff bot commented Mar 28, 2023

PR Summary

  • 🚀 Optimized Middleware Handling
    Improved the way middlewares are managed in the Group class to avoid building them multiple times when they don't change, boosting efficiency.
  • 🧹 Code Clean-up
    Removed unused classes and fixed some typos for better readability and maintenance.
  • 🛠️ Refactoring
    Refactored various methods, attributes, and typehints for improved consistency, readability, and functionality.
  • Improved Testing
    Enhanced the tests for the Group and RouteCollection classes, and fixed the bug in the RouteCollectionTest.
  • 💡 Convenience Enhancements
    Added the withMiddlewares method to the DispatcherInterface for easier usage in certain cases.

@codecov
Copy link
Copy Markdown

codecov bot commented Mar 28, 2023

Codecov Report

❌ Patch coverage is 99.50739% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 99.83%. Comparing base (1e9bfd5) to head (bb2a333).

Files with missing lines Patch % Lines
src/Provider/FileRoutesProvider.php 97.36% 1 Missing ⚠️
Additional details and impacted files
@@              Coverage Diff              @@
##              master     #196      +/-   ##
=============================================
- Coverage     100.00%   99.83%   -0.17%     
- Complexity       129      186      +57     
=============================================
  Files             13       23      +10     
  Lines            400      589     +189     
=============================================
+ Hits             400      588     +188     
- Misses             0        1       +1     

☔ 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.

@rustamwin rustamwin changed the title Add attributes support Add attributes support & improve MatchingResult Mar 28, 2023
@samdark samdark mentioned this pull request Apr 2, 2023
@rustamwin rustamwin added status:code review The pull request needs review. and removed status:under development Someone is working on a pull request. labels Jun 15, 2023
@rustamwin rustamwin requested a review from a team June 15, 2023 14:40
@vjik vjik added status:under development Someone is working on a pull request. and removed status:code review The pull request needs review. labels Jun 16, 2023
rustamwin and others added 5 commits October 21, 2025 22:27
# Conflicts:
#	.phpstorm.meta.php/Group.php
#	.phpstorm.meta.php/Route.php
#	src/Group.php
#	src/MatchingResult.php
#	src/Middleware/Router.php
#	src/Route.php
#	tests/GroupTest.php
#	tests/RouteCollectionTest.php
#	tests/RouteTest.php
@rustamwin rustamwin added status:code review The pull request needs review. and removed status:under development Someone is working on a pull request. labels Oct 22, 2025
@rustamwin rustamwin requested a review from a team October 22, 2025 09:24
@rustamwin rustamwin changed the title Add attributes support & improve MatchingResult Add attributes support Oct 27, 2025
@vjik
Copy link
Copy Markdown
Member

vjik commented Nov 2, 2025

Waiting #225

Copy link
Copy Markdown

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 introduces route declaration via PHP attributes and adds a provider abstraction for supplying routes from different sources (arrays and PHP files/directories). It also refactors Route and Group into more DTO-like objects with public constructors and additional validation, and extends the test suite accordingly.

Changes:

  • Add PHP attribute classes (Get, Post, etc.) to model routes as native PHP attributes.
  • Add RoutesProviderInterface plus ArrayRoutesProvider and FileRoutesProvider, and extend RouteCollector to accept providers.
  • Refactor Route and Group constructors (public, validation, caching tweaks) and add/adjust tests + fixtures.

Reviewed changes

Copilot reviewed 39 out of 40 changed files in this pull request and generated 11 comments.

Show a summary per file
File Description
tests/Support/resources/test.php Adds a simple test fixture file.
tests/Support/resources/scope/scope_routes.php Adds scoped route fixture to test providing routes with extracted variables.
tests/Support/resources/routes.php Adds routes fixture used by provider tests.
tests/Support/resources/mixed_dir/valid_routes.php Adds valid PHP routes fixture for directory scanning tests.
tests/Support/resources/mixed_dir/not_routes.txt Adds non-PHP fixture to verify directory scanning ignores it.
tests/Support/resources/foo.php Adds fixture returning non-routes to validate provider filtering.
tests/Support/TestController.php Adds attribute usage examples with #[Group] and #[Get].
tests/RouteTest.php Adds/updates tests for new Route constructor behavior and validation.
tests/RouteCollectorTest.php Adds tests for collecting routes from providers.
tests/RouteCollectionTest.php Mostly formatting adjustments in chained calls.
tests/Provider/FileRoutesProviderTest.php Adds tests for file/directory-based route provider behavior.
tests/Provider/ArrayRoutesProviderTest.php Adds tests for array-based route provider behavior.
tests/GroupTest.php Adds tests for Group constructor validation and caching behavior.
tests/Attribute/RouteTest.php Adds tests for generic #[Route] attribute.
tests/Attribute/PutTest.php Adds tests for #[Put] attribute.
tests/Attribute/PostTest.php Adds tests for #[Post] attribute.
tests/Attribute/PatchTest.php Adds tests for #[Patch] attribute.
tests/Attribute/OptionsTest.php Adds tests for #[Options] attribute.
tests/Attribute/HeadTest.php Adds tests for #[Head] attribute.
tests/Attribute/GetTest.php Adds tests for #[Get] attribute.
tests/Attribute/DeleteTest.php Adds tests for #[Delete] attribute.
src/RouteCollector.php Adds route providers support in the collector.
src/RouteCollection.php Small docblock wording adjustment.
src/Route.php Makes constructor public, adds validation and defaults normalization, adjusts cache invalidation.
src/Provider/RoutesProviderInterface.php Introduces the provider interface contract.
src/Provider/FileRoutesProvider.php Implements loading routes from a file or directory.
src/Provider/ArrayRoutesProvider.php Implements loading routes from an in-memory array.
src/MatchingResult.php Adds a Psalm assertion for route() nullability.
src/Group.php Makes constructor public, adds validation, and marks Group as a PHP attribute.
src/Attribute/RouteAttributeInterface.php Introduces an interface for route attribute classes.
src/Attribute/Route.php Adds a generic #[Route] attribute implementation.
src/Attribute/Put.php Adds #[Put] attribute implementation.
src/Attribute/Post.php Adds #[Post] attribute implementation.
src/Attribute/Patch.php Adds #[Patch] attribute implementation.
src/Attribute/Options.php Adds #[Options] attribute implementation.
src/Attribute/Head.php Adds #[Head] attribute implementation.
src/Attribute/Get.php Adds #[Get] attribute implementation.
src/Attribute/Delete.php Adds #[Delete] attribute implementation.
README.md Documents support for attribute-based route declaration.
CHANGELOG.md Adds release notes for attributes/providers/DTO refactor.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +44 to +53
$files = new CallbackFilterIterator(
new FilesystemIterator(
$this->file,
/** @infection-ignore-all Bitwise flags; CallbackFilterIterator already filters by extension */
FilesystemIterator::SKIP_DOTS | FilesystemIterator::FOLLOW_SYMLINKS,
),
fn(SplFileInfo $fileInfo) => $fileInfo->isFile() && $fileInfo->getExtension() === 'php',
);
/** @var SplFileInfo[] $files */
foreach ($files as $file) {
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

When a directory is provided, iteration order of FilesystemIterator is filesystem-dependent. Because route order typically affects matching/precedence, the resulting route set can become non-deterministic across environments. Consider collecting filenames and sorting them (or otherwise enforcing a stable order) before requiring files.

Copilot uses AI. Check for mistakes.
Comment on lines 49 to +55
/**
* @param string[] $methods
* @param array|callable|string|null $action Action handler. It is a primary middleware definition that
* should be invoked last for a matched route.
* @param array[]|callable[]|string[] $middlewares Middleware definitions.
* @param array<string,scalar|Stringable|null> $defaults Parameter default values indexed by parameter names.
* @param bool $override Marks route as override. When added it will replace existing route with the same name.
* @param array $disabledMiddlewares Excludes middleware from being invoked when action is handled.
Copy link

Copilot AI Mar 25, 2026

Choose a reason for hiding this comment

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

Docblocks/types for $defaults allow scalar|Stringable|null, but both the constructor and defaults() cast values via strval(), which turns null into '' and Stringable into its string representation. Either adjust the docs/psalm types to reflect array<string,string>, or preserve null/Stringable values instead of forcing strings.

Copilot uses AI. Check for mistakes.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

status:code review The pull request needs review.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Named arguments vs immutable methods Add attributes support

6 participants