1: <?php
2:
3: namespace PHPFastCGI\FastCGIDaemon;
4:
5: use PHPFastCGI\FastCGIDaemon\Connection\StreamSocketConnectionPool;
6: use PHPFastCGI\FastCGIDaemon\ConnectionHandler\ConnectionHandlerFactory;
7:
8: 9: 10:
11: class DaemonFactory implements DaemonFactoryInterface
12: {
13: 14: 15: 16: 17:
18: public function createDaemon($kernel)
19: {
20: $socket = fopen('php://fd/'.DaemonInterface::FCGI_LISTENSOCK_FILENO, 'r');
21:
22: if (false === $socket) {
23: throw new \RuntimeException('Could not open FCGI_LISTENSOCK_FILENO');
24: }
25:
26: return $this->createDaemonFromStreamSocket($kernel, $socket);
27: }
28:
29: 30: 31: 32: 33:
34: public function createTcpDaemon($kernel, $port, $host = 'localhost')
35: {
36: $address = 'tcp://'.$host.':'.$port;
37: $socket = stream_socket_server($address);
38:
39: if (false === $socket) {
40: throw new \RuntimeException('Could not create stream socket server on: '.$address);
41: }
42:
43: return $this->createDaemonFromStreamSocket($kernel, $socket);
44: }
45:
46: 47: 48:
49: public function createDaemonFromStreamSocket($kernel, $socket)
50: {
51: $connectionHandlerFactory = new ConnectionHandlerFactory($kernel);
52: $connectionPool = new StreamSocketConnectionPool($socket);
53:
54: return new Daemon($connectionPool, $connectionHandlerFactory);
55: }
56: }
57: