-
Notifications
You must be signed in to change notification settings - Fork 160
/
EWZRecaptchaExtension.php
executable file
·100 lines (84 loc) · 3.73 KB
/
EWZRecaptchaExtension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
<?php
namespace EWZ\Bundle\RecaptchaBundle\DependencyInjection;
use EWZ\Bundle\RecaptchaBundle\Factory\EWZRecaptchaV2FormBuilderFactory;
use EWZ\Bundle\RecaptchaBundle\Factory\EWZRecaptchaV3FormBuilderFactory;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Loader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
/**
* This is the class that loads and manages your bundle configuration.
*/
class EWZRecaptchaExtension extends Extension
{
/**
* {@inheritdoc}
*/
public function load(array $configs, ContainerBuilder $container): void
{
$configuration = new Configuration();
$config = $this->processConfiguration($configuration, $configs);
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader->load('services.yml');
foreach ($config as $key => $value) {
$container->setParameter('ewz_recaptcha.'.$key, $value);
}
$this->registerWidget($container, $config['version']);
if (null !== $config['http_proxy']['host'] && null !== $config['http_proxy']['port']) {
$recaptchaService = $container->findDefinition('ewz_recaptcha.recaptcha');
$recaptchaService->replaceArgument(1, new Reference('ewz_recaptcha.extension.recaptcha.request_method.proxy_post'));
}
if (3 === $config['version']) {
$container->register('ewz_recaptcha.form_builder_factory', EWZRecaptchaV3FormBuilderFactory::class)
->addArgument(new Reference('form.factory'));
} else {
$container->register('ewz_recaptcha.form_builder_factory', EWZRecaptchaV2FormBuilderFactory::class)
->addArgument(new Reference('form.factory'));
}
foreach ($config['service_definition'] as $serviceDefinition) {
$container->register('ewz_recaptcha.'.$serviceDefinition['service_name'], FormBuilderInterface::class)
->setFactory(array(
new Reference('ewz_recaptcha.form_builder_factory'),
'get',
))
->setArguments([$serviceDefinition['options']]);
}
}
/**
* Registers the form widget.
*
* @param ContainerBuilder $container
*/
protected function registerWidget(ContainerBuilder $container, int $version = 2): void
{
$templatingEngines = $container->hasParameter('templating.engines')
? $container->getParameter('templating.engines')
: array('twig');
if (in_array('php', $templatingEngines, true)) {
$formResource = 'EWZRecaptchaBundle:Form';
$container->setParameter('templating.helper.form.resources', array_merge(
$container->getParameter('templating.helper.form.resources'),
array($formResource)
));
}
if (in_array('twig', $templatingEngines, true)) {
$formResource = '@EWZRecaptcha/Form/ewz_recaptcha_widget.html.twig';
if (3 === $version) {
$formResource = '@EWZRecaptcha/Form/v3/ewz_recaptcha_widget.html.twig';
}
$container->setParameter('twig.form.resources', array_merge(
$this->getTwigFormResources($container),
array($formResource)
));
}
}
private function getTwigFormResources(ContainerBuilder $container)
{
if (!$container->hasParameter('twig.form.resources')) {
return [];
}
return $container->getParameter('twig.form.resources');
}
}