1: <?php
2:
3: namespace PHPFastCGI\FastCGIDaemon;
4:
5: use PHPFastCGI\FastCGIDaemon\Connection\ConnectionPoolInterface;
6: use PHPFastCGI\FastCGIDaemon\ConnectionHandler\ConnectionHandlerFactoryInterface;
7: use PHPFastCGI\FastCGIDaemon\Exception\ShutdownException;
8: use Psr\Log\LoggerAwareInterface;
9: use Psr\Log\LoggerAwareTrait;
10: use Psr\Log\LoggerInterface;
11: use Psr\Log\NullLogger;
12:
13: 14: 15: 16:
17: class Daemon implements DaemonInterface, LoggerAwareInterface
18: {
19: use LoggerAwareTrait;
20:
21: 22: 23:
24: private $connectionPool;
25:
26: 27: 28:
29: private $connectionHandlerFactory;
30:
31: 32: 33: 34: 35: 36: 37:
38: public function __construct(ConnectionPoolInterface $connectionPool, ConnectionHandlerFactoryInterface $connectionHandlerFactory, LoggerInterface $logger = null)
39: {
40: $this->connectionPool = $connectionPool;
41: $this->connectionHandlerFactory = $connectionHandlerFactory;
42:
43: $this->setLogger((null === $logger) ? new NullLogger() : $logger);
44:
45: if ($this->connectionPool instanceof LoggerAwareInterface) {
46: $this->connectionPool->setLogger($this->logger);
47: }
48:
49: if ($this->connectionHandlerFactory instanceof LoggerAwareInterface) {
50: $this->connectionHandlerFactory->setLogger($this->logger);
51: }
52: }
53:
54: 55: 56:
57: public function run()
58: {
59: declare(ticks = 1);
60:
61: pcntl_signal(SIGINT, function () {
62: $this->connectionPool->shutdown();
63: throw new ShutdownException('Received SIGINT, shutting down...');
64: });
65:
66: try {
67: while (1) {
68: $this->connectionPool->operate($this->connectionHandlerFactory, 5);
69:
70: }
71:
72: } catch (ShutdownException $exception) {
73: $this->logger->notice($exception->getMessage());
74: } catch (\RuntimeException $exception) {
75: $this->logger->emergency($exception->getMessage());
76: throw $exception;
77: }
78: }
79: }
80: