1: <?php
2:
3: namespace PHPFastCGI\FastCGIDaemon\Command;
4:
5: use PHPFastCGI\FastCGIDaemon\DaemonFactory;
6: use PHPFastCGI\FastCGIDaemon\DaemonFactoryInterface;
7: use PHPFastCGI\FastCGIDaemon\KernelInterface;
8: use Symfony\Component\Console\Command\Command;
9: use Symfony\Component\Console\Input\InputInterface;
10: use Symfony\Component\Console\Input\InputOption;
11: use Symfony\Component\Console\Output\OutputInterface;
12: use Symfony\Component\Console\Logger\ConsoleLogger;
13:
14: class DaemonRunCommand extends Command
15: {
16: const DEFAULT_NAME = 'run';
17: const DEFAULT_DESCRIPTION = 'Run the FastCGI daemon';
18:
19: 20: 21:
22: private $daemonFactory;
23:
24: 25: 26:
27: private $kernel;
28:
29: 30: 31: 32: 33: 34: 35: 36:
37: public function __construct($kernel, DaemonFactoryInterface $daemonFactory = null, $name = null, $description = null)
38: {
39: $daemonFactory = $daemonFactory ?: new DaemonFactory;
40: $name = $name ?: self::DEFAULT_NAME;
41: $description = $description ?: self::DEFAULT_DESCRIPTION;
42:
43: parent::__construct($name);
44:
45: $this
46: ->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:
50: $this->daemonFactory = $daemonFactory;
51:
52: if (!$kernel instanceof KernelInterface && !is_callable($kernel)) {
53: throw new \InvalidArgumentException('Kernel parameter must be an instance of KernelInterface or a callable');
54: }
55:
56: $this->kernel = $kernel;
57: }
58:
59: 60: 61:
62: protected function execute(InputInterface $input, OutputInterface $output)
63: {
64: $port = $input->getOption('port');
65: $host = $input->getOption('host');
66:
67: if (null !== $port) {
68:
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: }
75: } elseif (null !== $host) {
76:
77: throw new \InvalidArgumentException('TCP port option must be set if host option is set');
78: } else {
79:
80: $daemon = $this->daemonFactory->createDaemon($this->kernel);
81: }
82:
83: $daemon->setLogger(new ConsoleLogger($output));
84:
85: $daemon->run();
86: }
87: }
88: