1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* WebSocketsSessionExtension.php |
4
|
|
|
* |
5
|
|
|
* @copyright More in license.md |
6
|
|
|
* @license http://www.ipublikuj.eu |
7
|
|
|
* @author Adam Kadlec http://www.ipublikuj.eu |
8
|
|
|
* @package iPublikuj:WebSocketSession! |
9
|
|
|
* @subpackage DI |
10
|
|
|
* @since 1.0.0 |
11
|
|
|
* |
12
|
|
|
* @date 01.03.17 |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
declare(strict_types = 1); |
16
|
|
|
|
17
|
|
|
namespace IPub\WebSocketsSession\DI; |
18
|
|
|
|
19
|
|
|
use Nette; |
20
|
|
|
use Nette\DI; |
21
|
|
|
use Nette\Http; |
22
|
|
|
|
23
|
|
|
use IPub; |
24
|
|
|
use IPub\WebSocketsSession; |
25
|
|
|
use IPub\WebSocketsSession\Events; |
26
|
|
|
use IPub\WebSocketsSession\Serializers; |
27
|
|
|
use IPub\WebSocketsSession\Session; |
28
|
|
|
use IPub\WebSocketsSession\Users; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* WebSockets session extension container |
32
|
|
|
* |
33
|
|
|
* @package iPublikuj:WebSocketSession! |
34
|
|
|
* @subpackage DI |
35
|
|
|
* |
36
|
|
|
* @author Adam Kadlec <[email protected]> |
37
|
|
|
* |
38
|
|
|
* @method DI\ContainerBuilder getContainerBuilder() |
39
|
|
|
* @method array getConfig(array $default) |
40
|
|
|
* @method string prefix($id) |
41
|
|
|
*/ |
42
|
1 |
|
final class WebSocketsSessionExtension extends DI\CompilerExtension |
43
|
|
|
{ |
44
|
|
|
/** |
45
|
|
|
* @var array |
46
|
|
|
*/ |
47
|
|
|
private $defaults = [ |
48
|
|
|
'enable' => TRUE, |
49
|
|
|
]; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* {@inheritdoc} |
53
|
|
|
*/ |
54
|
|
|
public function loadConfiguration() |
55
|
|
|
{ |
56
|
1 |
|
parent::loadConfiguration(); |
57
|
|
|
|
58
|
|
|
/** @var DI\ContainerBuilder $builder */ |
59
|
1 |
|
$builder = $this->getContainerBuilder(); |
60
|
|
|
/** @var array $configuration */ |
61
|
1 |
|
$configuration = $this->getConfig($this->defaults); |
62
|
|
|
|
63
|
1 |
|
if ($configuration['enable']) { |
64
|
|
|
/** |
65
|
|
|
* SESSION |
66
|
|
|
*/ |
67
|
|
|
|
68
|
1 |
|
$builder->addDefinition($this->prefix('serializer.session')) |
69
|
1 |
|
->setClass(Serializers\ISessionSerializer::class) |
70
|
1 |
|
->setFactory(Serializers\SessionSerializerFactory::class . '::create'); |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* USERS |
74
|
|
|
*/ |
75
|
|
|
|
76
|
1 |
|
$builder->addDefinition($this->prefix('users.repository')) |
77
|
1 |
|
->setClass(Users\Repository::class); |
78
|
|
|
} |
79
|
1 |
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* {@inheritdoc} |
83
|
|
|
*/ |
84
|
|
|
public function beforeCompile() |
85
|
|
|
{ |
86
|
1 |
|
parent::beforeCompile(); |
87
|
|
|
|
88
|
|
|
/** @var DI\ContainerBuilder $builder */ |
89
|
1 |
|
$builder = $this->getContainerBuilder(); |
90
|
|
|
/** @var array $configuration */ |
91
|
1 |
|
$configuration = $this->getConfig($this->defaults); |
92
|
|
|
|
93
|
1 |
|
if ($configuration['enable']) { |
94
|
|
|
// Sessions switcher |
95
|
1 |
|
$original = $builder->getDefinition($originalSessionServiceName = $builder->getByType(Http\Session::class) ?: 'session'); |
96
|
|
|
|
97
|
1 |
|
$builder->removeDefinition($originalSessionServiceName); |
98
|
|
|
|
99
|
1 |
|
$builder->addDefinition($this->prefix('session.original'), $original) |
100
|
1 |
|
->setAutowired(FALSE); |
101
|
|
|
|
102
|
1 |
|
$switchableSession = $builder->addDefinition($originalSessionServiceName) |
103
|
1 |
|
->setClass(Http\Session::class) |
104
|
1 |
|
->setFactory(Session\SwitchableSession::class, [$this->prefix('@session.original')]); |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* EVENTS |
108
|
|
|
*/ |
109
|
|
|
|
110
|
1 |
|
$builder->addDefinition($this->prefix('events.onClientConnected')) |
111
|
1 |
|
->setClass(Events\OnClientConnectedHandler::class) |
112
|
1 |
|
->setArguments(['session' => $switchableSession]); |
113
|
|
|
|
114
|
1 |
|
$builder->addDefinition($this->prefix('events.onClientDisconnected')) |
115
|
1 |
|
->setClass(Events\OnClientDisconnectedHandler::class) |
116
|
1 |
|
->setArguments(['session' => $switchableSession]); |
117
|
|
|
|
118
|
1 |
|
$builder->addDefinition($this->prefix('events.onIncomingMessage')) |
119
|
1 |
|
->setClass(Events\OnIncomingMessageHandler::class) |
120
|
1 |
|
->setArguments(['session' => $switchableSession]); |
121
|
|
|
|
122
|
1 |
|
$builder->addDefinition($this->prefix('events.onAfterIncomingMessage')) |
123
|
1 |
|
->setClass(Events\OnAfterIncomingMessageHandler::class) |
124
|
1 |
|
->setArguments(['session' => $switchableSession]); |
125
|
|
|
|
126
|
1 |
|
$serverWrapper = $builder->getDefinitionByType(IPub\WebSockets\Server\Wrapper::class); |
127
|
1 |
|
$serverWrapper->addSetup('$service->onClientConnected[] = ?', ['@' . $this->prefix('events.onClientConnected')]); |
128
|
1 |
|
$serverWrapper->addSetup('$service->onClientDisconnected[] = ?', ['@' . $this->prefix('events.onClientDisconnected')]); |
129
|
1 |
|
$serverWrapper->addSetup('$service->onIncomingMessage[] = ?', ['@' . $this->prefix('events.onIncomingMessage')]); |
130
|
1 |
|
$serverWrapper->addSetup('$service->onAfterIncomingMessage[] = ?', ['@' . $this->prefix('events.onAfterIncomingMessage')]); |
131
|
|
|
} |
132
|
1 |
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @param Nette\Configurator $config |
136
|
|
|
* @param string $extensionName |
137
|
|
|
* |
138
|
|
|
* @return void |
139
|
|
|
*/ |
140
|
|
|
public static function register(Nette\Configurator $config, string $extensionName = 'webSocketsSession') |
141
|
|
|
{ |
142
|
|
|
$config->onCompile[] = function (Nette\Configurator $config, DI\Compiler $compiler) use ($extensionName) { |
143
|
|
|
$compiler->addExtension($extensionName, new WebSocketsSessionExtension()); |
144
|
|
|
}; |
145
|
|
|
} |
146
|
|
|
} |
147
|
|
|
|