New concept for the context #102
Open
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.