2
2
3
3
namespace Kickin \ExceptionHandlerBundle \DependencyInjection ;
4
4
5
+ use Exception ;
6
+ use Kickin \ExceptionHandlerBundle \Handler \SwiftMailerExceptionHandler ;
7
+ use Kickin \ExceptionHandlerBundle \Handler \SymfonyMailerExceptionHandler ;
8
+ use Symfony \Component \Config \Definition \Exception \InvalidConfigurationException ;
5
9
use Symfony \Component \DependencyInjection \ContainerBuilder ;
6
- use Symfony \Component \Config \FileLocator ;
10
+ use Symfony \Component \DependencyInjection \Definition ;
11
+ use Symfony \Component \DependencyInjection \Reference ;
7
12
use Symfony \Component \HttpKernel \DependencyInjection \Extension ;
8
- use Symfony \Component \DependencyInjection \Loader ;
9
13
10
14
/**
11
15
* This is the class that loads and manages your bundle configuration
@@ -16,12 +20,69 @@ class KickinExceptionHandlerExtension extends Extension
16
20
{
17
21
/**
18
22
* {@inheritDoc}
23
+ * @throws Exception
19
24
*/
20
25
public function load (array $ configs , ContainerBuilder $ container )
21
26
{
27
+ // Parse configuration
22
28
$ configuration = new Configuration ();
23
- $ this ->processConfiguration ($ configuration , $ configs );
24
- $ loader = new Loader \YamlFileLoader ($ container , new FileLocator (__DIR__ .'/../Resources/config ' ));
25
- $ loader ->load ('services.yml ' );
29
+ $ config = $ this ->processConfiguration ($ configuration , $ configs );
30
+
31
+ // Configure the correct service
32
+ if (in_array ($ config ['mail_backend ' ], ['swift ' , 'swift_mailer ' ])) {
33
+ $ this ->configureSwiftmailer ($ container );
34
+ } else {
35
+ $ this ->configureSymfonyMailer ($ container );
36
+ }
37
+ }
38
+
39
+ /**
40
+ * Configures the Swift Mailer handler
41
+ *
42
+ * @param ContainerBuilder $container
43
+ */
44
+ private function configureSwiftMailer (ContainerBuilder $ container )
45
+ {
46
+ if (!class_exists ('Swift_Mailer ' )) {
47
+ throw new InvalidConfigurationException ('You selected SwiftMailer as mail backend, but it is not installed. Try running `composer req symfony/swiftmailer-bundle` or switch to the Symfony mailer in the configuration. ' );
48
+ }
49
+
50
+ $ this ->configureMailer ($ container , SwiftMailerExceptionHandler::class)
51
+ // Explicitly bind the correct mailer, as we do not want the default mailer
52
+ ->setArgument ('$mailer ' , new Reference ('swiftmailer.mailer.exception_mailer ' ))
53
+ // Explicitly bind the correct real transport, as we do not want the default mailer transport
54
+ // Next to that, we also need the real transport, and not the spool
55
+ ->setArgument ('$transport ' , new Reference ('swiftmailer.mailer.exception_mailer.transport.real ' ));
56
+ }
57
+
58
+ /**
59
+ * Configures the Symfony Mailer handler
60
+ *
61
+ * @param ContainerBuilder $container
62
+ */
63
+ private function configureSymfonyMailer (ContainerBuilder $ container )
64
+ {
65
+ if (!class_exists ('Symfony\Component\Mailer\Mailer ' )) {
66
+ throw new InvalidConfigurationException ('You selected the Symfony mailer as mail backend, but it is not installed. Try running `composer req symfony/mailer` or switch to SwiftMailer in the configuration. ' );
67
+ }
68
+
69
+ $ this ->configureMailer ($ container , SymfonyMailerExceptionHandler::class);
70
+ }
71
+
72
+ /**
73
+ * Configure the shared defaults for both handlers
74
+ *
75
+ * @param ContainerBuilder $container
76
+ * @param string $handlerClass
77
+ *
78
+ * @return Definition
79
+ */
80
+ private function configureMailer (ContainerBuilder $ container , string $ handlerClass ): Definition
81
+ {
82
+ return $ container
83
+ ->autowire ($ handlerClass )
84
+ ->setPublic (false )
85
+ ->setLazy (true )
86
+ ->setAutoconfigured (true );
26
87
}
27
- }
88
+ }
0 commit comments