/** * Runs PieCrust on the given URI. */ public function run($uri = null, array $server = null) { try { $this->runUnsafe($uri, $server); } catch (\Exception $e) { $handler = new PieCrustErrorHandler($this->pieCrust); $handler->handleError($e); } }
/** * For internal use only. */ public function _runPieCrustRequest(\StupidHttp_HandlerContext $context) { $startTime = microtime(true); // Things like the plugin loader will add paths to the PHP include path. // Let's save it and restore it later. $includePath = get_include_path(); // Run PieCrust dynamically. $pieCrustException = null; try { $params = PieCrustRunner::getPieCrustParameters(array('root' => $this->rootDir, 'cache' => $this->options['cache']), $context->getRequest()->getServerVariables()); $pieCrust = new PieCrust($params); $pieCrust->getConfig()->setValue('server/is_hosting', true); $pieCrust->getConfig()->setValue('site/cache_time', false); $pieCrust->getConfig()->setValue('site/pretty_urls', true); $pieCrust->getConfig()->setValue('site/root', '/'); if ($this->options['debug']) { $pieCrust->getConfig()->setValue('site/display_errors', true); } } catch (Exception $e) { // Error while setting up PieCrust. $pieCrustException = $e; } // If there was no error setting up the application, see if there was // an error in the last pre-bake. if ($pieCrustException == null) { $pieCrustException = $this->bakeError; $this->bakeError = null; } // If there was no error so far, run the current request. $headers = array(); if ($pieCrustException == null) { try { $runner = new PieCrustRunner($pieCrust); $runner->runUnsafe(null, $context->getRequest()->getServerVariables(), null, $headers); } catch (Exception $e) { // Error while running the request. $pieCrustException = $e; } } // Set the return HTTP status code. $code = 500; if (isset($headers[0])) { // The HTTP return code is set by PieCrust in the '0'-th header (if // set at all). $code = $headers[0]; unset($headers[0]); // Unset so we can iterate on headers more easily later. } else { $code = $pieCrustException == null ? 200 : ($pieCrustException->getMessage() == '404' ? 404 : 500); } $context->getResponse()->setStatus($code); // Set the headers. foreach ($headers as $h => $v) { $context->getResponse()->setHeader($h, $v); } // Show an error message, if needed. if ($pieCrustException) { $headers = array(); $handler = new PieCrustErrorHandler($pieCrust); $handler->handleError($pieCrustException, null, $headers); } // Restore the include path. set_include_path($includePath); $endTime = microtime(true); $timeSpan = microtime(true) - $startTime; $context->getLog()->debug("Ran PieCrust request in " . $timeSpan * 1000 . "ms."); if ($pieCrustException != null) { $context->getLog()->error($pieCrustException->getMessage()); } }