Created
December 29, 2016 20:50
-
-
Save havvg/2a013fe3fc871e14827ba250b25eef91 to your computer and use it in GitHub Desktop.
StackPHP Example: Symfony + Zend Framework (ZF1)
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 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); |
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 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