Skip to content

Commit 198414d

Browse files
Merge pull request #8 from PHPFastCGI/application-factory
Created application factory class
2 parents c959b2a + 6a8c657 commit 198414d

File tree

8 files changed

+144
-28
lines changed

8 files changed

+144
-28
lines changed

README.md

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,31 +27,20 @@ Below is an example of a simple 'Hello, World!' FastCGI application in PHP.
2727
// Include the composer autoloader
2828
require_once dirname(__FILE__) . '/../vendor/autoload.php';
2929

30-
use PHPFastCGI\FastCGIDaemon\Command\DaemonRunCommand;
31-
use PHPFastCGI\FastCGIDaemon\DaemonFactory;
30+
use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
3231
use Psr\Http\Message\ServerRequestInterface;
33-
use Symfony\Component\Console\Application;
3432
use Zend\Diactoros\Response\HtmlResponse;
3533

36-
// Create the dependencies for the DaemonRunCommand
37-
38-
// Dependency 1: The daemon factory
39-
$daemonFactory = new DaemonFactory();
40-
41-
// Dependency 2: A simple kernel. This is the core of your application
34+
// A simple kernel. This is the core of your application
4235
$kernel = function (ServerRequestInterface $request) {
4336
return new HtmlResponse('<h1>Hello, World!</h1>');
4437
};
4538

46-
// Create an instance of DaemonRunCommand using the daemon factory and the kernel
47-
$command = new DaemonRunCommand('run', 'Run a FastCGI daemon', $daemonFactory, $kernel);
48-
49-
// Create a symfony console application and add the command
50-
$consoleApplication = new Application();
51-
$consoleApplication->add($command);
39+
// Create your Symfony console application using the factory
40+
$application = (new ApplicationFactory)->createApplication($kernel);
5241

53-
// Run the symfony console application
54-
$consoleApplication->run();
42+
// Run the Symfony console application
43+
$application->run();
5544
```
5645

5746
If you wish to configure your FastCGI application to work with the apache web server, you can use the apache FastCGI module to process manage your application.

src/ApplicationFactory.php

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace PHPFastCGI\FastCGIDaemon;
4+
5+
use PHPFastCGI\FastCGIDaemon\Command\DaemonRunCommand;
6+
use Symfony\Component\Console\Application;
7+
/**
8+
* The default implementation of the ApplicationFactoryInterface.
9+
*/
10+
class ApplicationFactory implements ApplicationFactoryInterface
11+
{
12+
/**
13+
* @var DaemonFactoryInterface
14+
*/
15+
protected $daemonFactory;
16+
17+
/**
18+
* Constructor.
19+
*
20+
* @param DaemonFactoryInterface $daemonFactory The factory to use to create daemons
21+
*/
22+
public function __construct(DaemonFactoryInterface $daemonFactory = null)
23+
{
24+
$this->daemonFactory = $daemonFactory;
25+
}
26+
27+
/**
28+
* {@inheritdoc}
29+
*/
30+
public function createApplication($kernel, $commandName = null, $commandDescription = null)
31+
{
32+
$command = $this->createCommand($kernel, $commandName, $commandDescription);
33+
34+
$application = new Application;
35+
$application->add($command);
36+
37+
return $application;
38+
}
39+
40+
/**
41+
* {@inheritdoc}
42+
*/
43+
public function createCommand($kernel, $commandName = null, $commandDescription = null)
44+
{
45+
return new DaemonRunCommand($kernel, $this->daemonFactory, $commandName, $commandDescription);
46+
}
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
namespace PHPFastCGI\FastCGIDaemon;
4+
5+
use Symfony\Component\Console\Application;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
/**
9+
* Objects that implement the ApplicationFactoryInterface can be used to create
10+
* Symfony console commands and applications.
11+
*/
12+
interface ApplicationFactoryInterface
13+
{
14+
/**
15+
* Create a Symfony console application
16+
*
17+
* @param KernelInterface|callable $kernel The kernel to use
18+
* @param string $commandName The name of the daemon run command
19+
* @param string $commandDescription The description of the daemon run command
20+
*
21+
* @return Application The Symfony console application
22+
*/
23+
public function createApplication($kernel, $commandName = null, $commandDescription = null);
24+
25+
/**
26+
* Create a Symfony console command
27+
*
28+
* @param KernelInterface|callable $kernel The kernel to use
29+
* @param string $commandName The name of the daemon run command
30+
* @param string $commandDescription The description of the daemon run command
31+
*
32+
* @return Command The Symfony console command
33+
*/
34+
public function createCommand($kernel, $commandName = null, $commandDescription = null);
35+
}

src/Command/DaemonRunCommand.php

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace PHPFastCGI\FastCGIDaemon\Command;
44

5+
use PHPFastCGI\FastCGIDaemon\DaemonFactory;
56
use PHPFastCGI\FastCGIDaemon\DaemonFactoryInterface;
67
use PHPFastCGI\FastCGIDaemon\KernelInterface;
78
use Symfony\Component\Console\Command\Command;
@@ -12,6 +13,9 @@
1213

1314
class DaemonRunCommand extends Command
1415
{
16+
const DEFAULT_NAME = 'run';
17+
const DEFAULT_DESCRIPTION = 'Run the FastCGI daemon';
18+
1519
/**
1620
* @var DaemonFactoryInterface
1721
*/
@@ -25,13 +29,17 @@ class DaemonRunCommand extends Command
2529
/**
2630
* Constructor.
2731
*
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
2834
* @param string $name The name of the daemon run command
2935
* @param string $description The description of the daemon run command
30-
* @param DaemonFactoryInterface $daemonFactory The factory to use to create the daemon
31-
* @param KernelInterface|callable $kernel The kernel to be given to the daemon
3236
*/
33-
public function __construct($name, $description, DaemonFactoryInterface $daemonFactory, $kernel)
37+
public function __construct($kernel, DaemonFactoryInterface $daemonFactory = null, $name = null, $description = null)
3438
{
39+
$daemonFactory = $daemonFactory ?: new DaemonFactory;
40+
$name = $name ?: self::DEFAULT_NAME;
41+
$description = $description ?: self::DEFAULT_DESCRIPTION;
42+
3543
parent::__construct($name);
3644

3745
$this

src/DaemonFactory.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,10 @@ public function createDaemon($kernel)
1919
{
2020
$socket = fopen('php://fd/'.DaemonInterface::FCGI_LISTENSOCK_FILENO, 'r');
2121

22+
if (false === $socket) {
23+
throw new \RuntimeException('Could not open FCGI_LISTENSOCK_FILENO');
24+
}
25+
2226
return $this->createDaemonFromStreamSocket($kernel, $socket);
2327
}
2428

@@ -29,7 +33,12 @@ public function createDaemon($kernel)
2933
*/
3034
public function createTcpDaemon($kernel, $port, $host = 'localhost')
3135
{
32-
$socket = stream_socket_server('tcp://'.$host.':'.$port);
36+
$address = 'tcp://'.$host.':'.$port;
37+
$socket = stream_socket_server($address);
38+
39+
if (false === $socket) {
40+
throw new \RuntimeException('Could not create stream socket server on: '.$address);
41+
}
3342

3443
return $this->createDaemonFromStreamSocket($kernel, $socket);
3544
}

test/ApplicationFactoryTest.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
namespace PHPFastCGI\Test\FastCGIDaemon;
4+
5+
use PHPFastCGI\FastCGIDaemon\ApplicationFactory;
6+
7+
/**
8+
* Tests the daemon.
9+
*/
10+
class ApplicationFactoryTest extends \PHPUnit_Framework_TestCase
11+
{
12+
/**
13+
* Tests that the factory can create a Symfony console application
14+
*/
15+
public function testCreateApplication()
16+
{
17+
$applicationFactory = new ApplicationFactory;
18+
19+
$name = 'foo';
20+
$description = 'bar';
21+
22+
$application = $applicationFactory->createApplication(function () { }, $name, $description);
23+
24+
$this->assertInstanceOf('Symfony\\Component\\Console\\Application', $application);
25+
$this->assertTrue($application->has($name));
26+
$this->assertEquals($description, $application->get($name)->getDescription());
27+
}
28+
}

test/Command/DaemonRunCommandTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class DaemonRunCommandTest extends \PHPUnit_Framework_TestCase
2020
*/
2121
public function testInvalidArgumentException()
2222
{
23-
new DaemonRunCommand('name', 'description', new DaemonFactory(), 'not a callable function');
23+
new DaemonRunCommand('not a callable function');
2424
}
2525

2626
/**
@@ -29,7 +29,7 @@ public function testInvalidArgumentException()
2929
*/
3030
public function testOptions()
3131
{
32-
$command = new DaemonRunCommand('name', 'description', new DaemonFactory(), function () { });
32+
$command = new DaemonRunCommand(function () { });
3333

3434
$definition = $command->getDefinition();
3535

@@ -48,7 +48,7 @@ public function testOptions()
4848
*/
4949
public function testInvalidOptions()
5050
{
51-
$command = new DaemonRunCommand('name', 'description', new DaemonFactory(), function () { });
51+
$command = new DaemonRunCommand(function () { });
5252

5353
$input = new ArrayInput(['--host' => '_']);
5454
$output = new NullOutput();
@@ -103,7 +103,7 @@ public function testCreateDefaultDaemon()
103103
->method('createDaemon')
104104
->will($this->returnValue($mockDaemon));
105105

106-
$command = new DaemonRunCommand('name', 'description', $mockDaemonFactory, function () { });
106+
$command = new DaemonRunCommand(function () { }, $mockDaemonFactory);
107107

108108
$command->run($input, $output);
109109
}
@@ -130,7 +130,7 @@ public function testCreateTcpDaemonWithHost()
130130
->with($this->equalTo($kernel), $this->equalTo($port), $this->equalTo($host))
131131
->will($this->returnValue($mockDaemon));
132132

133-
$command = new DaemonRunCommand('name', 'description', $mockDaemonFactory, $kernel);
133+
$command = new DaemonRunCommand($kernel, $mockDaemonFactory);
134134

135135
$command->run($input, $output);
136136
}
@@ -156,7 +156,7 @@ public function testCreateTcpDaemonWithoutHost()
156156
->with($this->equalTo($kernel), $this->equalTo($port), $this->equalTo('localhost'))
157157
->will($this->returnValue($mockDaemon));
158158

159-
$command = new DaemonRunCommand('name', 'description', $mockDaemonFactory, $kernel);
159+
$command = new DaemonRunCommand($kernel, $mockDaemonFactory);
160160

161161
$command->run($input, $output);
162162
}

test/DaemonFactoryTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use PHPFastCGI\FastCGIDaemon\DaemonFactory;
77

88
/**
9-
* Tests the daemon.
9+
* Tests the daemon factory.
1010
*/
1111
class DaemonFactoryTest extends \PHPUnit_Framework_TestCase
1212
{

0 commit comments

Comments
 (0)