Skip to content
This repository was archived by the owner on Nov 9, 2024. It is now read-only.

Commit f3ecbf8

Browse files
committed
Add TwigCommandHandler
1 parent 576717b commit f3ecbf8

5 files changed

Lines changed: 81 additions & 14 deletions

File tree

README.md

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,8 @@ You just need to register one or more `InjectorCommand` :
3636

3737
### With twig template
3838

39-
$command = ( new TwigCommand($container->get('twig')) )
40-
->setTemplate('@AppBundle\Resources/assets/twig/foo.html.twig')
41-
->setData(['foo' => 'bar']);
42-
$subscriber = $container->get(ContentInjectorSubscriber::class)->regiterCommand($command);
43-
39+
$commandHandler = $container->get(TwigCommandHandler::class);
40+
$commandHandler->registerCommand('@AppBundle\Resources/assets/twig/foo.html.twig', ['foo' => 'bar']);
4441

4542
### With FormType
4643
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 :
@@ -126,8 +123,10 @@ Injects just before `</body>` tag.
126123

127124
### Test helper
128125
- `Cethyworks\ContentInjectorBundle\Test\InjectorTypeTestCase`
126+
127+
### Command Handler
128+
- `Cethyworks\ContentInjectorBundle\Command\Handler\TwigCommandHandler`
129+
130+
Shorcut service to create & register a `TwigCommand`.
129131

130132
Extends `TypeTestCase` and initialize the `InjectorAwareTypeExtension` extension.
131-
132-
### todo
133-
- Custom `@inject` annotation (?)
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace Cethyworks\ContentInjectorBundle\Command\Handler;
4+
5+
use Cethyworks\ContentInjectorBundle\Command\TwigCommand;
6+
use Cethyworks\ContentInjectorBundle\EventSubscriber\ContentInjectorSubscriber;
7+
use Twig_Environment;
8+
9+
class TwigCommandHandler
10+
{
11+
/**
12+
* @var Twig_Environment
13+
*/
14+
protected $twig;
15+
16+
/**
17+
* @var ContentInjectorSubscriber
18+
*/
19+
protected $injectorSubscriber;
20+
21+
function __construct(Twig_Environment $twig, ContentInjectorSubscriber $injectorSubscriber)
22+
{
23+
$this->twig = $twig;
24+
$this->injectorSubscriber = $injectorSubscriber;
25+
}
26+
27+
/**
28+
* @param $template
29+
* @param array $data
30+
*/
31+
function registerCommand($template, array $data = [])
32+
{
33+
$this->injectorSubscriber->registerCommand((new TwigCommand($this->twig))
34+
->setTemplate($template)
35+
->setData($data));
36+
}
37+
}

src/Command/TwigCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class TwigCommand implements DataAwareCommandInterface, TemplateAwareCommandInte
1919
/**
2020
* @var array
2121
*/
22-
protected $data;
22+
protected $data = [];
2323

2424
/**
2525
* TwigCommand constructor.

src/Resources/config/services.yml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,24 @@
11
services:
2-
Cethyworks\ContentInjectorBundle\EventSubscriber\ContentInjectorSubscriber:
3-
arguments: [ "@Cethyworks\\ContentInjectorBundle\\Injector\\BodyEndInjector" ]
4-
tags: [ { name: "kernel.event_subscriber" } ]
5-
62
Cethyworks\ContentInjectorBundle\Injector\BodyEndInjector: {}
73

84
Cethyworks\ContentInjectorBundle\Command\Factory\TwigFormCommandFactory:
9-
arguments: [ "@twig" ]
5+
arguments:
6+
- "@twig"
107

118
Cethyworks\ContentInjectorBundle\Form\Extension\InjectorAwareTypeExtension:
129
arguments:
1310
- "@Cethyworks\\ContentInjectorBundle\\Command\\Factory\\TwigFormCommandFactory"
1411
- "@Cethyworks\\ContentInjectorBundle\\EventSubscriber\\ContentInjectorSubscriber"
1512
tags:
1613
- { name: form.type_extension, extended_type: Symfony\Component\Form\Extension\Core\Type\FormType }
14+
15+
Cethyworks\ContentInjectorBundle\Command\Handler\TwigCommandHandler:
16+
arguments:
17+
- "@twig"
18+
- "@Cethyworks\\ContentInjectorBundle\\EventSubscriber\\ContentInjectorSubscriber"
19+
20+
Cethyworks\ContentInjectorBundle\EventSubscriber\ContentInjectorSubscriber:
21+
arguments:
22+
- "@Cethyworks\\ContentInjectorBundle\\Injector\\BodyEndInjector"
23+
tags:
24+
- { name: "kernel.event_subscriber" }
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Cethyworks\ContentInjectorBundle\Command\Handler;
4+
5+
use Cethyworks\ContentInjectorBundle\Command\TwigCommand;
6+
use Cethyworks\ContentInjectorBundle\EventSubscriber\ContentInjectorSubscriber;
7+
use PHPUnit\Framework\TestCase;
8+
use Twig_Environment;
9+
10+
class TwigCommandHandlerTest extends TestCase
11+
{
12+
public function testRegisterCommand()
13+
{
14+
$twig = $this->getMockBuilder(Twig_Environment::class)->disableOriginalConstructor()->getMock();
15+
$injectorSubscriber = $this->getMockBuilder(ContentInjectorSubscriber::class)->disableOriginalConstructor()->getMock();
16+
17+
$injectorSubscriber->expects($this->once())->method('registerCommand')->with((new TwigCommand($twig))->setTemplate('foo')->setData(['bar' => 'baz']));
18+
19+
$handler = new TwigCommandHandler($twig, $injectorSubscriber);
20+
21+
$handler->registerCommand('foo', ['bar' => 'baz']);
22+
}
23+
}

0 commit comments

Comments
 (0)