2017 © Pedro Peláez
 

symfony-bundle content-injector-bundle

Allow content injection before the app send the response to the client.

image

cethyworks/content-injector-bundle

Allow content injection before the app send the response to the client.

  • Wednesday, August 9, 2017
  • by Cethy
  • Repository
  • 1 Watchers
  • 1 Stars
  • 611 Installations
  • PHP
  • 2 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 5 Versions
  • 35 % Grown

The README.md

Cethyworks\ContentInjectorBundle

Allow effective content injection before the app sends the response to the client., (*1)

It uses a global subscriber which will inject content from collected InjectorCommands when kernel.response event is fired. The InjectorCommands can be a simple callable returning a string or be as complex as a rendered twig template with data., (*2)

The bundle provides helpers to inject simple text, twig templates and FormView aware commands., (*3)

CircleCI, (*4)

Install

composer require cethyworks/content-injector-bundle

AppKernel.php, (*5)

class AppKernel extends Kernel
{
    registerBundles()
    {
        return [
            // ...
            new Cethyworks\ContentInjectorBundle\CethyworksContentInjectorBundle()
        ];
    }
}

How to use

The global subscriber is configured out of the box., (*6)

You just need to register one or more InjectorCommand :, (*7)

$subscriber = $container->get(ContentInjectorSubscriber::class);
$subscriber->regiterCommand(function(){ return 'inject_me'; });

With twig template

$commandHandler = $container->get(TwigCommandHandler::class);
$commandHandler->registerCommand('@AppBundle\Resources/assets/twig/foo.html.twig', ['foo' => 'bar']);

With FormType

The bundle provides a TypeExtension "extending" FormType (virtually all forms) adding a injector option allowing the configuration of an injector aware of the FormType's FormView. It ca be used like this :, (*8)

AppInjectJsType.php, (*9)

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, (*10)

<script>
    var formId = "{{ form_view.vars['id'] }}";

    // do something to your form
</script>

What's in the box ?

EventSubscriber

  • Cethyworks\ContentInjectorBundle\EventSubscriber\ContentInjectorSubscriber($injector)

Collects InjectorCommands, execute and inject them into the Response when kernel.response event is fired., (*11)

Commands

  • Cethyworks\ContentInjectorBundle\Command\CommandInterface

Command interface., (*12)

  • Cethyworks\ContentInjectorBundle\Command\TextCommand($text)

Simple Text Command., (*13)

  • Cethyworks\ContentInjectorBundle\Command\TwigCommand($twig)->setTemplate($template)->setData($data)

Twig Command, render $template with $data., (*14)

FormExtension

  • Cethyworks\ContentInjectorBundle\Form\Extension\InjectorAwareTypeExtension($commandFactory, $responseSubscriber)

Enable the injector form option., (*15)

@see section How to / With Form" ""Type above., (*16)

Factories

  • Cethyworks\ContentInjectorBundle\Command\Factory\TwigFormCommandFactory

Used internally by InjectorAwareTypeExtension, create TwigCommands aware of FormView., (*17)

Injectors

  • Cethyworks\ContentInjectorBundle\Injector\InjectorInterface

Injector interface., (*18)

  • Cethyworks\ContentInjectorBundle\Injector\BodyEndInjector

Injects just before </body> tag., (*19)

Test helper

  • Cethyworks\ContentInjectorBundle\Test\InjectorTypeTestCase

Command Handler

  • Cethyworks\ContentInjectorBundle\Command\Handler\TwigCommandHandler

Shorcut service to create & register a TwigCommand., (*20)

Extends TypeTestCase and initialize the InjectorAwareTypeExtension extension., (*21)

The Versions

06/07 2017