|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace PHPFastCGI\SpeedfonyBundle\Command; |
| 4 | + |
| 5 | +use PHPFastCGI\FastCGIDaemon\SocketDaemon; |
| 6 | +use PHPFastCGI\FastCGIDaemon\StreamSocketDaemon; |
| 7 | +use Symfony\Component\Console\Command\Command; |
| 8 | +use Symfony\Component\Console\Input\InputArgument; |
| 9 | +use Symfony\Component\Console\Input\InputInterface; |
| 10 | +use Symfony\Component\Console\Output\OutputInterface; |
| 11 | +use Symfony\Component\HttpFoundation\Request; |
| 12 | +use Symfony\Component\HttpKernel\Kernel; |
| 13 | + |
| 14 | +class DaemonRunCommand extends Command |
| 15 | +{ |
| 16 | + private $kernel; |
| 17 | + |
| 18 | + public function __construct(Kernel $kernel) |
| 19 | + { |
| 20 | + $this->kernel = $kernel; |
| 21 | + |
| 22 | + parent::__construct(); |
| 23 | + } |
| 24 | + |
| 25 | + protected function configure() |
| 26 | + { |
| 27 | + $this |
| 28 | + ->setName('speedfony:daemon:run') |
| 29 | + ->setDescription('Execute the FCGI daemon') |
| 30 | + ->addArgument('socket', InputArgument::OPTIONAL, 'The socket stream to listen on') |
| 31 | + ->addArgument('port', InputArgument::OPTIONAL, 'Port to listen on') |
| 32 | + ->addArgument('cycles', InputArgument::OPTIONAL, 'Request cycles to live for, 0 means infinite', 0); |
| 33 | + } |
| 34 | + |
| 35 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 36 | + { |
| 37 | + $getIntegerArgument = function ($argument, $minimumValue) use ($input) { |
| 38 | + $value = (string) $input->getArgument($argument); |
| 39 | + $intValue = (int) $value; |
| 40 | + |
| 41 | + if ((string) $intValue !== $value) { |
| 42 | + throw new \Exception('The ' . $argument . ' argument must be an integer'); |
| 43 | + } elseif ($value < $minimumValue) { |
| 44 | + throw new \Exception('The ' . $argument . ' argument must be at least ' . $minimumValue); |
| 45 | + } |
| 46 | + |
| 47 | + return $value; |
| 48 | + }; |
| 49 | + |
| 50 | + if ($input->hasArgument('socket')) { |
| 51 | + $daemon = new StreamSocketDaemon($input->getArgument('socket')); |
| 52 | + } elseif ($input->hasArgument('port')) { |
| 53 | + $daemon = new SocketDaemon($getIntegerArgument('port', 0)); |
| 54 | + } else { |
| 55 | + throw new \Exception('You must specify either the socket or port argument'); |
| 56 | + } |
| 57 | + |
| 58 | + $maximumCycles = $getIntegerArgument('cycles', 0); |
| 59 | + |
| 60 | + for($cycles = 0; ($maximumCycles == 0) || $cycles < $maximumCycles; $cycles++) { |
| 61 | + $request = $daemon->getRequest(); |
| 62 | + |
| 63 | + $httpRequest = new Request(array(), array(), array(), array(), |
| 64 | + array(), $request->getServer(), $request->getContent()); |
| 65 | + |
| 66 | + $response = str_replace( |
| 67 | + array('HTTP/1.1 ', 'HTTP/1.0 '), |
| 68 | + array('Status: ', 'Status: '), |
| 69 | + (string) $this->kernel->handle($httpRequest) |
| 70 | + ); |
| 71 | + |
| 72 | + $request->respond($response); |
| 73 | + } |
| 74 | + } |
| 75 | +} |
0 commit comments