bangpound/callable-compiler-pass
Composer 安装命令:
composer require bangpound/callable-compiler-pass
包简介
Create compiler passes in Symfony without defining another class
README 文档
README
Use this implementation of CompilerPassInterface to create compiler passes in
Symfony without defining another class.
Usage
You should use this class wherever you instantiate a container compiler pass to modify the Symfony dependency injection container. For typical Symfony applications, this happens in a bundle.
Without CallableCompilerPass, your bundle class would add the compiler pass this way:
<?php namespace My\Bundle\CoolBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use My\Bundle\CoolBundle\DependencyInjection\Compiler\AddMyStuffPass; class MyCoolBundle extends Bundle { public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass(new AddMyTaggedServicesPass()); } }
With CallableCompilerPass, you can skip creating the AddMyTaggedServicesPass and
define the compiler pass in the bundle's build method:
<?php namespace My\Bundle\CoolBundle; use Symfony\Component\HttpKernel\Bundle\Bundle; use Symfony\Component\DependencyInjection\ContainerBuilder; use Bangpound\Symfony\DependencyInjection\CallableCompilerPass; class MyCoolBundle extends Bundle { public function build(ContainerBuilder $container) { parent::build($container); $container->addCompilerPass(new CallableCompilerPass( function (ContainerBuilder $container) { if (!$container->has('my_cool.service')) { return; } $definition = $container->findDefinition('my_cool.service'); $taggedServices = $container->findTaggedServiceIds( 'my_cool.service_addition' ); foreach ($taggedServices as $id => $tags) { $definition->addMethodCall( 'addThing', array(new Reference($id)) ); } } )); } }
The utility of the CallableCompilerPass is most apparent when developing a Symfony
application with the new MicroKernelTrait in Symfony 2.8 and 3.0.
<?php use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait; use Symfony\Component\Config\Loader\LoaderInterface; use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\HttpKernel\Kernel; class AppKernel extends Kernel { use MicroKernelTrait; public function registerBundles() { $bundles = array( new Symfony\Bundle\FrameworkBundle\FrameworkBundle(), ); return $bundles; } protected function configureContainer(ContainerBuilder $c, LoaderInterface $loader) { $c->addCompilerPass(new CallableCompilerPass(function (ContainerBuilder $container) { if (!$container->has('my_cool.service')) { return; } $definition = $container->findDefinition('my_cool.service'); $taggedServices = $container->findTaggedServiceIds( 'my_cool.service_addition' ); foreach ($taggedServices as $id => $tags) { $definition->addMethodCall( 'addThing', array(new Reference($id)) ); } }); } }
In the microkernel, you might also use CallableCompilerPass to remove services you do
not need but which are added by the Symfony FrameworkBundle by default, for example.
bangpound/callable-compiler-pass 适用场景与选型建议
bangpound/callable-compiler-pass 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 822 次下载、GitHub Stars 达 6, 最近一次更新时间为 2015 年 12 月 13 日, 在 PHP 生态内属于活跃度较高的组件。
我们在过去多个企业项目中使用过 bangpound/callable-compiler-pass 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 bangpound/callable-compiler-pass 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
统计信息
- 总下载量: 822
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 6
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-12-13