Skip to content

Instantly share code, notes, and snippets.

@havvg
Created December 29, 2016 20:50
Show Gist options
  • Save havvg/2a013fe3fc871e14827ba250b25eef91 to your computer and use it in GitHub Desktop.
Save havvg/2a013fe3fc871e14827ba250b25eef91 to your computer and use it in GitHub Desktop.
StackPHP Example: Symfony + Zend Framework (ZF1)
<?php
use Symfony\Component\Debug\Debug;
use Symfony\Component\HttpFoundation\Request;
// Read environment
$env = getenv('SYMFONY_ENV') ?: 'dev';
$debug = (bool) getenv('SYMFONY_DEBUG');
$cache = (bool) getenv('SYMFONY_CACHE');
$classLoader = require_once __DIR__.'/../app/autoload.php';
if ($debug) {
Debug::enable();
}
$kernel = new AppKernel($env, $debug);
$kernel->loadClassCache();
$kernel = new ZendKernel($kernel);
if ($cache) {
$kernel = new AppCache($kernel);
Request::enableHttpMethodParameterOverride();
}
$request = Request::createFromGlobals();
$response = $kernel->handle($request);
$response->send();
$kernel->terminate($request, $response);
<?php
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\TerminableInterface;
use Symfony\Component\Routing\Exception\ResourceNotFoundException;
class ZendKernel implements HttpKernelInterface, TerminableInterface
{
/**
* @var HttpKernelInterface
*/
private $app;
/**
* Constructor.
*
* @param HttpKernelInterface $app
*/
public function __construct(HttpKernelInterface $app)
{
$this->app = $app;
}
/**
* {@inheritdoc}
*/
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
try {
return $this->app->handle($request, $type, false);
} catch (ResourceNotFoundException $e) {
return $this->doHandle($request, $type, $catch);
} catch (NotFoundHttpException $e) {
if ($e->getPrevious() instanceof ResourceNotFoundException) {
return $this->doHandle($request, $type, $catch);
}
throw $e;
}
}
/**
* @see HttpKernelInterface::handle
*/
private function doHandle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
ob_start();
$application = new Zend_Application(
APPLICATION_ENVIRONMENT,
APPLICATION_PATH.'/config/legacy/config.ini'
);
$application->bootstrap();
$application->run();
$headers = headers_list();
header_remove();
$response = Response::create(ob_get_clean(), http_response_code());
foreach ($headers as $header) {
$pieces = explode(':', $header);
$headerName = array_shift($pieces);
$response->headers->set($headerName, trim(implode(':', $pieces)), false);
}
return $response;
}
/**
* {@inheritdoc}
*/
public function terminate(Request $request, Response $response)
{
if ($this->app instanceof TerminableInterface) {
$this->app->terminate($request, $response);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment