cethyworks / content-injector-bundle
允许在应用程序向客户端发送响应之前进行内容注入。
Requires
- php: ^7.1.3
- symfony/config: ^3.3|^4.2|^5.0
- symfony/dependency-injection: ^3.3|^4.2|^5.0
- symfony/event-dispatcher: ^3.3|^4.2|^5.0
- symfony/form: ^3.3|^4.2|^5.0
- symfony/http-foundation: ^3.3|^4.2|^5.0
- symfony/http-kernel: ^3.3|^4.2|^5.0
- twig/twig: ^1.0||^2.0|^3.0
Requires (Dev)
- phpunit/phpunit: ^4.8
- symfony/asset: ^3.3|^4.2|^5.0
- symfony/framework-bundle: ^3.3|^4.2|^5.0
- symfony/templating: ^3.3|^4.2|^5.0
- symfony/translation: ^3.3|^4.2|^5.0
- symfony/twig-bundle: ^3.3|^4.2|^5.0
- symfony/validator: ^3.3|^4.2|^5.0
This package is not auto-updated.
Last update: 2024-09-25 13:10:44 UTC
README
允许在应用程序向客户端发送响应之前进行有效的内容注入。
它使用一个全局订阅者,当触发 kernel.response
事件时,将从收集的 InjectorCommands
注入内容。 InjectorCommands
可以是一个返回字符串的简单 callable
,也可以是一个具有数据的渲染 twig 模板,非常复杂。
该组件提供了一些助手,用于注入简单文本、twig 模板和 FormView
意识的命令。
安装
composer require cethyworks/content-injector-bundle
AppKernel.php
class AppKernel extends Kernel
{
registerBundles()
{
return [
// ...
new Cethyworks\ContentInjectorBundle\CethyworksContentInjectorBundle()
];
}
}
如何使用
全局订阅者默认配置。
您只需要注册一个或多个 InjectorCommand
$subscriber = $container->get(ContentInjectorSubscriber::class);
$subscriber->regiterCommand(function(){ return 'inject_me'; });
使用 twig 模板
$commandHandler = $container->get(TwigCommandHandler::class);
$commandHandler->registerCommand('@AppBundle\Resources/assets/twig/foo.html.twig', ['foo' => 'bar']);
使用 FormType
该组件提供了一个 TypeExtension
"扩展" FormType
(几乎所有表单),添加了一个 injector
选项,允许配置一个了解 FormType 的 FormView
的注入器。它可以这样使用
AppInjectJsType.php
class AppInjectJsType extends AbstractType
{
public function configureOptions(OptionsResolver $resolver)
{
$resolver->setDefaults(array(
'injector' => [
'template' => '@AppBundle/Resources/assets/twig/app_inject_js_type.html.twig' ]
));
}
public function getBlockPrefix()
{
return 'my_form_id';
}
public function getParent()
{
return EntityType::class;
}
}
app_inject_js_type.html.twig
<script>
var formId = "{{ form_view.vars['id'] }}";
// do something to your form
</script>
盒子里有什么?
事件订阅者
Cethyworks\ContentInjectorBundle\EventSubscriber\ContentInjectorSubscriber($injector)
收集 InjectorCommands
,在触发 kernel.response
事件时执行并注入到 Response
中。
命令
Cethyworks\ContentInjectorBundle\Command\CommandInterface
命令接口。
Cethyworks\ContentInjectorBundle\Command\TextCommand($text)
简单的文本命令。
Cethyworks\ContentInjectorBundle\Command\TwigCommand($twig)->setTemplate($template)->setData($data)
Twig 命令,使用 $data
渲染 $template
。
表单扩展
Cethyworks\ContentInjectorBundle\Form\Extension\InjectorAwareTypeExtension($commandFactory, $responseSubscriber)
启用 injector
表单选项。
@see 上面的 如何/使用 FormType 部分。
工厂
Cethyworks\ContentInjectorBundle\Command\Factory\TwigFormCommandFactory
由 InjectorAwareTypeExtension
内部使用,创建了解 FormView
的 TwigCommands。
注入器
Cethyworks\ContentInjectorBundle\Injector\InjectorInterface
注入器接口。
Cethyworks\ContentInjectorBundle\Injector\BodyEndInjector
在 </body>
标签之前注入。
测试助手
Cethyworks\ContentInjectorBundle\Test\InjectorTypeTestCase
命令处理程序
Cethyworks\ContentInjectorBundle\Command\Handler\TwigCommandHandler
创建并注册 TwigCommand
的快捷服务。
扩展 TypeTestCase
并初始化 InjectorAwareTypeExtension
扩展。