Skip to content

Instantly share code, notes, and snippets.

@xserrat
Forked from havvg/ZendKernel.php
Created July 2, 2020 16:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xserrat/454134e437bd70c0d2590ce455bf2d00 to your computer and use it in GitHub Desktop.
Save xserrat/454134e437bd70c0d2590ce455bf2d00 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