Skip to content

Commit c17f8f7

Browse files
committed
Implement basic front controller support
1 parent ddd17b1 commit c17f8f7

File tree

5 files changed

+226
-7
lines changed

5 files changed

+226
-7
lines changed

composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"symfony/http-kernel": "2.1.*",
44
"symfony/process": "2.1.*"
55
},
6+
"require-dev": {
7+
"silex/silex": "1.0.*@dev"
8+
},
69
"autoload": {
710
"psr-0": { "Igorw": "src" }
811
}

composer.lock

Lines changed: 189 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Igorw/CgiHttpKernel/CgiHttpKernel.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,17 @@
1010
class CgiHttpKernel implements HttpKernelInterface
1111
{
1212
private $rootDir;
13+
private $frontController;
1314

14-
public function __construct($rootDir)
15+
public function __construct($rootDir, $frontController = null)
1516
{
1617
$this->rootDir = $rootDir;
18+
$this->frontController = $frontController;
1719
}
1820

1921
public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQUEST, $catch = true)
2022
{
21-
$filename = ltrim($request->getPathInfo(), '/');
23+
$filename = $this->frontController ?: ltrim($request->getPathInfo(), '/');
2224

2325
if (!file_exists($this->rootDir.'/'.$filename)) {
2426
return new Response('The requested file could not be found.', 404);
@@ -28,6 +30,10 @@ public function handle(Request $request, $type = HttpKernelInterface::MASTER_REQ
2830
->add('php-cgi')
2931
->add('-d expose_php=Off')
3032
->add($filename)
33+
->setEnv('SCRIPT_NAME', $this->rootDir.'/'.$filename)
34+
->setEnv('PATH_INFO', $request->getPathInfo())
35+
->setEnv('QUERY_STRING', $request->getQueryString())
36+
->setEnv('REQUEST_URI', $request->getRequestUri())
3137
->setWorkingDirectory($this->rootDir)
3238
->getProcess();
3339

tests/Igorw/CgiHttpKernel/CgiHttpKernelTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,15 @@ public function customErrorStatusCodeShouldBeSent()
5151

5252
$this->assertSame(500, $response->getStatusCode());
5353
}
54+
55+
/** @test */
56+
public function frontControllerShouldLoadPathInfo()
57+
{
58+
$this->kernel = new CgiHttpKernel(__DIR__.'/Fixtures', 'silex.php');
59+
60+
$request = Request::create('/foo');
61+
$response = $this->kernel->handle($request);
62+
63+
$this->assertSame('bar', $response->getContent());
64+
}
5465
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require __DIR__.'/../../../../vendor/autoload.php';
4+
5+
$app = new Silex\Application();
6+
7+
$app->get('/foo', function () {
8+
return 'bar';
9+
});
10+
11+
$app->post('/baz', function () {
12+
return 'qux';
13+
});
14+
15+
$app->run();

0 commit comments

Comments
 (0)