-
Notifications
You must be signed in to change notification settings - Fork 16
/
DaemonFactory.php
56 lines (46 loc) · 1.49 KB
/
DaemonFactory.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
<?php
namespace PHPFastCGI\FastCGIDaemon;
use PHPFastCGI\FastCGIDaemon\Connection\StreamSocketConnectionPool;
use PHPFastCGI\FastCGIDaemon\ConnectionHandler\ConnectionHandlerFactory;
/**
* The default implementation of the DaemonFactoryInterface.
*/
class DaemonFactory implements DaemonFactoryInterface
{
/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public function createDaemon($kernel)
{
$socket = fopen('php://fd/'.DaemonInterface::FCGI_LISTENSOCK_FILENO, 'r');
if (false === $socket) {
throw new \RuntimeException('Could not open FCGI_LISTENSOCK_FILENO');
}
return $this->createDaemonFromStreamSocket($kernel, $socket);
}
/**
* {@inheritdoc}
*
* @codeCoverageIgnore
*/
public function createTcpDaemon($kernel, $port, $host = 'localhost')
{
$address = 'tcp://'.$host.':'.$port;
$socket = stream_socket_server($address);
if (false === $socket) {
throw new \RuntimeException('Could not create stream socket server on: '.$address);
}
return $this->createDaemonFromStreamSocket($kernel, $socket);
}
/**
* {@inheritdoc}
*/
public function createDaemonFromStreamSocket($kernel, $socket)
{
$connectionHandlerFactory = new ConnectionHandlerFactory($kernel);
$connectionPool = new StreamSocketConnectionPool($socket);
return new Daemon($connectionPool, $connectionHandlerFactory);
}
}