/**
* The main method that handles the thread in a separate context.
*
* @return void
*/
public function run()
{
try {
// register the default autoloader
require SERVER_AUTOLOADER;
// register shutdown handler
register_shutdown_function(array(&$this, "shutdown"));
// synchronize the application instance and register the class loaders
$application = $this->application;
$application->registerClassLoaders();
// synchronize the valves, servlet request/response
$valves = $this->valves;
$servletRequest = $this->servletRequest;
// initialize servlet session, request + response
$servletResponse = new Response();
$servletResponse->init();
// we initialize this with a 500 to handle 'Fatal Error' case
$this->statusCode = 500;
// initialize arrays for header and cookies
$this->state = $servletResponse->getState();
$this->version = $servletResponse->getVersion();
$this->headers = $servletResponse->getHeaders();
$this->cookies = $servletResponse->getCookies();
// inject the sapplication and servlet response
$servletRequest->injectResponse($servletResponse);
$servletRequest->injectContext($application);
// prepare the request instance
$servletRequest->prepare();
// process the valves
foreach ($valves as $valve) {
$valve->invoke($servletRequest, $servletResponse);
if ($servletRequest->isDispatched() === true) {
break;
}
}
// profile the request if the profile logger is available
if ($profileLogger = $application->getInitialContext()->getLogger(LoggerUtils::PROFILE)) {
$profileLogger->appendThreadContext('request-handler');
$profileLogger->debug($servletRequest->getUri());
}
} catch (\Exception $e) {
// log the exception
$application->getInitialContext()->getSystemLogger()->error($e->__toString());
// ATTENTION: We MUST wrap the exception, because it's possible that
// the exception contains not serializable data that will
// lead to a white page!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
$this->exception = new ServletException($e, $e->getCode());
}
// copy the the response values
$this->statusCode = $servletResponse->getStatusCode();
$this->statusReasonPhrase = $servletResponse->getStatusReasonPhrase();
$this->version = $servletResponse->getVersion();
$this->state = $servletResponse->getState();
// copy the content of the body stream
$this->bodyStream = $servletResponse->getBodyStream();
// copy headers and cookies
$this->headers = $servletResponse->getHeaders();
$this->cookies = $servletResponse->getCookies();
}