Skip to content
This repository was archived by the owner on Jul 11, 2023. It is now read-only.

Commit 0c6d64d

Browse files
author
Tito Costa
committed
used a form extension to subscribe the listener instead of injecting on every form individually
1 parent 34553e6 commit 0c6d64d

6 files changed

Lines changed: 49 additions & 29 deletions

File tree

DependencyInjection/Configuration.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,10 @@ public function getConfigTreeBuilder()
2020
$treeBuilder = new TreeBuilder();
2121
$rootNode = $treeBuilder->root('oh_form_error_log');
2222

23-
// Here you should define the parameters that are allowed to
24-
// configure your bundle. See the documentation linked above for
25-
// more information on that topic.
23+
$rootNode
24+
->children()
25+
->scalarNode('logger')->isRequired()->cannotBeEmpty()->end()
26+
->end();
2627

2728
return $treeBuilder;
2829
}

DependencyInjection/OhFormErrorLogExtension.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public function load(array $configs, ContainerBuilder $container)
2222
$configuration = new Configuration();
2323
$config = $this->processConfiguration($configuration, $configs);
2424

25+
$container->setAlias('oh_form_error_log.logger.manager', $config['logger']);
26+
2527
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
2628
$loader->load('services.yml');
2729
}

EventListener/ErrorLogSubscriber.php

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,7 @@
66
use Symfony\Component\Form\FormEvent;
77
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
88
use Symfony\Component\Form\FormEvents;
9-
use Symfony\Component\HttpFoundation\Request;
10-
11-
//use Symfony\Component\Serializer\Serializer;
12-
//use Symfony\Component\Serializer\Normalizer\GetSetMethodNormalizer;
13-
//use Symfony\Component\Serializer\Encoder\JsonEncoder;
14-
9+
use Symfony\Component\HttpFoundation\RequestStack;
1510

1611
class ErrorLogSubscriber implements EventSubscriberInterface
1712
{
@@ -27,10 +22,10 @@ class ErrorLogSubscriber implements EventSubscriberInterface
2722
*/
2823
private $request;
2924

30-
public function __construct(ErrorLogInterface $logger, Request $request)
25+
public function __construct(ErrorLogInterface $logger, RequestStack $request)
3126
{
3227
$this->logger = $logger;
33-
$this->request = $request;
28+
$this->request = $request->getMasterRequest();
3429
}
3530

3631
public static function getSubscribedEvents()
@@ -132,4 +127,4 @@ private function getErrorMessages(\Symfony\Component\Form\Form $form) {
132127

133128
return $errors;
134129
}
135-
}
130+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace Oh\FormErrorLogBundle\Form\Extension;
4+
5+
use Symfony\Component\Form\AbstractTypeExtension;
6+
use Symfony\Component\Form\FormBuilderInterface;
7+
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
8+
9+
class FormLogTypeExtension extends AbstractTypeExtension
10+
{
11+
12+
private $eventSubscriber;
13+
14+
public function __construct(EventSubscriberInterface $eventSubscriber)
15+
{
16+
$this->eventSubscriber = $eventSubscriber;
17+
}
18+
19+
public function buildForm(FormBuilderInterface $builder, array $options)
20+
{
21+
$builder->addEventSubscriber($this->eventSubscriber);
22+
}
23+
24+
public function getExtendedType()
25+
{
26+
return 'form';
27+
}
28+
}

Resources/config/services.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,14 @@ services:
2121
class: %oh_form_error_log.listener.class%
2222
arguments: [@oh_form_error_log.logger, @request]
2323
scope: request
24+
oh_form_subscriber:
25+
class: Oh\FormErrorLogBundle\EventListener\ErrorLogSubscriber
26+
arguments:
27+
- @oh_form_error_log.logger.manager
28+
- @request_stack
29+
oh_form_extension:
30+
class: Oh\FormErrorLogBundle\Form\Extension\FormLogTypeExtension
31+
arguments:
32+
- @oh_form_subscriber
33+
tags:
34+
- { name: form.type_extension, alias: form }

Tests/Controller/DefaultControllerTest.php

Lines changed: 0 additions & 17 deletions
This file was deleted.

0 commit comments

Comments
 (0)