Skip to content

Commit ef8812a

Browse files
Merge pull request #6 from Kick-In/symfony-mailer-v2
Add support for Symfony Mailer
2 parents bdff8d6 + b4a9054 commit ef8812a

17 files changed

+555
-224
lines changed

Backtrace/BacktraceLogFile.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Kickin\ExceptionHandlerBundle\Backtrace;
44

5+
use Exception;
56
use Symfony\Component\Filesystem\Exception\IOException;
67
use Symfony\Component\Filesystem\Filesystem;
78

@@ -39,6 +40,8 @@ class BacktraceLogFile
3940
* Constructor -> generate an unique name
4041
*
4142
* @param string $folder
43+
*
44+
* @throws Exception
4245
*/
4346
public function __construct($folder)
4447
{
@@ -58,6 +61,8 @@ public function getName()
5861

5962
/**
6063
* Generates a new, unique name
64+
*
65+
* @throws Exception
6166
*/
6267
public function generateNewName()
6368
{

Configuration/ConfigurationInterface.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,52 +2,52 @@
22

33
namespace Kickin\ExceptionHandlerBundle\Configuration;
44

5-
use Symfony\Component\DependencyInjection\ContainerInterface;
65
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
76

7+
/**
8+
* Interface ConfigurationInterface
9+
*
10+
* @internal
11+
*/
812
interface ConfigurationInterface
913
{
1014
/**
1115
* Indicate whether the current environment is a production environment
1216
*
1317
* @return bool
1418
*/
15-
public function isProductionEnvironment();
19+
public function isProductionEnvironment(): bool;
1620

1721
/**
1822
* Return the backtrace file root folder path
1923
*
2024
* @return string
2125
*/
22-
public function getBacktraceFolder();
26+
public function getBacktraceFolder(): string;
2327

2428
/**
25-
* SwiftMailer representation of the error sender
29+
* Retrieve user information from the token, and return it in a single string
30+
*
31+
* @param TokenInterface|null $token
2632
*
27-
* @return string|array
33+
* @return string
2834
*/
29-
public function getSender();
35+
public function getUserInformation(TokenInterface $token = NULL): string;
3036

3137
/**
32-
* SwiftMailer representation of the error receiver
38+
* Retrieve the system version
3339
*
34-
* @return mixed
40+
* @return string
3541
*/
36-
public function getReceiver();
42+
public function getSystemVersion(): string;
3743

3844
/**
39-
* Retrieve user information from the token, and return it in a single string
40-
*
41-
* @param TokenInterface|null $token
42-
*
43-
* @return string
45+
* Representation of the error sender
4446
*/
45-
public function getUserInformation(TokenInterface $token = null);
47+
public function getSender();
4648

4749
/**
48-
* Retrieve the system version
49-
*
50-
* @return mixed
50+
* Representation of the error receiver
5151
*/
52-
public function getSystemVersion();
52+
public function getReceiver();
5353
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace Kickin\ExceptionHandlerBundle\Configuration;
4+
5+
/**
6+
* Interface SwiftMailerConfigurationInterface
7+
*/
8+
interface SwiftMailerConfigurationInterface extends ConfigurationInterface
9+
{
10+
/**
11+
* @inheritDoc
12+
*
13+
* @return string|array
14+
*/
15+
public function getSender();
16+
17+
/**
18+
* @inheritDoc
19+
*
20+
* @return mixed
21+
*/
22+
public function getReceiver();
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace Kickin\ExceptionHandlerBundle\Configuration;
4+
5+
use Symfony\Component\Mime\Address;
6+
7+
/**
8+
* Interface SymfonyMailerConfigurationInterface
9+
*/
10+
interface SymfonyMailerConfigurationInterface extends ConfigurationInterface
11+
{
12+
/**
13+
* @inheritDoc
14+
*
15+
* @return Address|string
16+
*/
17+
public function getSender();
18+
19+
/**
20+
* @inheritDoc
21+
*
22+
* @return Address|string
23+
*/
24+
public function getReceiver();
25+
}

DependencyInjection/Configuration.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,27 @@ class Configuration implements ConfigurationInterface
1717
*/
1818
public function getConfigTreeBuilder()
1919
{
20-
$treeBuilder = new TreeBuilder();
21-
$treeBuilder->root('kickin_exception_handler');
20+
$treeBuilder = new TreeBuilder('kickin_exception_handler');
21+
22+
if (method_exists($treeBuilder, 'getRootNode')) {
23+
$rootNode = $treeBuilder->getRootNode();
24+
} else {
25+
// for symfony/config 4.1 and older
26+
$rootNode = $treeBuilder->root('kickin_exception_handler');
27+
}
28+
2229
// Here you should define the parameters that are allowed to
2330
// configure your bundle. See the documentation linked above for
2431
// more information on that topic.
32+
33+
$rootNode
34+
->children()
35+
->enumNode('mail_backend')
36+
->values(['swift', 'swift_mailer', 'symfony', 'symfony_mailer'])
37+
->defaultValue('swift')
38+
->end() // mailer_style
39+
->end(); // root children
40+
2541
return $treeBuilder;
2642
}
2743
}

DependencyInjection/KickinExceptionHandlerExtension.php

Lines changed: 67 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@
22

33
namespace Kickin\ExceptionHandlerBundle\DependencyInjection;
44

5+
use Exception;
6+
use Kickin\ExceptionHandlerBundle\Handler\SwiftMailerExceptionHandler;
7+
use Kickin\ExceptionHandlerBundle\Handler\SymfonyMailerExceptionHandler;
8+
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
59
use Symfony\Component\DependencyInjection\ContainerBuilder;
6-
use Symfony\Component\Config\FileLocator;
10+
use Symfony\Component\DependencyInjection\Definition;
11+
use Symfony\Component\DependencyInjection\Reference;
712
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
8-
use Symfony\Component\DependencyInjection\Loader;
913

1014
/**
1115
* This is the class that loads and manages your bundle configuration
@@ -16,12 +20,69 @@ class KickinExceptionHandlerExtension extends Extension
1620
{
1721
/**
1822
* {@inheritDoc}
23+
* @throws Exception
1924
*/
2025
public function load(array $configs, ContainerBuilder $container)
2126
{
27+
// Parse configuration
2228
$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);
2687
}
27-
}
88+
}

Exceptions/FileAlreadyExistsException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Kickin\ExceptionHandlerBundle\Exceptions;
44

5-
class FileAlreadyExistsException extends \Exception
5+
use Exception;
6+
7+
class FileAlreadyExistsException extends Exception
68
{
79

8-
}
10+
}

Exceptions/UploadFailedException.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@
22

33
namespace Kickin\ExceptionHandlerBundle\Exceptions;
44

5-
class UploadFailedException extends \Exception
5+
use Exception;
6+
7+
class UploadFailedException extends Exception
68
{
79

8-
}
10+
}

0 commit comments

Comments
 (0)