Fabien Potencier 428a161790 feature #4989 [3.x] Allow registering an extension class lazily with AttributeExtension (nicolas-grekas)
This PR was merged into the 3.x branch.

Discussion
----------

[3.x] Allow registering an extension class lazily with `AttributeExtension`

When an extension class is registered through `AttributeExtension`, `Environment::getExtension()` returns the `AttributeExtension`, not an instance of the class that was asked for. This PR makes it return the instance provided by the runtime loaders instead. An extension can then be registered lazily, so that its dependencies are created on its first call from a template, without changing what `getExtension()` returns.

`getRuntime()` also falls back to the registered extension of the requested class. An extension can then return the `[self::class, 'method']` callables that `AttributeExtension` builds from its `getFunctions()`, and still work when it's added with `addExtension(new MyExtension(...))`.

Needed for symfony/symfony#66828, which registers Symfony's own extensions this way.

Merge-up to 4.x: only the imports of `tests/EnvironmentTest.php` conflict, and the CHANGELOG entry doesn't go to 4.x.

Commits
-------

6e527a86e0 Allow registering an extension class lazily with `AttributeExtension`
2026-10-16 11:00:11 +02:00
…
…

Twig, the flexible, fast, and secure template language for PHP
==============================================================

Twig is a template language for PHP.

Twig uses a syntax similar to the Django and Jinja template languages which
inspired the Twig runtime environment.

Sponsors
--------

.. raw:: html

    <a href="https://docs.blackfire.io/introduction?utm_source=twig&utm_medium=github_readme&utm_campaign=logo">
        <img src="https://static.blackfire.io/assets/intemporals/logo/png/blackfire-io_secondary_horizontal_transparent.png?1" width="255px" alt="Blackfire.io">
    </a>

More Information
----------------

Read the `documentation`_ for more information.

.. _documentation: https://twig.symfony.com/documentation
Languages
PHP 99.9%