22
33namespace PHPFastCGI \FastCGIDaemon \Command ;
44
5- use PHPFastCGI \FastCGIDaemon \DaemonFactory ;
6- use PHPFastCGI \FastCGIDaemon \DaemonFactoryInterface ;
5+ use PHPFastCGI \FastCGIDaemon \DaemonOptions ;
6+ use PHPFastCGI \FastCGIDaemon \Driver \ DriverContainerInterface ;
77use PHPFastCGI \FastCGIDaemon \KernelInterface ;
88use Symfony \Component \Console \Command \Command ;
99use Symfony \Component \Console \Input \InputInterface ;
@@ -17,43 +17,61 @@ class DaemonRunCommand extends Command
1717 const DEFAULT_DESCRIPTION = 'Run the FastCGI daemon ' ;
1818
1919 /**
20- * @var DaemonFactoryInterface
20+ * @var KernelInterface
2121 */
22- private $ daemonFactory ;
22+ private $ kernel ;
2323
2424 /**
25- * @var KernelInterface|callable
25+ * @var DriverContainerInterface
2626 */
27- private $ kernel ;
27+ private $ driverContainer ;
2828
2929 /**
3030 * Constructor.
3131 *
32- * @param KernelInterface|callable $kernel The kernel to be given to the daemon
33- * @param DaemonFactoryInterface $daemonFactory The factory to use to create the daemon
34- * @param string $name The name of the daemon run command
35- * @param string $description The description of the daemon run command
32+ * @param KernelInterface $kernel The kernel to be given to the daemon
33+ * @param DriverContainerInterface $driverContainer The driver container
34+ * @param string $name The name of the daemon run command
35+ * @param string $description The description of the daemon run command
3636 */
37- public function __construct ($ kernel , DaemonFactoryInterface $ daemonFactory = null , $ name = null , $ description = null )
37+ public function __construct (KernelInterface $ kernel , DriverContainerInterface $ driverContainer , $ name = null , $ description = null )
3838 {
39- $ daemonFactory = $ daemonFactory ?: new DaemonFactory ;
40- $ name = $ name ?: self ::DEFAULT_NAME ;
41- $ description = $ description ?: self ::DEFAULT_DESCRIPTION ;
39+ $ this ->kernel = $ kernel ;
40+ $ this ->driverContainer = $ driverContainer ;
41+
42+ $ name = $ name ?: self ::DEFAULT_NAME ;
43+ $ description = $ description ?: self ::DEFAULT_DESCRIPTION ;
4244
4345 parent ::__construct ($ name );
4446
4547 $ this
4648 ->setDescription ($ description )
47- ->addOption ('port ' , null , InputOption::VALUE_OPTIONAL , 'TCP port to listen on (if not present, daemon will listen on FCGI_LISTENSOCK_FILENO) ' )
48- ->addOption ('host ' , null , InputOption::VALUE_OPTIONAL , 'TCP host to listen on ' );
49+ ->addOption ('port ' , null , InputOption::VALUE_OPTIONAL , 'TCP port to listen on (if not present, daemon will listen on FCGI_LISTENSOCK_FILENO) ' )
50+ ->addOption ('host ' , null , InputOption::VALUE_OPTIONAL , 'TCP host to listen on ' )
51+ ->addOption ('request-limit ' , null , InputOption::VALUE_OPTIONAL , 'The maximum number of requests to handle before shutting down ' )
52+ ->addOption ('memory-limit ' , null , InputOption::VALUE_OPTIONAL , 'The memory limit on the daemon instance before shutting down ' )
53+ ->addOption ('time-limit ' , null , InputOption::VALUE_OPTIONAL , 'The time limit on the daemon in seconds before shutting down ' )
54+ ->addOption ('driver ' , null , InputOption::VALUE_OPTIONAL , 'The implementation of the FastCGI protocol to use ' , 'userland ' );
55+ }
4956
50- $ this ->daemonFactory = $ daemonFactory ;
57+ /**
58+ * Retrieves the daemon configuration from the Symfony command input and
59+ * output objects.
60+ *
61+ * @param InputInterface $input The Symfony command input
62+ * @param OutputInterface $output The Symfony command output
63+ *
64+ * @return DaemonOptions The daemon configuration
65+ */
66+ private function getDaemonOptions (InputInterface $ input , OutputInterface $ output )
67+ {
68+ $ logger = new ConsoleLogger ($ output );
5169
52- if (! $ kernel instanceof KernelInterface && ! is_callable ( $ kernel )) {
53- throw new \ InvalidArgumentException ( ' Kernel parameter must be an instance of KernelInterface or a callable ' ) ;
54- }
70+ $ requestLimit = $ input -> getOption ( ' request-limit ' ) ?: DaemonOptions:: NO_LIMIT ;
71+ $ memoryLimit = $ input -> getOption ( ' memory-limit ' ) ?: DaemonOptions:: NO_LIMIT ;
72+ $ timeLimit = $ input -> getOption ( ' time-limit ' ) ?: DaemonOptions:: NO_LIMIT ;
5573
56- $ this -> kernel = $ kernel ;
74+ return new DaemonOptions ( $ logger , $ requestLimit , $ memoryLimit , $ timeLimit ) ;
5775 }
5876
5977 /**
@@ -64,24 +82,22 @@ protected function execute(InputInterface $input, OutputInterface $output)
6482 $ port = $ input ->getOption ('port ' );
6583 $ host = $ input ->getOption ('host ' );
6684
85+ $ daemonOptions = $ this ->getDaemonOptions ($ input , $ output );
86+
87+ $ driver = $ input ->getOption ('driver ' );
88+ $ daemonFactory = $ this ->driverContainer ->getFactory ($ driver );
89+
6790 if (null !== $ port ) {
6891 // If we have the port, create a TCP daemon
69-
70- if (null !== $ host ) {
71- $ daemon = $ this ->daemonFactory ->createTcpDaemon ($ this ->kernel , $ port , $ host );
72- } else {
73- $ daemon = $ this ->daemonFactory ->createTcpDaemon ($ this ->kernel , $ port );
74- }
92+ $ daemon = $ daemonFactory ->createTcpDaemon ($ this ->kernel , $ daemonOptions , $ host ?: 'localhost ' , $ port );
7593 } elseif (null !== $ host ) {
7694 // If we have the host but not the port, we cant create a TCP daemon - throw exception
7795 throw new \InvalidArgumentException ('TCP port option must be set if host option is set ' );
7896 } else {
7997 // With no host or port, listen on FCGI_LISTENSOCK_FILENO (default)
80- $ daemon = $ this -> daemonFactory ->createDaemon ($ this ->kernel );
98+ $ daemon = $ daemonFactory ->createDaemon ($ this ->kernel , $ daemonOptions );
8199 }
82100
83- $ daemon ->setLogger (new ConsoleLogger ($ output ));
84-
85101 $ daemon ->run ();
86102 }
87103}
0 commit comments