Repository navigation
Add #[PurgeContext] to share a context across a controller or action - #182
Merged
Merged
Conversation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## 2.x #182 +/- ##
============================================
+ Coverage 97.46% 97.49% +0.03%
- Complexity 729 737 +8
============================================
Files 76 77 +1
Lines 1736 1759 +23
============================================
+ Hits 1692 1715 +23
Misses 44 44
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
petarf-sofascore
marked this pull request as ready for review
November 16, 2026 11:22
Brajk19
reviewed
Nov 16, 2026
Brajk19
reviewed
Nov 16, 2026
petarf-sofascore
force-pushed
the
petarf-sofascore/PurgeContext
branch
from
November 17, 2026 06:42
223fae5 to
1682d31
Compare
HypeMC
force-pushed
the
petarf-sofascore/PurgeContext
branch
from
November 19, 2026 09:22
1682d31 to
8be890f
Compare
Brajk19
previously approved these changes
Nov 30, 2026
The `context` of a purge subscription can now also be set with the `#[PurgeContext]` attribute on a controller class or an action. The context of a subscription is the class context, merged with the action context and then with the `context` of the `PurgeOn`: arrays with keys are merged key by key, lists are appended without duplicates, and any other value replaces the previous one. The merged context is stored on `RouteMetadata::$context`, which the subscription resolvers now pass to the purge subscriptions.
As agreed in review, a more specific context now replaces the whole value of each top-level key it sets (`array_replace`), instead of merging nested arrays and appending lists. A level that sets a key must write its full value, and `null` removes a value. This removes `ContextMerger`, as the merge is a single `array_replace`.
Like its other arguments, which accept a short form or an object, `PurgeOn` now also takes a `PurgeContext` for its `context` and turns it into its array.
`AGENTS.md` asks not to add tests that only check that a constructor keeps its arguments, to only add comments that say something the code doesn't, and to put a new test right after the test it relates to, not between two tests that belong together. So this removes the test of the `PurgeContext` constructor, the comments that named the rule each block of the attribute provider test checks, and moves the new `PurgeOn` and debug command tests after their groups.
As agreed in review, applications can now extend `PurgeContext` to create their own context attributes, such as a `#[PurgePriority]` that sets one key. The attribute provider finds them with `IS_INSTANCEOF`, and `PurgeOn` already accepts them as its `context`. A controller class or method can still have only one of these attributes. With several on one level, their contexts would have to be merged before the class, method and `PurgeOn` precedence applies, and a key set by two of them would depend on the order of the attributes. Loading the subscriptions fails with an exception instead, so the order never matters.
HypeMC
force-pushed
the
petarf-sofascore/PurgeContext
branch
from
December 2, 2026 06:29
4d4e9b9 to
6c54091
Compare
The resolvers now read the context from `RouteMetadata::$context`, which the YAML provider leaves to its default, the context of the `PurgeOn`. No test failed when that default was changed to an empty array, which would drop the context of every YAML subscription.
The other docs and the changelog write `#[PurgeOn]` and talk about its parameters, start a how-to with "You can", use "In this example", link attributes to their source and say an invalid attribute throws "an exception during cache warmup". The `PurgeContext` docs now do the same. Setting a key to `null` doesn't remove the key, the purger still gets it with a `null` value, so the docs now say it clears the value, and name the actions of the example instead of "the action above".
When a controller class or method had the same context attribute more than once, such as a repeatable subclass of `PurgeContext`, the message repeated its class name for each of them. It now names each class once, followed by how many times it was found, as in `"PurgeReason" 2 times`. A count of one is left out, so with one attribute of each class the message stays the same. The count follows the name because before it, as in `found 2 "A", "B"`, it would read as the total.
1 of 4 tasks
HypeMC
approved these changes
Dec 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary 📝
The
contextof#[PurgeOn](#176) must be repeated on every subscription, even when all routes of a controller need the same data, for example a priority or a cache tag on every social route.This adds a
#[PurgeContext]attribute for controller classes and actions. The context of a subscription starts as the class context. The action context replaces the top-level keys it sets, and then thecontextof thePurgeOndoes the same. It's a plainarray_replace(): a key replaces the whole value, nested arrays included, so a level that sets a key writes its full value, andnullremoves a value.The action context also applies to the class-level
#[PurgeOn]subscriptions of its route.#[PurgeContext]doesn't apply to YAML subscriptions, which have no class or action to put it on. It accepts the same values as thePurgeOncontext and validates them the same way: the validation moved fromPurgeOnto an internalPurgeContext::validate().PurgeContextcan be extended to create your own context attributes, and like its other arguments, which accept a short form or an object, thecontextofPurgeOnalso accepts aPurgeContextor a subclass of it:The merge happens once, in
AttributeMetadataProvider, and the result is stored on a newRouteMetadata::$context. It defaults to thePurgeOncontext, soYamlMetadataProviderand other code that buildsRouteMetadatakeep working. The built-in subscription resolvers andPurgeSubscriptionProvidernow readRouteMetadata::$contextinstead ofPurgeOn::$context. A custom subscription resolver written against 2.1.0 that copies$routeMetadata->purgeOn->contextkeeps working as before, but doesn't get the#[PurgeContext]values until it switches to$routeMetadata->context. The changelog says so. Everything after the subscription (the cached configuration,PurgeRequest::$contexts,purgatory:debugand the profiler) already carries the context, so it shows the merged value without changes.Decisions:
#[PurgeContext([...])]has the same shape asPurgeOn(context: [...]). The bundle uses variadics only for lists of values, never for keys and values.nullis a normal value. It replaces the less specific value and the key stays, so a purger that reads the context with??orisset()sees no value. This keeps the 2.1.0 behaviour, wherePurgeOn(context: ['ttl' => null])already reaches the purger as is.EntityChangeListenercompares contexts strictly when it removes duplicates.#[PurgeOn]attributes are read from.PurgeContextper controller class or action, subclasses included. With several on one level, a key set by two of them would depend on the order of the attributes. Loading the subscriptions throws aLogicExceptionthat names the attributes instead, so the order of the attributes never matters, also relative to the#[PurgeOn]attributes.Tested locally with PHPUnit on PHP 8.5 and a PHP 8.1 lint of the changed files. PHPStan and PHP CS Fixer run in CI.
Checklist ✅
🤖 Generated with Claude Code