8000 [TwigBundle] Add loader priority by wizhippo · Pull Request #12174 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[TwigBundle] Add loader priority #12174

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed

[TwigBundle] Add loader priority #12174

wants to merge 1 commit into from

Conversation

wizhippo
Copy link
Contributor
@wizhippo wizhippo commented Oct 8, 2014
Q A
Bug fix? no
New feature? yes
BC breaks? no
Deprecations? no
Tests pass? yes
Fixed tickets
License MIT
Doc PR

Add the ability to specify a priority to the tag of twig.loader services.

eg:

        <service id="twig.loader.filesystem" class="%twig.loader.filesystem.class%" public="false">
            <argument type="service" id="templating.locator" />
            <argument type="service" id="templating.name_parser" />
            <tag name="twig.loader" priority="100"/>
        </service>

return 0;
}
return ($a[0] > $b[0]) ? -1 : 1;
});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this can be much faster by using nested arrays instead, as done in other places:

$prioritizedLoaders = array();

foreach ($loaderIds as $id => $tags) {
    foreach($tags as $tag) {
        $priority = isset($attribute['priority']) ? $attribute['priority'] : 0;
        $prioritizedLoaders[$priority][] = $id;
    }
}

ksort($prioritizedLoaders);

foreach ($prioritizedLoaders as $loaders) {
    foreach ($loaders as $loader) {
        $chainLoader->addMethodCall('addLoader', array(new Reference($loader)));
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that my own logic supports having multiple tags on the same service properly instead of ignoring all tags except the first one defining a priority (even though registering the same loader multiple times does not make much sense)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is ksort correct here? Higher priority needs to be first.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm, indeed, it should be krsort

@Nicofuma
Copy link
Contributor
Nicofuma commented Oct 9, 2014

Shouldn't it be better to add the support of the priorities to the ChainLoader? (in this case it's up to the Twig project and you should create a ticket in fabpot/twig)

@stof
Copy link
Member
stof commented Oct 9, 2014

@Nicofuma putting this in the ChainLoader would mean doing the sorting at runtime, which has an impact on perf. IMO, it is perfectly fine for Twig to assume that you register the loaders in the order which matters for you.
Using priorities in the tags is a way to control this order in the container, instead of having loaders registered in the order in which they were defined as services.

@Nicofuma
Copy link
Contributor
Nicofuma commented Oct 9, 2014

I see the point, I was thinking to something similar to the EventDispatcher where the overhead of the priorities is very low.

@stof
Copy link
Member
stof commented Oct 9, 2014

Well, the priority at runtime are logical in the dispatcher, as the registration of event listeners is not expected to happen in a central place, even when not using it in a DI context. For instance, for forms, each form type in the hierarchy can add listeners, and they might want to run either before or after the listener added by the parent type in the hierarchy.
On the other hand, configuring a chain loader is something you will do in the place configuring your Twig environment. The use case for the priority is not the same

@fabpot
Copy link
Member
fabpot commented Feb 5, 2015

Thank you @wizhippo.

@fabpot
Copy link
Member
fabpot commented Feb 5, 2015

@wizhippo Can you submit a PR on symfony/symfony-docs to document this new feature? Thanks.

@fabpot fabpot closed this Feb 5, 2015
fabpot added a commit that referenced this pull request Feb 5, 2015
This PR was submitted for the master branch but it was merged into the 2.7 branch instead (closes #12174).

Discussion
----------

[TwigBundle] Add loader priority

| Q             | A
| ------------- | ---
| Bug fix?      | no
| New feature?  | yes
| BC breaks?    | no
| Deprecations? | no
| Tests pass?   | yes
| Fixed tickets |
| License       | MIT
| Doc PR        |

Add the ability to specify a priority to the tag of `twig.loader` services.

eg:
```
        <service id="twig.loader.filesystem" class="%twig.loader.filesystem.class%" public="false">
            <argument type="service" id="templating.locator" />
            <argument type="service" id="templating.name_parser" />
            <tag name="twig.loader" priority="100"/>
        </service>
```

Commits
-------

67dffea Add Twig loader priority
@wizhippo wizhippo deleted the patch-1 branch February 9, 2015 15:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants
0