cethyworks / content-injector-bundle
Allow content injection before the app send the response to the client.
Installs: 10 792
Dependents: 3
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 6
Open Issues: 0
Type:symfony-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-11-06 13:37:52 UTC
README
Allow effective content injection before the app sends the response to the client.
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.
The bundle provides helpers to inject simple text, twig templates and FormView
aware commands.
Install
composer require cethyworks/content-injector-bundle
AppKernel.php
class AppKernel extends Kernel
{
registerBundles()
{
return [
// ...
new Cethyworks\ContentInjectorBundle\CethyworksContentInjectorBundle()
];
}
}
How to use
The global subscriber is configured out of the box.
You just need to register one or more InjectorCommand
:
$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 :
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>
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.
Commands
Cethyworks\ContentInjectorBundle\Command\CommandInterface
Command interface.
Cethyworks\ContentInjectorBundle\Command\TextCommand($text)
Simple Text Command.
Cethyworks\ContentInjectorBundle\Command\TwigCommand($twig)->setTemplate($template)->setData($data)
Twig Command, render $template
with $data
.
FormExtension
Cethyworks\ContentInjectorBundle\Form\Extension\InjectorAwareTypeExtension($commandFactory, $responseSubscriber)
Enable the injector
form option.
@see section How to / With Form" ""Type above.
Factories
Cethyworks\ContentInjectorBundle\Command\Factory\TwigFormCommandFactory
Used internally by InjectorAwareTypeExtension
, create TwigCommands aware of FormView
.
Injectors
Cethyworks\ContentInjectorBundle\Injector\InjectorInterface
Injector interface.
Cethyworks\ContentInjectorBundle\Injector\BodyEndInjector
Injects just before </body>
tag.
Test helper
Cethyworks\ContentInjectorBundle\Test\InjectorTypeTestCase
Command Handler
Cethyworks\ContentInjectorBundle\Command\Handler\TwigCommandHandler
Shorcut service to create & register a TwigCommand
.
Extends TypeTestCase
and initialize the InjectorAwareTypeExtension
extension.