-
Notifications
You must be signed in to change notification settings - Fork 15
/
NetworkProvider.php
80 lines (68 loc) · 1.95 KB
/
NetworkProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
namespace App\Provider\Network;
use App\Component\Http\Http;
use Kraken\Container\ContainerInterface;
use Kraken\Container\ServiceProvider;
use Kraken\Container\ServiceProviderInterface;
use Kraken\Ipc\Socket\SocketListener;
use Kraken\Network\NetworkServer;
class NetworkProvider extends ServiceProvider implements ServiceProviderInterface
{
/**
* @var string[]
*/
protected $requires = [
'Kraken\Loop\LoopInterface',
];
/**
* @var string[]
*/
protected $provides = [
'Kraken\Network\NetworkServerInterface',
'App\Component\Http\HttpInterface'
];
/**
* @param ContainerInterface $container
*/
protected function register(ContainerInterface $container)
{
$loop = $container->make('Kraken\Loop\LoopInterface');
$listener = new SocketListener('tcp://127.0.0.1:6080', $loop);
$server = new NetworkServer($listener);
$http = new Http($server);
$container->instance(
'Kraken\Network\NetworkServerInterface',
$server
);
$container->instance(
'App\Component\Http\HttpInterface',
$http
);
}
/**
* @param ContainerInterface $container
*/
protected function unregister(ContainerInterface $container)
{
$container->remove(
'Kraken\Network\NetworkServerInterface'
);
$container->remove(
'App\Component\Http\HttpInterface'
);
}
/**
* @param ContainerInterface $container
*/
protected function boot(ContainerInterface $container)
{
$runtime = $container->make('Kraken\Runtime\RuntimeContainerInterface');
$http = $container->make('App\Component\Http\HttpInterface');
$runtime->onStart(function() use($http) {
$http->start();
});
$runtime->onStop(function() use($http) {
$http->stop();
});
}
}