New concept for the context#102
Open
jdreesen wants to merge 10 commits into
Open
Conversation
4f5fede to
580a5e1
Compare
- Added `Context` and `ContextFactory` for managing configurable contexts. - Implemented `ContextConfigurator` interface for defining context configurators. - Enhanced YAML configuration to support global and converter-specific context configurators. - Introduced `ConverterWithDefaultContext` to support default context merging in converters.
- Removed `merge` method from `Context`, simplifying implementation. - Enhanced `with` method to handle `Context` instances directly. - Adjusted `ConverterWithDefaultContext` to replace `merge` usage with updated `with` method. - Added validation to prevent removing a `Context` from another `Context`.
It isn't really usable as long as the `$ctx` parameter is optional. It might make sense to make the parameter non-nullable in the future, at least for populators, and to create an empty context in a converter if none is passed.
580a5e1 to
a58f933
Compare
a58f933 to
6b0f5c8
Compare
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.
New context class
This PR introduces a new
Neusta\ConverterBundle\Contextand deprecates the existingNeusta\ConverterBundle\Converter\Context\GenericContextas well as the use of any other (user-defined) context classes.The old
GenericContextdid not support typed values.User-defined context classes would allow typed values but are problematic if there are multiple bundles involved because it would need multiple inheritance if each bundle wants to define its own (typed) properties in this class.
The new
Contextfixes this by forcing each context value to be a specific (user-defined) class with the necessary (typed) properties, e.g.:You create a context instance with your context value objects and pass it to the converter:
Inside a populator, you can then access the context values like this:
Note
The new
Contextis immutable, so it cannot change within one converter (between its populators).It is, however, possible to create a derived context instance with the desired changes using
with()andwithout().This derived context can then be passed on to internal converters, for example.
Tip
If you need to set a context value in one populator and require it in a later one, you can still do so by defining a mutable context value class and adding it to the context before calling the converter.
However, this is at your own risk, and you must ensure that the populators run in the correct order!
ContextConfigurators
Apart from creating the context manually and passing it to the converter, you can also configure it via the new
ContextConfigurators.It is possible to configure global context (for all converters) or converter-specific context (via
ContextConfigurators) via the bundle’s configuration.To do so, first create a
ContextConfigurator:And register it as a service:
Then configure it either for all converters:
or just a specific one:
Note
If you register global context configurators and converter-specific ones, both will be applied (first the global ones, then the converter-specific ones).
Of course, it is still possible to pass the context manually to
Converter::convert()as well.In this case both contexts will be merged, with the context passed to
Converter::convert()taking precedence.