1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Linio\DynamicFormBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface; |
6
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
7
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
8
|
|
|
|
9
|
|
|
class CompilerPass implements CompilerPassInterface |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* @param ContainerBuilder $container |
13
|
|
|
*/ |
14
|
|
|
public function process(ContainerBuilder $container) |
15
|
|
|
{ |
16
|
|
|
$this->loadFormlyFields($container); |
17
|
|
|
$this->loadDataProviders($container); |
18
|
|
|
$this->loadSubscribers($container); |
19
|
|
|
$this->loadHelpMessageProviders($container); |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param ContainerBuilder $container |
24
|
|
|
*/ |
25
|
|
View Code Duplication |
public function loadFormlyFields(ContainerBuilder $container) |
|
|
|
|
26
|
|
|
{ |
27
|
|
|
if (!$container->hasDefinition('container.formly_field')) { |
28
|
|
|
return; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
$containerDefinition = $container->getDefinition('container.formly_field'); |
32
|
|
|
$taggedServices = $container->findTaggedServiceIds('formly_field'); |
33
|
|
|
|
34
|
|
|
foreach ($taggedServices as $id => $tags) { |
35
|
|
|
foreach ($tags as $attributes) { |
36
|
|
|
$containerDefinition->addMethodCall('addFormlyField', [$attributes['alias'], new Reference($id)]); |
37
|
|
|
} |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param ContainerBuilder $container |
43
|
|
|
*/ |
44
|
|
View Code Duplication |
public function loadDataProviders(ContainerBuilder $container) |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$containerDefinition = $container->getDefinition('dynamic_form.factory'); |
47
|
|
|
$taggedServices = $container->findTaggedServiceIds('dynamic_form.data_provider'); |
48
|
|
|
|
49
|
|
|
foreach ($taggedServices as $id => $tags) { |
50
|
|
|
foreach ($tags as $attributes) { |
51
|
|
|
$containerDefinition->addMethodCall('addDataProvider', [$attributes['alias'], new Reference($id)]); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param ContainerBuilder $container |
58
|
|
|
*/ |
59
|
|
View Code Duplication |
public function loadSubscribers(ContainerBuilder $container) |
|
|
|
|
60
|
|
|
{ |
61
|
|
|
$containerDefinition = $container->getDefinition('dynamic_form.factory'); |
62
|
|
|
$taggedServices = $container->findTaggedServiceIds('dynamic_form.event_subscriber'); |
63
|
|
|
|
64
|
|
|
foreach ($taggedServices as $id => $tags) { |
65
|
|
|
foreach ($tags as $attributes) { |
66
|
|
|
$containerDefinition->addMethodCall('addEventSubscriber', [$attributes['form_name'], new Reference($id)]); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param ContainerBuilder $container |
73
|
|
|
*/ |
74
|
|
View Code Duplication |
public function loadHelpMessageProviders(ContainerBuilder $container) |
|
|
|
|
75
|
|
|
{ |
76
|
|
|
$containerDefinition = $container->getDefinition('dynamic_form.factory'); |
77
|
|
|
$taggedServices = $container->findTaggedServiceIds('dynamic_form.help_message_provider'); |
78
|
|
|
|
79
|
|
|
foreach ($taggedServices as $id => $tags) { |
80
|
|
|
foreach ($tags as $attributes) { |
81
|
|
|
$containerDefinition->addMethodCall('addHelpMessageProvider', [$attributes['alias'], new Reference($id)]); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.