1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SfCod\SocketIoBundle\DependencyInjection; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use SfCod\SocketIoBundle\Command\NodeJsServerCommand; |
7
|
|
|
use SfCod\SocketIoBundle\Command\PhpServerCommand; |
8
|
|
|
use SfCod\SocketIoBundle\Command\ProcessCommand; |
9
|
|
|
use SfCod\SocketIoBundle\Events\EventInterface; |
10
|
|
|
use SfCod\SocketIoBundle\Events\EventPublisherInterface; |
11
|
|
|
use SfCod\SocketIoBundle\Events\EventSubscriberInterface; |
12
|
|
|
use SfCod\SocketIoBundle\Service\Broadcast; |
13
|
|
|
use SfCod\SocketIoBundle\Service\EventManager; |
14
|
|
|
use SfCod\SocketIoBundle\Service\Process; |
15
|
|
|
use SfCod\SocketIoBundle\Service\RedisDriver; |
16
|
|
|
use SfCod\SocketIoBundle\Service\Worker; |
17
|
|
|
use Symfony\Component\DependencyInjection\ContainerBuilder; |
18
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
19
|
|
|
use Symfony\Component\DependencyInjection\Definition; |
20
|
|
|
use Symfony\Component\DependencyInjection\Extension\Extension; |
21
|
|
|
use Symfony\Component\DependencyInjection\Reference; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Class SocketIoExtension |
25
|
|
|
* |
26
|
|
|
* @author Virchenko Maksim <[email protected]> |
27
|
|
|
* |
28
|
|
|
* @package SfCod\SocketIoBundle\DependencyInjection |
29
|
|
|
*/ |
30
|
|
|
class SocketIoExtension extends Extension |
31
|
|
|
{ |
32
|
|
|
/** |
33
|
|
|
* Loads a specific configuration. |
34
|
|
|
* |
35
|
|
|
* @param array $configs |
36
|
|
|
* @param ContainerBuilder $container |
37
|
|
|
* |
38
|
|
|
* @throws \Exception |
39
|
|
|
*/ |
40
|
|
|
public function load(array $configs, ContainerBuilder $container) |
41
|
|
|
{ |
42
|
|
|
$configuration = new SocketIoConfiguration(); |
43
|
|
|
|
44
|
|
|
$config = $this->processConfiguration($configuration, $configs); |
45
|
|
|
|
46
|
|
|
$container->registerForAutoconfiguration(EventPublisherInterface::class) |
47
|
|
|
->addTag('socketio.publisher'); |
48
|
|
|
$container->registerForAutoconfiguration(EventSubscriberInterface::class) |
49
|
|
|
->addTag('socketio.subscriber'); |
50
|
|
|
$container->registerForAutoconfiguration(EventInterface::class) |
51
|
|
|
->addTag('socketio.events'); |
52
|
|
|
|
53
|
|
|
$this->createDriver($config, $container); |
54
|
|
|
$this->createBroadcast($config, $container); |
55
|
|
|
$this->createEventManager($config, $container); |
56
|
|
|
$this->createProcess($config, $container); |
57
|
|
|
$this->createWorker($config, $container); |
58
|
|
|
$this->createCommands($config, $container); |
59
|
|
|
|
60
|
|
|
$eventManager = $container->get(EventManager::class); |
61
|
|
|
|
62
|
|
|
foreach ($eventManager->getList() as $name => $class) { |
63
|
|
|
$definition = new Definition($class); |
64
|
|
|
$definition |
65
|
|
|
->setAutowired(true) |
66
|
|
|
->setAutoconfigured(true) |
67
|
|
|
->setPublic(true); |
68
|
|
|
$container->setDefinition(sprintf('socketio.%s', $name), $definition); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Get extension alias |
74
|
|
|
* |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getAlias() |
78
|
|
|
{ |
79
|
|
|
return 'sfcod_socketio'; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Create worker |
84
|
|
|
* |
85
|
|
|
* @param array $config |
86
|
|
|
* @param ContainerBuilder $container |
87
|
|
|
* |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
private function createWorker(array $config, ContainerBuilder $container) |
|
|
|
|
91
|
|
|
{ |
92
|
|
|
$worker = new Definition(Worker::class); |
93
|
|
|
$worker->setArguments([ |
94
|
|
|
new Reference(EventManager::class), |
95
|
|
|
new Reference(RedisDriver::class), |
96
|
|
|
new Reference(Broadcast::class), |
97
|
|
|
$container->hasParameter('kernel.logs_dir') ? |
98
|
|
|
$container->getParameter('kernel.logs_dir') . '/socketio' : |
99
|
|
|
$container->getParameter('kernel.root_dir') . '/../../var/log/socketio', |
100
|
|
|
]); |
101
|
|
|
|
102
|
|
|
$container->setDefinition(Worker::class, $worker); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Create command |
107
|
|
|
* |
108
|
|
|
* @param array $config |
109
|
|
|
* @param ContainerBuilder $container |
110
|
|
|
*/ |
111
|
|
|
private function createCommands(array $config, ContainerBuilder $container) |
|
|
|
|
112
|
|
|
{ |
113
|
|
|
$nodeJs = new Definition(NodeJsServerCommand::class); |
114
|
|
|
$nodeJs->setArguments([ |
115
|
|
|
new Reference(Worker::class), |
116
|
|
|
]); |
117
|
|
|
$nodeJs->addTag('console.command'); |
118
|
|
|
|
119
|
|
|
$phpServer = new Definition(PhpServerCommand::class); |
120
|
|
|
$phpServer->setArguments([ |
121
|
|
|
new Reference(Worker::class), |
122
|
|
|
]); |
123
|
|
|
$phpServer->addTag('console.command'); |
124
|
|
|
|
125
|
|
|
$process = new Definition(ProcessCommand::class); |
126
|
|
|
$process->setArguments([ |
127
|
|
|
new Reference(Broadcast::class), |
128
|
|
|
]); |
129
|
|
|
$process->addTag('console.command'); |
130
|
|
|
|
131
|
|
|
$container->addDefinitions([ |
132
|
|
|
PhpServerCommand::class => $phpServer, |
133
|
|
|
ProcessCommand::class => $process, |
134
|
|
|
NodeJsServerCommand::class => $nodeJs, |
135
|
|
|
]); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Create driver |
140
|
|
|
* |
141
|
|
|
* @param array $config |
142
|
|
|
* @param ContainerBuilder $container |
143
|
|
|
*/ |
144
|
|
|
private function createDriver(array $config, ContainerBuilder $container) |
|
|
|
|
145
|
|
|
{ |
146
|
|
|
$redis = new Definition(RedisDriver::class); |
147
|
|
|
$redis->setArguments([ |
148
|
|
|
getenv('REDIS_URL'), |
149
|
|
|
]); |
150
|
|
|
|
151
|
|
|
$container->setDefinition(RedisDriver::class, $redis); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Create broadcast |
156
|
|
|
* |
157
|
|
|
* @param array $config |
158
|
|
|
* @param ContainerBuilder $container |
159
|
|
|
*/ |
160
|
|
|
private function createBroadcast(array $config, ContainerBuilder $container) |
|
|
|
|
161
|
|
|
{ |
162
|
|
|
$broadcast = new Definition(Broadcast::class); |
163
|
|
|
$broadcast->setArguments([ |
164
|
|
|
new Reference(ContainerInterface::class), |
165
|
|
|
new Reference(RedisDriver::class), |
166
|
|
|
new Reference(EventManager::class), |
167
|
|
|
new Reference(LoggerInterface::class), |
168
|
|
|
new Reference(Process::class), |
169
|
|
|
]); |
170
|
|
|
|
171
|
|
|
$container->setDefinition(Broadcast::class, $broadcast); |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Create event manager |
176
|
|
|
* |
177
|
|
|
* @param array $config |
178
|
|
|
* @param ContainerBuilder $container |
179
|
|
|
*/ |
180
|
|
|
private function createEventManager(array $config, ContainerBuilder $container) |
181
|
|
|
{ |
182
|
|
|
$eventManager = new Definition(EventManager::class); |
183
|
|
|
$eventManager->setArguments([ |
184
|
|
|
$container->getParameter('kernel.root_dir'), |
185
|
|
|
$config['namespaces'], |
186
|
|
|
]); |
187
|
|
|
|
188
|
|
|
$container->setDefinition(EventManager::class, $eventManager); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
/** |
192
|
|
|
* Create process |
193
|
|
|
* |
194
|
|
|
* @param array $config |
195
|
|
|
* @param ContainerBuilder $container |
196
|
|
|
*/ |
197
|
|
|
private function createProcess(array $config, ContainerBuilder $container) |
|
|
|
|
198
|
|
|
{ |
199
|
|
|
$jobProcess = new Definition(Process::class); |
200
|
|
|
$jobProcess->setArguments([ |
201
|
|
|
'console', |
202
|
|
|
sprintf('%s/bin', $container->getParameter('kernel.project_dir')), |
203
|
|
|
]); |
204
|
|
|
|
205
|
|
|
$container->setDefinition(Process::class, $jobProcess); |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.