22
33namespace PHPFastCGI \SpeedfonyBundle \Command ;
44
5- use PHPFastCGI \FastCGIDaemon \SocketDaemon ;
6- use PHPFastCGI \FastCGIDaemon \ StreamSocketDaemon ;
5+ use PHPFastCGI \FastCGIDaemon \Daemon ;
6+ use PHPFastCGI \SpeedfonyBundle \ Bridge \ KernelWrapper ;
77use Symfony \Component \Console \Command \Command ;
8- use Symfony \Component \Console \Input \InputArgument ;
98use Symfony \Component \Console \Input \InputInterface ;
109use Symfony \Component \Console \Input \InputOption ;
1110use Symfony \Component \Console \Output \OutputInterface ;
12- use Symfony \Component \HttpFoundation \Request ;
1311use Symfony \Component \HttpKernel \Kernel ;
1412
1513class DaemonRunCommand extends Command
@@ -28,53 +26,22 @@ protected function configure()
2826 $ this
2927 ->setName ('speedfony:daemon:run ' )
3028 ->setDescription ('Execute the FCGI daemon ' )
31- ->addArgument ('cycles ' , InputArgument::OPTIONAL , 'Request cycles to live for, 0 means infinite (default is 20) ' , 20 )
32- ->addOption ('port ' , null , InputOption::VALUE_REQUIRED , 'Port to listen on ' );
29+ ->addOption ('target ' , null , InputOption::VALUE_REQUIRED , 'Socket path ' );
3330 }
3431
3532 protected function execute (InputInterface $ input , OutputInterface $ output )
3633 {
37- $ getIntegerInput = function ($ type , $ name , $ minimumValue ) use ($ input ) {
38- if ('argument ' === $ type ) {
39- $ value = (string ) $ input ->getArgument ($ name );
40- } elseif ('option ' === $ type ) {
41- $ value = (string ) $ input ->getOption ($ name );
42- } else {
43- throw new \LogicException ('Unknown input type: ' . $ type );
44- }
34+ $ target = $ input ->getOption ('target ' );
4535
46- $ intValue = (int ) $ value ;
47-
48- if ((string ) $ intValue !== $ value ) {
49- throw new \Exception ('The ' . $ argument . ' argument must be an integer ' );
50- } elseif ($ value < $ minimumValue ) {
51- throw new \Exception ('The ' . $ argument . ' argument must be at least ' . $ minimumValue );
52- }
53-
54- return $ value ;
55- };
56-
57- if (null !== $ input ->getOption ('port ' )) {
58- $ daemon = new SocketDaemon ($ getIntegerInput ('option ' , 'port ' , 0 ));
36+ if (null !== $ target ) {
37+ $ stream = stream_socket_server ($ target );
5938 } else {
60- $ daemon = new StreamSocketDaemon ( );
39+ $ stream = fopen ( ' php://fd/ ' . Daemon:: FCGI_LISTENSOCK_FILENO , ' r ' );
6140 }
6241
63- $ maximumCycles = $ getIntegerInput ('argument ' , 'cycles ' , 0 );
64-
65- for ($ cycles = 0 ; ($ maximumCycles == 0 ) || $ cycles < $ maximumCycles ; $ cycles ++) {
66- $ request = $ daemon ->getRequest ();
42+ $ kernelWrapper = new KernelWrapper ($ this ->kernel );
6743
68- $ httpRequest = new Request (array (), array (), array (), array (),
69- array (), $ request ->getServer (), $ request ->getContent ());
70-
71- $ response = str_replace (
72- array ('HTTP/1.1 ' , 'HTTP/1.0 ' ),
73- array ('Status: ' , 'Status: ' ),
74- (string ) $ this ->kernel ->handle ($ httpRequest )
75- );
76-
77- $ request ->respond ($ response );
78- }
44+ $ daemon = new Daemon ($ stream );
45+ $ daemon ->run ($ kernelWrapper );
7946 }
8047}
0 commit comments