Skip to content
This repository was archived by the owner on Feb 25, 2026. It is now read-only.

Commit cb14100

Browse files
committed
FormBuilder service added
1 parent 48d7490 commit cb14100

10 files changed

Lines changed: 222 additions & 109 deletions

File tree

src/DependencyInjection/Configuration.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ public function getConfigTreeBuilder()
2525
$rootNode = $treeBuilder->root('ewz_recaptcha');
2626
}
2727

28+
$treeBuilder2 = new TreeBuilder('service_definition');
29+
$node = $treeBuilder2->getRootNode();
30+
$node->prototype('array')
31+
->children()
32+
->scalarNode('service_name')->isRequired()->end()
33+
->end();
34+
2835
$rootNode
2936
->children()
3037
->scalarNode('public_key')->isRequired()->end()
@@ -46,6 +53,7 @@ public function getConfigTreeBuilder()
4653
;
4754

4855
$this->addHttpClientConfiguration($rootNode);
56+
$this->addServiceDefinitionConfiguration($rootNode);
4957

5058
return $treeBuilder;
5159
}
@@ -65,4 +73,24 @@ private function addHttpClientConfiguration(ArrayNodeDefinition $node)
6573
->end()
6674
;
6775
}
76+
77+
private function addServiceDefinitionConfiguration(ArrayNodeDefinition $node) {
78+
$node
79+
->children()
80+
->arrayNode('service_definition')
81+
->arrayPrototype()
82+
->children()
83+
->scalarNode('service_name')->isRequired()->end()
84+
->arrayNode('options')
85+
->children()
86+
->scalarNode('action_name')->end()
87+
->scalarNode('script_nonce_csp')->end()
88+
->end()
89+
->end()
90+
->end()
91+
->end()
92+
->end()
93+
->end()
94+
;
95+
}
6896
}

src/DependencyInjection/EWZRecaptchaExtension.php

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@
44

55
use EWZ\Bundle\RecaptchaBundle\DependencyInjection\CompilerPass\WidgetCompilerPass;
66
use EWZ\Bundle\RecaptchaBundle\Resolver\WidgetResolver;
7+
use EWZ\Bundle\RecaptchaBundle\Factory\EWZRecaptchaV2FormBuilderFactory;
8+
use EWZ\Bundle\RecaptchaBundle\Factory\EWZRecaptchaV3FormBuilderFactory;
79
use Symfony\Component\Config\FileLocator;
810
use Symfony\Component\DependencyInjection\ContainerBuilder;
911
use Symfony\Component\DependencyInjection\Loader;
1012
use Symfony\Component\DependencyInjection\Reference;
13+
use Symfony\Component\Form\FormBuilder;
14+
use Symfony\Component\Form\FormBuilderInterface;
1115
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
1216

1317
/**
@@ -38,14 +42,27 @@ public function load(array $configs, ContainerBuilder $container)
3842
$recaptchaService->replaceArgument(1, new Reference('ewz_recaptcha.extension.recaptcha.request_method.proxy_post'));
3943
}
4044

45+
if (3 == $config['version']) {
46+
$container->autowire('ewz_recaptcha.form_builder_factory', EWZRecaptchaV3FormBuilderFactory::class);
47+
} else {
48+
$container->autowire('ewz_recaptcha.form_builder_factory', EWZRecaptchaV2FormBuilderFactory::class);
49+
}
50+
foreach($config['service_definition'] as $serviceDefinition) {
51+
$container->register('ewz_recaptcha.' . $serviceDefinition['service_name'], FormBuilderInterface::class)
52+
->setFactory(array(
53+
new Reference('ewz_recaptcha.form_builder_factory'),
54+
'get'))
55+
->setArguments([$serviceDefinition['options']]);
56+
}
57+
4158
}
4259

4360
/**
4461
* Registers the form widget.
4562
*
4663
* @param ContainerBuilder $container
4764
*/
48-
protected function registerWidget(ContainerBuilder $container, int $version)
65+
protected function registerWidget(ContainerBuilder $container, $version = 2)
4966
{
5067
$templatingEngines = $container->hasParameter('templating.engines')
5168
? $container->getParameter('templating.engines')
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace EWZ\Bundle\RecaptchaBundle\Factory;
4+
5+
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaType;
6+
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrue;
7+
use Symfony\Component\Form\FormFactoryInterface;
8+
9+
class EWZRecaptchaV2FormBuilderFactory
10+
{
11+
12+
private $builder;
13+
14+
public function __construct(FormFactoryInterface $builder)
15+
{
16+
$this->builder = $builder;
17+
}
18+
19+
public function get()
20+
{
21+
return $this->builder->createBuilder(EWZRecaptchaType::class, null, [
22+
'constraints' => array(
23+
new IsTrue()
24+
)
25+
]);
26+
}
27+
28+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
<?php
2+
3+
namespace EWZ\Bundle\RecaptchaBundle\Factory;
4+
5+
use EWZ\Bundle\RecaptchaBundle\Form\Type\EWZRecaptchaV3Type;
6+
use EWZ\Bundle\RecaptchaBundle\Validator\Constraints\IsTrueV3;
7+
use Symfony\Component\Form\FormFactoryInterface;
8+
9+
class EWZRecaptchaV3FormBuilderFactory
10+
{
11+
12+
private $builder;
13+
14+
public function __construct(FormFactoryInterface $builder)
15+
{
16+
$this->builder = $builder;
17+
}
18+
19+
public function get(array $options = array())
20+
{
21+
$constraint = array(
22+
'constraints' => array(
23+
new IsTrueV3()
24+
));
25+
26+
return $this->builder->createBuilder(EWZRecaptchaV3Type::class, null, array_merge($options, $constraint)
27+
);
28+
}
29+
30+
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
<?php
2+
3+
namespace EWZ\Bundle\RecaptchaBundle\Form\Type;
4+
5+
use Symfony\Component\Form\AbstractType;
6+
use Symfony\Component\Form\FormInterface;
7+
use Symfony\Component\Form\FormView;
8+
9+
abstract class AbstractEWZRecaptchaType extends AbstractType
10+
{
11+
/**
12+
* The public key.
13+
*
14+
* @var string
15+
*/
16+
protected $publicKey;
17+
18+
/**
19+
* Enable recaptcha?
20+
*
21+
* @var bool
22+
*/
23+
protected $enabled;
24+
25+
/**
26+
* The API server host name.
27+
*
28+
* @var string
29+
*/
30+
protected $apiHost;
31+
32+
/**
33+
* The reCAPTCHA server URL.
34+
*
35+
* @var string
36+
*/
37+
protected $recaptchaApiServer;
38+
39+
/**
40+
* @param string $publicKey Recaptcha public key
41+
* @param bool $enabled Recaptcha status
42+
* @param string $apiHost Api host
43+
*/
44+
public function __construct($publicKey, $enabled, $apiHost = 'www.google.com')
45+
{
46+
$this->publicKey = $publicKey;
47+
$this->enabled = $enabled;
48+
$this->apiHost = $apiHost;
49+
$this->recaptchaApiServer = sprintf('https://%s/recaptcha/api.js', $apiHost);
50+
}
51+
52+
abstract protected function addCustomVars(FormView $view, FormInterface $form, array $options);
53+
54+
/**
55+
* {@inheritdoc}
56+
*/
57+
public function buildView(FormView $view, FormInterface $form, array $options)
58+
{
59+
$view->vars = array_replace($view->vars, array(
60+
'ewz_recaptcha_enabled' => $this->enabled,
61+
'ewz_recaptcha_apihost' => $this->apiHost,
62+
'ewz_recaptcha_apiuri' => $this->recaptchaApiServer,
63+
'public_key' => $this->publicKey,
64+
));
65+
66+
if (!$this->enabled) {
67+
return;
68+
}
69+
70+
$this->addCustomVars($view, $form, $options);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
public function getBlockPrefix()
77+
{
78+
return 'ewz_recaptcha';
79+
}
80+
81+
/**
82+
* Gets the public key.
83+
*
84+
* @return string The javascript source URL
85+
*/
86+
public function getPublicKey()
87+
{
88+
return $this->publicKey;
89+
}
90+
91+
/**
92+
* Gets the API host name.
93+
*
94+
* @return string The hostname for API
95+
*/
96+
public function getApiHost()
97+
{
98+
return $this->apiHost;
99+
}
100+
}

src/Form/Type/EWZRecaptchaType.php

100644100755
Lines changed: 5 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -12,43 +12,15 @@
1212
/**
1313
* A field for entering a recaptcha text.
1414
*/
15-
class EWZRecaptchaType extends AbstractType
15+
class EWZRecaptchaType extends AbstractEWZRecaptchaType
1616
{
17-
/**
18-
* The reCAPTCHA server URL.
19-
*
20-
* @var string
21-
*/
22-
protected $recaptchaApiServer;
23-
2417
/**
2518
* The reCAPTCHA JS server URL.
2619
*
2720
* @var string
2821
*/
2922
protected $recaptchaApiJsServer;
3023

31-
/**
32-
* The public key.
33-
*
34-
* @var string
35-
*/
36-
protected $publicKey;
37-
38-
/**
39-
* The API server host name.
40-
*
41-
* @var string
42-
*/
43-
protected $apiHost;
44-
45-
/**
46-
* Enable recaptcha?
47-
*
48-
* @var bool
49-
*/
50-
protected $enabled;
51-
5224
/**
5325
* Use AJAX api?
5426
*
@@ -69,43 +41,31 @@ class EWZRecaptchaType extends AbstractType
6941
*/
7042
public function __construct($publicKey, $enabled, $ajax, LocaleResolver $localeResolver, $apiHost = 'www.google.com')
7143
{
72-
$this->publicKey = $publicKey;
73-
$this->enabled = $enabled;
44+
parent::__construct($publicKey, $enabled, $apiHost);
7445
$this->ajax = $ajax;
75-
$this->apiHost = $apiHost;
7646
$this->localeResolver = $localeResolver;
77-
$this->recaptchaApiJsServer = sprintf('//%s/recaptcha/api/js/recaptcha_ajax.js', $apiHost);
78-
$this->recaptchaApiServer = sprintf('https://%s/recaptcha/api.js', $apiHost);
7947
}
8048

8149
/**
8250
* {@inheritdoc}
8351
*/
84-
public function buildView(FormView $view, FormInterface $form, array $options)
52+
protected function addCustomVars(FormView $view, FormInterface $form, array $options)
8553
{
8654
$view->vars = array_replace($view->vars, array(
87-
'ewz_recaptcha_enabled' => $this->enabled,
8855
'ewz_recaptcha_ajax' => $this->ajax,
89-
'ewz_recaptcha_apihost' => $this->apiHost,
9056
));
9157

92-
if (!$this->enabled) {
93-
return;
94-
}
95-
9658
if (!isset($options['language'])) {
9759
$options['language'] = $this->localeResolver->resolve();
9860
}
9961

10062
if (!$this->ajax) {
10163
$view->vars = array_replace($view->vars, array(
102-
'url_challenge' => sprintf('%s?hl=%s', $this->recaptchaApiServer, $options['language']),
103-
'public_key' => $this->publicKey,
64+
'url_challenge' => sprintf('%s?hl=%s', $this->recaptchaApiServer, $options['language'])
10465
));
10566
} else {
10667
$view->vars = array_replace($view->vars, array(
107-
'url_api' => $this->recaptchaApiJsServer,
108-
'public_key' => $this->publicKey,
68+
'url_api' => sprintf('//%s/recaptcha/api/js/recaptcha_ajax.js', $this->apiHost)
10969
));
11070
}
11171
}
@@ -145,14 +105,6 @@ public function getParent()
145105
return TextType::class;
146106
}
147107

148-
/**
149-
* {@inheritdoc}
150-
*/
151-
public function getBlockPrefix()
152-
{
153-
return 'ewz_recaptcha';
154-
}
155-
156108
/**
157109
* Gets the Javascript source URLs.
158110
*
@@ -165,23 +117,4 @@ public function getScriptURL($key)
165117
return isset($this->scripts[$key]) ? $this->scripts[$key] : null;
166118
}
167119

168-
/**
169-
* Gets the public key.
170-
*
171-
* @return string The javascript source URL
172-
*/
173-
public function getPublicKey()
174-
{
175-
return $this->publicKey;
176-
}
177-
178-
/**
179-
* Gets the API host name.
180-
*
181-
* @return string The hostname for API
182-
*/
183-
public function getApiHost()
184-
{
185-
return $this->apiHost;
186-
}
187120
}

0 commit comments

Comments
 (0)