Skip to content
This repository was archived by the owner on Apr 22, 2025. It is now read-only.

Commit 4748cd7

Browse files
authored
Merge pull request #24 from codete/autoconfiguration
Add autoconfigure support
2 parents de92964 + d9ec04a commit 4748cd7

6 files changed

Lines changed: 78 additions & 4 deletions

File tree

DependencyInjection/CodeteFormGeneratorExtension.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22

33
namespace Codete\FormGeneratorBundle\DependencyInjection;
44

5+
use Codete\FormGeneratorBundle\DependencyInjection\Compiler\ConfigurationModifiersCompilerPass;
6+
use Codete\FormGeneratorBundle\DependencyInjection\Compiler\FieldResolversCompilerPass;
7+
use Codete\FormGeneratorBundle\DependencyInjection\Compiler\ViewProvidersCompilerPass;
8+
use Codete\FormGeneratorBundle\FormConfigurationModifierInterface;
9+
use Codete\FormGeneratorBundle\FormFieldResolverInterface;
10+
use Codete\FormGeneratorBundle\FormViewProviderInterface;
511
use Symfony\Component\Config\FileLocator;
612
use Symfony\Component\DependencyInjection\ContainerBuilder;
713
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
@@ -16,5 +22,14 @@ public function load(array $configs, ContainerBuilder $container)
1622
{
1723
$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
1824
$loader->load('form_generator.xml');
25+
26+
if (method_exists($container, 'registerForAutoconfiguration')) {
27+
$container->registerForAutoconfiguration(FormConfigurationModifierInterface::class)
28+
->addTag(ConfigurationModifiersCompilerPass::TAG);
29+
$container->registerForAutoconfiguration(FormViewProviderInterface::class)
30+
->addTag(ViewProvidersCompilerPass::TAG);
31+
$container->registerForAutoconfiguration(FormFieldResolverInterface::class)
32+
->addTag(FieldResolversCompilerPass::TAG);
33+
}
1934
}
2035
}

DependencyInjection/Compiler/ConfigurationModifiersCompilerPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
class ConfigurationModifiersCompilerPass extends AbstractCompilerPass
99
{
10+
const TAG = 'form_generator.configuration_modifier';
11+
1012
/**
1113
* @inheritdoc
1214
*/
@@ -20,6 +22,6 @@ protected function getMethodToCall()
2022
*/
2123
protected function getTagName()
2224
{
23-
return 'form_generator.configuration_modifier';
25+
return self::TAG;
2426
}
2527
}

DependencyInjection/Compiler/FieldResolversCompilerPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
class FieldResolversCompilerPass extends AbstractCompilerPass
99
{
10+
const TAG = 'form_generator.field_resolver';
11+
1012
/**
1113
* @inheritdoc
1214
*/
@@ -20,6 +22,6 @@ protected function getMethodToCall()
2022
*/
2123
protected function getTagName()
2224
{
23-
return 'form_generator.field_resolver';
25+
return self::TAG;
2426
}
2527
}

DependencyInjection/Compiler/ViewProvidersCompilerPass.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*/
88
class ViewProvidersCompilerPass extends AbstractCompilerPass
99
{
10+
const TAG = 'form_generator.view_provider';
11+
1012
/**
1113
* @inheritdoc
1214
*/
@@ -20,6 +22,6 @@ protected function getMethodToCall()
2022
*/
2123
protected function getTagName()
2224
{
23-
return 'form_generator.view_provider';
25+
return self::TAG;
2426
}
2527
}

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public $person;
122122
This will build form from all nested model properties,
123123
but we can specify which view we want to use:
124124

125-
``` php
125+
``` php
126126
/*
127127
* @Form\Embed(
128128
* class = "Codete\FormGeneratorBundle\Tests\Model\Person",
@@ -132,6 +132,11 @@ but we can specify which view we want to use:
132132
public $employee;
133133
```
134134

135+
---
136+
137+
**If you have enabled [Service autoconfiguration](http://symfony.com/blog/new-in-symfony-3-3-service-autoconfiguration)
138+
the bundle (since version 1.2.0) will automatically tag services for you.**
139+
135140
FormViewProvider
136141
----------------
137142

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
3+
namespace DependencyInjection;
4+
5+
use Codete\FormGeneratorBundle\DependencyInjection\CodeteFormGeneratorExtension;
6+
use Codete\FormGeneratorBundle\DependencyInjection\Compiler\ConfigurationModifiersCompilerPass;
7+
use Codete\FormGeneratorBundle\DependencyInjection\Compiler\FieldResolversCompilerPass;
8+
use Codete\FormGeneratorBundle\DependencyInjection\Compiler\ViewProvidersCompilerPass;
9+
use Codete\FormGeneratorBundle\FormConfigurationModifierInterface;
10+
use Codete\FormGeneratorBundle\FormFieldResolverInterface;
11+
use Codete\FormGeneratorBundle\FormViewProviderInterface;
12+
use Symfony\Component\DependencyInjection\ContainerBuilder;
13+
14+
class CodeteFormGeneratorExtensionTest extends \PHPUnit_Framework_TestCase
15+
{
16+
public function testCoreServicesAreLoaded()
17+
{
18+
$container = new ContainerBuilder();
19+
$extension = new CodeteFormGeneratorExtension();
20+
$extension->load([], $container);
21+
22+
$this->assertTrue($container->has('form_generator'));
23+
$this->assertTrue($container->has('form_generator.type.embed'));
24+
$embedType = $container->getDefinition('form_generator.type.embed');
25+
$this->assertTrue($embedType->hasTag('form.type'));
26+
}
27+
28+
public function testAutoconfigure()
29+
{
30+
if (! method_exists(ContainerBuilder::class, 'registerForAutoconfiguration')) {
31+
$this->markTestSkipped('This test requires Symfony 3.3 or above');
32+
}
33+
34+
$container = new ContainerBuilder();
35+
$extension = new CodeteFormGeneratorExtension();
36+
$extension->load([], $container);
37+
$autoconfigure = $container->getAutoconfiguredInstanceof();
38+
39+
$this->assertArrayHasKey(FormConfigurationModifierInterface::class, $autoconfigure);
40+
$this->assertTrue($autoconfigure[FormConfigurationModifierInterface::class]->hasTag(ConfigurationModifiersCompilerPass::TAG));
41+
42+
$this->assertArrayHasKey(FormViewProviderInterface::class, $autoconfigure);
43+
$this->assertTrue($autoconfigure[FormViewProviderInterface::class]->hasTag(ViewProvidersCompilerPass::TAG));
44+
45+
$this->assertArrayHasKey(FormFieldResolverInterface::class, $autoconfigure);
46+
$this->assertTrue($autoconfigure[FormFieldResolverInterface::class]->hasTag(FieldResolversCompilerPass::TAG));
47+
}
48+
}

0 commit comments

Comments
 (0)