Created
July 13, 2011 15:00
-
-
Save stof/1080466 to your computer and use it in GitHub Desktop.
HttpKernel integration with AiP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Midgard\AppServerBundle\AiP; | |
use Symfony\Component\HttpKernel\HttpKernelInterface; | |
use Symfony\Component\HttpFoundation\Request; | |
class Application | |
{ | |
/** | |
* @var Symfony\Component\HttpKernel\HttpKernelInterface | |
*/ | |
private $kernel; | |
private $prefix; | |
/** | |
* Construct prepares the AppServer in PHP URL mappings | |
* and is run once. It also loads the Symfony Application kernel | |
*/ | |
public function __construct(HttpKernelInterface $kernel, $prefix) | |
{ | |
$this->kernel = $kernel; | |
$this->prefix = $prefix; | |
} | |
/** | |
* Invoke is run once per each request. Here we generate a | |
* Request object, tell Symfony2 to handle it, and eventually | |
* return the Result contents back to AiP | |
*/ | |
public function __invoke($context) | |
{ | |
// Prepare Request object | |
$request = $this->ctx2Request($context); | |
$response = $this->kernel->handle($request); | |
foreach ($response->headers->getCookies() as $cookie) { | |
$context['_COOKIE']->setcookie($cookie->getName(), $cookie->getValue(), $cookie->getExpiresTime(), $cookie->getPath(), $cookie->getDomain(), $cookie->isSecure(), $cookie->isHttpOnly()); | |
} | |
return array($response->getStatusCode(), $this->getHeaders($response), $response->getContent()); | |
} | |
private function ctx2Request($context) | |
{ | |
$requestUri = $context['env']['REQUEST_URI']; | |
$_SERVER = $context['env']; | |
$uriParts = explode('?', $requestUri); | |
$_SERVER['PHP_SELF'] = $uriParts[0]; | |
$_SERVER['SCRIPT_FILENAME'] = "/some/path{$this->prefix}"; | |
if (isset($context['_GET'])) { | |
$_GET = $context['_GET']; | |
} | |
if (isset($context['_POST'])) { | |
$_POST = $context['_POST']; | |
} | |
if (isset($context['_FILES'])) { | |
$_FILES = $context['_FILES']; | |
} | |
$_COOKIE = $context['_COOKIE']->__toArray(); | |
return Request::createFromGlobals(); | |
} | |
/** | |
* Normalize headers from a Symfony2 Response ParameterBag | |
* to the array used by AiP | |
*/ | |
private function getHeaders($response) | |
{ | |
$ret = array(); | |
$headers = $response->headers->all(); | |
foreach ($headers as $header => $values) { | |
$ret[] = $header; | |
$ret[] = implode(';', $values); | |
} | |
return $ret; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Midgard\AppServerBundle\AiP\HttpKernelInterfaceApplication; | |
$app = require my_app.php; // Getting the Silex\Application instance with the recommended way to write them. | |
$aipApp = new HttpKernelInterfaceApplication($app); | |
// Do something to run this application |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require_once __DIR__.'/../vendor/symfony/src/Symfony/Component/ClassLoader/UniversalClassLoader.php'; | |
require_once __DIR__.'/autoload.php'; | |
require_once __DIR__.'/AppKernel.php'; | |
use Midgard\AppServerBundle\AiP\HttpKernelInterfaceApplication; | |
$aipApp = new HttpKernelInterfaceApplication($new AppKernel('prod', false)); | |
// Do something to run this application |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment