1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace M6Web\Bundle\ApiExceptionBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
6
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
7
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
8
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* This is the class that loads and manages your bundle configuration |
12
|
|
|
*/ |
13
|
|
|
class M6WebApiExceptionExtension extends Extension |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* {@inheritDoc} |
17
|
|
|
*/ |
18
|
|
|
public function load(array $configs, ContainerBuilder $container) |
19
|
|
|
{ |
20
|
1 |
|
$configuration = new Configuration(); |
21
|
1 |
|
$config = $this->processConfiguration($configuration, $configs); |
22
|
|
|
|
23
|
1 |
|
$this->loadExceptionManager($container, $config); |
24
|
1 |
|
$this->loadExceptionListener($container, $config); |
25
|
1 |
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return string |
29
|
|
|
*/ |
30
|
|
|
public function getAlias() |
31
|
|
|
{ |
32
|
1 |
|
return 'm6web_api_exception'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* load service exception manager |
37
|
|
|
* |
38
|
|
|
* @param ContainerBuilder $container |
39
|
|
|
* @param array $config |
40
|
|
|
*/ |
41
|
|
|
protected function loadExceptionManager(ContainerBuilder $container, array $config) |
42
|
|
|
{ |
43
|
1 |
|
$definition = new Definition( |
44
|
1 |
|
'M6Web\Bundle\ApiExceptionBundle\Manager\ExceptionManager', |
45
|
|
|
[ |
46
|
1 |
|
$config['default'], |
47
|
1 |
|
$config['exceptions'], |
48
|
|
|
] |
49
|
1 |
|
); |
50
|
|
|
|
51
|
1 |
|
$container->setDefinition($this->getAlias().'.manager.exception', $definition); |
52
|
1 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* load service exception listener |
56
|
|
|
* |
57
|
|
|
* @param ContainerBuilder $container |
58
|
|
|
* @param array $config |
59
|
|
|
*/ |
60
|
|
|
protected function loadExceptionListener(ContainerBuilder $container, array $config) |
61
|
|
|
{ |
62
|
1 |
|
$definition = new Definition( |
63
|
1 |
|
'M6Web\Bundle\ApiExceptionBundle\EventListener\ExceptionListener', |
64
|
|
|
[ |
65
|
1 |
|
new Reference('kernel'), |
66
|
1 |
|
new Reference($this->getAlias().'.manager.exception'), |
67
|
1 |
|
$config['match_all'], |
68
|
1 |
|
$config['default'], |
69
|
1 |
|
$config['stack_trace'], |
70
|
|
|
] |
71
|
1 |
|
); |
72
|
|
|
|
73
|
1 |
|
$definition->addTag( |
74
|
1 |
|
'kernel.event_listener', |
75
|
|
|
[ |
76
|
1 |
|
'event' => 'kernel.exception', |
77
|
1 |
|
'method' => 'onKernelException', |
78
|
|
|
'priority' => '-100' // as the setresponse stop the exception propagation, this listener has to pass in last position |
79
|
1 |
|
] |
80
|
1 |
|
); |
81
|
|
|
|
82
|
1 |
|
$container->setDefinition($this->getAlias().'.listener.exception', $definition); |
83
|
1 |
|
} |
84
|
|
|
} |
85
|
|
|
|