markup/twig-extension-proxy-bundle
Composer 安装命令:
composer require markup/twig-extension-proxy-bundle
包简介
A Symfony bundle that can provide lazy access to simpler, more common kinds of registered Twig extensions.
README 文档
README
A Symfony bundle that can provide lazy access to simpler, more common kinds of registered Twig extensions.
Why this bundle?
In a typical application using Twig, all extension services need to be instantiated to be added to a Twig environment even if a small minority of extensions will actually be used in any given execution. In a large application, the number of Twig extensions instantiated in this way can become excessive, causing mushrooming memory usage and slowdowns.
For Twig extensions that only declare functions, filters and tests into the Twig environment, this bundle provides a mechanism by which these extensions can be used lazily within a Symfony-based application.
How to use
Typically, a Twig extension will be declared into an application using a service definition such as the following:
my_extension: class: MyBundle\MyExtension arguments: - '@my_heavy_dependency' tags: - { name: twig.extension }
By default, Symfony's TwigBundle will then ensure that my_extension will be added to the twig environment service.
As long as the my_extension extension adheres to the following rules:
- the extension must only declare functions, filters and tests, and must instantiate them independently of any injected dependencies
- the options for the functions, filters and tests must themselves not be dependent on injected dependencies
then it can be declared as a proxied extension instead.
The same functional effect as above can be achieved by the following declaration:
my_extension: class: MyBundle\MyExtension arguments: - '@my_heavy_dependency' tags: - { name: twig.proxied_extension }
With this example, any functions, filters or tests will still be declared into the twig environment, but the extension object itself will only be instantiated (and therefore, my_heavy_dependency will itself only be instantiated) once one of these Twig functors are actually used.
Grouped proxies
In cases where it is useful for Twig extensions to be arranged into groups of proxies (in order to sandbox certain extensions to particular areas of an application), extension services can be tagged with the optional "groups" option. The default group is "default", so to declare additional groups on top of this there should be a comma-separated list in the tag declaration.
The following is the above extension service declaration but declaring in addition a group named "custom":
my_extension: class: MyBundle\MyExtension arguments: - '@my_heavy_dependency' tags: - { name: twig.proxied_extension, groups: "default, custom" }
This custom proxy extension can then be accessed with the service ID markup_twig_extension_proxy.proxy.custom. Typically this would then be added to a manually-declared Twig environment.
Proxying third-party extensions
With the bundle configuration:
markup_twig_extension_proxy: proxify_tagged_extensions: true
an application will opt into converting any service tagged twig.extension into a proxied extension instead (if the extension is compatible, otherwise it will be included in the normal manner).
If you are using a version of Symfony prior to Symfony 3.2, you may need to list this bundle right near the top of your Symfony app kernel in order to convert other extensions. (The compiler pass that achieves this has a priority of 100.)
Known issues
Named arguments are not supported at the current time - unfortunately these are only caught at template compilation time not application compilation time, so please watch out for that!
markup/twig-extension-proxy-bundle 适用场景与选型建议
markup/twig-extension-proxy-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 40.07k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2017 年 09 月 26 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 markup/twig-extension-proxy-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 markup/twig-extension-proxy-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 40.07k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 4
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-09-26