Skip to content

Commit 79cc26a

Browse files
Merge pull request #16 from PHPFastCGI/v0.7-refactor
v0.7 Branch
2 parents 9dd2db8 + 8644f05 commit 79cc26a

File tree

63 files changed

+1826
-1197
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1826
-1197
lines changed

src/ApplicationFactory.php

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,28 @@
33
namespace PHPFastCGI\FastCGIDaemon;
44

55
use PHPFastCGI\FastCGIDaemon\Command\DaemonRunCommand;
6+
use PHPFastCGI\FastCGIDaemon\Driver\DriverContainer;
7+
use PHPFastCGI\FastCGIDaemon\Driver\DriverContainerInterface;
68
use Symfony\Component\Console\Application;
9+
710
/**
811
* The default implementation of the ApplicationFactoryInterface.
912
*/
1013
class ApplicationFactory implements ApplicationFactoryInterface
1114
{
1215
/**
13-
* @var DaemonFactoryInterface
16+
* @var DriverContainerInterface
1417
*/
15-
private $daemonFactory;
18+
private $driverContainer;
1619

1720
/**
1821
* Constructor.
19-
*
20-
* @param DaemonFactoryInterface $daemonFactory The factory to use to create daemons
22+
*
23+
* @param DriverContainerInterface $driverContainer The driver container
2124
*/
22-
public function __construct(DaemonFactoryInterface $daemonFactory = null)
25+
public function __construct(DriverContainerInterface $driverContainer = null)
2326
{
24-
$this->daemonFactory = $daemonFactory;
27+
$this->driverContainer = $driverContainer ?: new DriverContainer();
2528
}
2629

2730
/**
@@ -31,7 +34,7 @@ public function createApplication($kernel, $commandName = null, $commandDescript
3134
{
3235
$command = $this->createCommand($kernel, $commandName, $commandDescription);
3336

34-
$application = new Application;
37+
$application = new Application();
3538
$application->add($command);
3639

3740
return $application;
@@ -42,6 +45,29 @@ public function createApplication($kernel, $commandName = null, $commandDescript
4245
*/
4346
public function createCommand($kernel, $commandName = null, $commandDescription = null)
4447
{
45-
return new DaemonRunCommand($kernel, $this->daemonFactory, $commandName, $commandDescription);
48+
$kernelObject = $this->getKernelObject($kernel);
49+
50+
return new DaemonRunCommand($kernelObject, $this->driverContainer, $commandName, $commandDescription);
51+
}
52+
53+
/**
54+
* Converts the kernel parameter to an object implementing the KernelInterface
55+
* if it is a callable.
56+
*
57+
* Otherwise returns the object directly.
58+
*
59+
* @param KernelInterface|callable $kernel The kernel
60+
*
61+
* @return KernelInterface The kernel as an object implementing the KernelInterface
62+
*/
63+
private function getKernelObject($kernel)
64+
{
65+
if ($kernel instanceof KernelInterface) {
66+
return $kernel;
67+
} elseif (is_callable($kernel)) {
68+
return new CallbackWrapper($kernel);
69+
}
70+
71+
throw new \InvalidArgumentException('Kernel must be callable or an instance of KernelInterface');
4672
}
4773
}

src/ApplicationFactoryInterface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
interface ApplicationFactoryInterface
1313
{
1414
/**
15-
* Create a Symfony console application
15+
* Create a Symfony console application.
1616
*
1717
* @param KernelInterface|callable $kernel The kernel to use
1818
* @param string $commandName The name of the daemon run command
@@ -23,7 +23,7 @@ interface ApplicationFactoryInterface
2323
public function createApplication($kernel, $commandName = null, $commandDescription = null);
2424

2525
/**
26-
* Create a Symfony console command
26+
* Create a Symfony console command.
2727
*
2828
* @param KernelInterface|callable $kernel The kernel to use
2929
* @param string $commandName The name of the daemon run command

src/Command/DaemonRunCommand.php

Lines changed: 46 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33
namespace 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;
77
use PHPFastCGI\FastCGIDaemon\KernelInterface;
88
use Symfony\Component\Console\Command\Command;
99
use 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
}

src/Connection/ConnectionPoolInterface.php

Lines changed: 0 additions & 29 deletions
This file was deleted.

src/Connection/StreamSocketConnectionPool.php

Lines changed: 0 additions & 144 deletions
This file was deleted.

0 commit comments

Comments
 (0)