1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* GpsLab component. |
4
|
|
|
* |
5
|
|
|
* @author Peter Gribanov <[email protected]> |
6
|
|
|
* @copyright Copyright (c) 2016, Peter Gribanov |
7
|
|
|
* @license http://opensource.org/licenses/MIT |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace GpsLab\Bundle\DomainEvent\DependencyInjection; |
11
|
|
|
|
12
|
|
|
use GpsLab\Domain\Event\Bus\EventBus; |
13
|
|
|
use GpsLab\Domain\Event\Listener\Subscriber; |
14
|
|
|
use GpsLab\Domain\Event\Queue\EventQueue; |
15
|
|
|
use Symfony\Component\Config\FileLocator; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
17
|
|
|
use Symfony\Component\DependencyInjection\Loader; |
18
|
|
|
use Symfony\Component\HttpKernel\DependencyInjection\Extension; |
19
|
|
|
|
20
|
|
|
class GpsLabDomainEventExtension extends Extension |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @param array $configs |
24
|
|
|
* @param ContainerBuilder $container |
25
|
|
|
*/ |
26
|
4 |
|
public function load(array $configs, ContainerBuilder $container) |
27
|
|
|
{ |
28
|
4 |
|
$loader = new Loader\YamlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config')); |
29
|
4 |
|
$loader->load('queue.yml'); |
30
|
4 |
|
$loader->load('bus.yml'); |
31
|
4 |
|
$loader->load('locator.yml'); |
32
|
4 |
|
$loader->load('publisher.yml'); |
33
|
|
|
|
34
|
4 |
|
$config = $this->processConfiguration(new Configuration(), $configs); |
35
|
|
|
|
36
|
4 |
|
$container->setAlias('domain_event.bus', $this->busRealName($config['bus'])); |
37
|
4 |
|
$container->setAlias('domain_event.queue', $this->queueRealName($config['queue'])); |
38
|
4 |
|
$container->setAlias('domain_event.locator', $this->locatorRealName($config['locator'])); |
39
|
4 |
|
$container->setAlias(EventBus::class, $this->busRealName($config['bus'])); |
40
|
4 |
|
$container->setAlias(EventQueue::class, $this->queueRealName($config['queue'])); |
41
|
|
|
|
42
|
4 |
|
$container->getDefinition('domain_event.publisher')->replaceArgument(2, $config['publish_on_flush']); |
43
|
|
|
|
44
|
|
|
// subscribers tagged automatically |
45
|
4 |
|
if (method_exists($container, 'registerForAutoconfiguration')) { |
46
|
|
|
$container |
47
|
4 |
|
->registerForAutoconfiguration(Subscriber::class) |
48
|
4 |
|
->addTag('domain_event.subscriber') |
49
|
4 |
|
->setAutowired(true) |
50
|
|
|
; |
51
|
|
|
} |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $name |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
4 |
|
private function busRealName($name) |
60
|
|
|
{ |
61
|
4 |
|
if (in_array($name, ['listener_located', 'queue'])) { |
62
|
3 |
|
return 'domain_event.bus.'.$name; |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
return $name; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $name |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
4 |
|
private function queueRealName($name) |
74
|
|
|
{ |
75
|
4 |
|
if (in_array($name, ['pull_memory', 'subscribe_executing'])) { |
76
|
3 |
|
return 'domain_event.queue.'.$name; |
77
|
|
|
} |
78
|
|
|
|
79
|
1 |
|
return $name; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* @param string $name |
84
|
|
|
* |
85
|
|
|
* @return string |
86
|
|
|
*/ |
87
|
4 |
|
private function locatorRealName($name) |
88
|
|
|
{ |
89
|
4 |
|
if (in_array($name, ['direct_binding', 'container', 'symfony'])) { |
90
|
3 |
|
return 'domain_event.locator.'.$name; |
91
|
|
|
} |
92
|
|
|
|
93
|
1 |
|
return $name; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @return string |
98
|
|
|
*/ |
99
|
1 |
|
public function getAlias() |
100
|
|
|
{ |
101
|
1 |
|
return 'gpslab_domain_event'; |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|