Skip to content

Feature request: add base_source_class option to Environment #3141

Description

@markhalliwell

Follow-up from #3082 (comment):

I still don't think that this [metadata] belongs to Twig and there are ways to implement that outside of core.

I managed to get a working POC, but it unfortunately relied heavily on a decorator pattern to extend the Source class.

It was based on older Twig code, prior to it having the final syntax on the Source class (I was recently made aware that this change).

So it's really not that easy to implement this "outside of [Twig] core"; especially when there's a clear habit of making all classes final and locking down every bit of code for "reasons".

The entire point of that issue was to avoid this unnecessary boilerplate/wrapper code and performance issues that would be inherently caused by such code.

There are legitimate use-cases for needing associated metadata (see #3082) and it is a problem that hasn't been easily solvable due to the massively restrictive nature of the code; despite the quick dismissal that it somehow is.

I get that the thinking is that Twig is "highly extensible" via Filters, Functions, etc. and sure... at that level, it is.

However, this issue is extremely low level; way before it even reaches the rendering level.

It's my hope that this issue would actually be taken seriously by this project.


On actually attempting to solve this issue:

I'm still not convinced a loader is the best approach either. The metadata really should be associated with the source it was taken from; not a loader (which would have to keep a map of names/Source objects and metadata... again, costly and horrible DX).

Could there be another option introduced that allows an Environment to set the Source class that should be used (similar to base_template_class)?

'base_source_class' => Source::class,

This would allow final to be kept on Source directly but also allow custom Source objects to be used instead as well.

I know almost all interfaces were removed in 2.0, however, this would mean a SourceInterface would actually need to be created and used for type hints to prevent any BC break.

This would allow for a very clean and easy way for metadata to be captured and parsed (outside of Twig core) without having to use a custom loader and all the boilerplate code that comes with that approach.

From #2549 (comment):

Making this extendable will not be done without a clear use case justifying the extension point (any new extension point has a maintenance cost, and inheritance-based extension points are the most costly in regard to backward compatibility)

The clear use cases for needing metadata associated with a Source object (there are multiple, see #3082). The justification is that it's impossible to do otherwise.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions