cethyworks/content-injector-bundle

允许在应用程序向客户端发送响应之前进行内容注入。

安装次数: 10,780

依赖项: 3

建议者: 0

安全性: 0

星级: 3

关注者: 2

分支: 6

开放问题: 0

类型:symfony-bundle

v5.0 2019-11-26 12:57 UTC

This package is not auto-updated.

Last update: 2024-09-25 13:10:44 UTC


README

允许在应用程序向客户端发送响应之前进行有效的内容注入。

它使用一个全局订阅者,当触发 kernel.response 事件时,将从收集的 InjectorCommands 注入内容。 InjectorCommands 可以是一个返回字符串的简单 callable,也可以是一个具有数据的渲染 twig 模板,非常复杂。

该组件提供了一些助手,用于注入简单文本、twig 模板和 FormView 意识的命令。

CircleCI

安装

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 扩展。