twigphp / Twig

Twig, the flexible, fast, and secure template language for PHP
https://twig.symfony.com/
BSD 3-Clause "New" or "Revised" License
8.18k stars 1.25k forks source link

[Twig\Loader\ChainLoader] Constructor could accept iterable instead of array. #4200

Closed TheCelavi closed 3 months ago

TheCelavi commented 3 months ago

Proposal - Instead of current constructor:

    /**
     * @param LoaderInterface[] $loaders
     */
    public function __construct(array $loaders = [])
    {
        foreach ($loaders as $loader) {
            $this->addLoader($loader);
        }
    }

replace it with:

    /**
     * @param iterable<LoaderInterface> $loaders
     */
    public function __construct(iterable $loaders = [])
    {
        foreach ($loaders as $loader) {
            $this->addLoader($loader);
        }
    }

No BC break, allows to use tagged_iterator in SF autowiring for creating our own Twig environment (beside templating delivered with SF app by default).

WDYT?

stof commented 3 months ago

even better would be to support an iterable in the constructor without iterating it there, to benefit from the lazy-loading feature of the IteratorArgument in Symfony (when using tagged_iterator)

fabpot commented 3 months ago

Looks like good ideas. @TheCelavi Do you want to work on a PR?

TheCelavi commented 3 months ago

Why not - it has been a long time... 😃

Thx guys, sending PR soon.