1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace PHPFastCGI\FastCGIDaemon\Command; |
6
|
|
|
|
7
|
|
|
use PHPFastCGI\FastCGIDaemon\DaemonInterface; |
8
|
|
|
use PHPFastCGI\FastCGIDaemon\DaemonOptions; |
9
|
|
|
use PHPFastCGI\FastCGIDaemon\DaemonOptionsInterface; |
10
|
|
|
use PHPFastCGI\FastCGIDaemon\Driver\DriverContainerInterface; |
11
|
|
|
use PHPFastCGI\FastCGIDaemon\KernelInterface; |
12
|
|
|
use Symfony\Component\Console\Command\Command; |
13
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
14
|
|
|
use Symfony\Component\Console\Input\InputOption; |
15
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
16
|
|
|
use Symfony\Component\Console\Logger\ConsoleLogger; |
17
|
|
|
|
18
|
|
|
class DaemonRunCommand extends Command |
19
|
|
|
{ |
20
|
|
|
const DEFAULT_NAME = 'run'; |
21
|
|
|
const DEFAULT_DESCRIPTION = 'Run the FastCGI daemon'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var KernelInterface |
25
|
|
|
*/ |
26
|
|
|
private $kernel; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var DriverContainerInterface |
30
|
|
|
*/ |
31
|
|
|
private $driverContainer; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var DaemonInterface |
35
|
|
|
*/ |
36
|
|
|
private $daemon; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Constructor. |
40
|
|
|
* |
41
|
|
|
* @param KernelInterface $kernel The kernel to be given to the daemon |
42
|
|
|
* @param DriverContainerInterface $driverContainer The driver container |
43
|
|
|
* @param string $name The name of the daemon run command |
44
|
|
|
* @param string $description The description of the daemon run command |
45
|
|
|
*/ |
46
|
|
|
public function __construct(KernelInterface $kernel, DriverContainerInterface $driverContainer, $name = null, $description = null) |
47
|
|
|
{ |
48
|
|
|
$this->kernel = $kernel; |
49
|
|
|
$this->driverContainer = $driverContainer; |
50
|
|
|
$this->daemon = null; |
51
|
|
|
|
52
|
|
|
$name = $name ?: self::DEFAULT_NAME; |
53
|
|
|
$description = $description ?: self::DEFAULT_DESCRIPTION; |
54
|
|
|
|
55
|
|
|
parent::__construct($name); |
56
|
|
|
|
57
|
|
|
$this |
58
|
|
|
->setDescription($description) |
59
|
|
|
->addOption('port', null, InputOption::VALUE_OPTIONAL, 'TCP port to listen on (if not present, daemon will listen on FCGI_LISTENSOCK_FILENO)') |
60
|
|
|
->addOption('host', null, InputOption::VALUE_OPTIONAL, 'TCP host to listen on') |
61
|
|
|
->addOption('fd', null, InputOption::VALUE_OPTIONAL, 'File descriptor to listen on - defaults to FCGI_LISTENSOCK_FILENO', DaemonInterface::FCGI_LISTENSOCK_FILENO) |
62
|
|
|
->addOption('request-limit', null, InputOption::VALUE_OPTIONAL, 'The maximum number of requests to handle before shutting down') |
63
|
|
|
->addOption('memory-limit', null, InputOption::VALUE_OPTIONAL, 'The memory limit on the daemon instance before shutting down') |
64
|
|
|
->addOption('time-limit', null, InputOption::VALUE_OPTIONAL, 'The time limit on the daemon in seconds before shutting down') |
65
|
|
|
->addOption('auto-shutdown', null, InputOption::VALUE_NONE, 'Perform a graceful shutdown after receiving a 5XX HTTP status code') |
66
|
|
|
->addOption('driver', null, InputOption::VALUE_OPTIONAL, 'The implementation of the FastCGI protocol to use', 'userland'); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Creates a daemon configuration object from the Symfony command input and |
71
|
|
|
* output objects. |
72
|
|
|
* |
73
|
|
|
* @param InputInterface $input The Symfony command input |
74
|
|
|
* @param OutputInterface $output The Symfony command output |
75
|
|
|
* |
76
|
|
|
* @return DaemonOptionsInterface The daemon configuration |
77
|
|
|
*/ |
78
|
|
|
private function getDaemonOptions(InputInterface $input, OutputInterface $output): DaemonOptionsInterface |
79
|
|
|
{ |
80
|
|
|
$logger = new ConsoleLogger($output); |
81
|
|
|
|
82
|
|
|
$requestLimit = $input->getOption('request-limit') ?: DaemonOptions::NO_LIMIT; |
83
|
|
|
$memoryLimit = $input->getOption('memory-limit') ?: DaemonOptions::NO_LIMIT; |
84
|
|
|
$timeLimit = $input->getOption('time-limit') ?: DaemonOptions::NO_LIMIT; |
85
|
|
|
$autoShutdown = $input->getOption('auto-shutdown'); |
86
|
|
|
|
87
|
|
|
return new DaemonOptions([ |
88
|
|
|
DaemonOptions::LOGGER => $logger, |
89
|
|
|
DaemonOptions::REQUEST_LIMIT => $requestLimit, |
90
|
|
|
DaemonOptions::MEMORY_LIMIT => $memoryLimit, |
91
|
|
|
DaemonOptions::TIME_LIMIT => $timeLimit, |
92
|
|
|
DaemonOptions::AUTO_SHUTDOWN => $autoShutdown, |
93
|
|
|
]); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* {@inheritdoc} |
98
|
|
|
*/ |
99
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
100
|
|
|
{ |
101
|
|
|
$port = $input->getOption('port'); |
102
|
|
|
$host = $input->getOption('host'); |
103
|
|
|
$fd = $input->getOption('fd'); |
104
|
|
|
|
105
|
|
|
$daemonOptions = $this->getDaemonOptions($input, $output); |
106
|
|
|
|
107
|
|
|
$driver = $input->getOption('driver'); |
108
|
|
|
$daemonFactory = $this->driverContainer->getFactory($driver); |
109
|
|
|
|
110
|
|
|
if (null !== $port) { |
111
|
|
|
// If we have the port, create a TCP daemon |
112
|
|
|
$this->daemon = $daemonFactory->createTcpDaemon($this->kernel, $daemonOptions, $host ?: 'localhost', (int) $port); |
|
|
|
|
113
|
|
|
} elseif (null !== $host) { |
114
|
|
|
// If we have the host but not the port, we cant create a TCP daemon - throw exception |
115
|
|
|
throw new \InvalidArgumentException('TCP port option must be set if host option is set'); |
116
|
|
|
} else { |
117
|
|
|
// With no host or port, listen on FCGI_LISTENSOCK_FILENO (default) |
118
|
|
|
$this->daemon = $daemonFactory->createDaemon($this->kernel, $daemonOptions, (int) $fd); |
|
|
|
|
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
$this->daemon->run(); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Flag the daemon for shutdown. |
126
|
|
|
*/ |
127
|
|
|
public function flagShutdown() |
128
|
|
|
{ |
129
|
|
|
if (null === $this->daemon) { |
130
|
|
|
throw new \RuntimeException('There is no daemon running'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$this->daemon->flagShutdown(); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.