Beispiel #1
0
 /**
  * Generates the response content for the client.
  *
  * This is normally called by platform/execute-http.php and must be called after all
  * modules have been registered to guarantee the correct output.
  *
  * @static
  * @param Request\Request $request
  * @return string
  */
 public static function generateResponseForRequest(Request\Request $request)
 {
     // Set the current request to be this one.
     $context = new Context();
     $context->Request = $request;
     $additionalData = [];
     if ($request instanceof WebRequest) {
         if (!empty($request->GetData)) {
             $additionalData = $request->GetData;
         }
     }
     Log::createEntry(Log::PERFORMANCE_LEVEL | Log::DEBUG_LEVEL, function () use($request) {
         if ($request instanceof WebRequest) {
             return "Generating response for url " . $request->UrlPath;
         }
         if ($request instanceof CliRequest) {
             return "Starting CLI response";
         }
         return "";
     }, "ROUTER", $additionalData);
     Log::indent();
     $handlers = self::getAllUrlHandlers();
     // an empty-string Response to fall back on if nothing else is generated
     $response = new HtmlResponse();
     $response->SetContent('');
     $filterResponse = true;
     try {
         // Iterate over each handler and ask them to generate a response.
         // If they do return a response we return that and exit the loop.
         // If they return false then we assume they couldn't handle the URL
         // and continue to the next handler.
         foreach ($handlers as $handler) {
             $generatedResponse = $handler->generateResponse($request);
             if ($generatedResponse !== false) {
                 Log::Debug(function () use($handler) {
                     return ["Handler `" . get_class($handler) . "` generated response.", []];
                 }, "ROUTER");
                 // it should be preferred for a handler to return a Response object,
                 // but checking this here retains the option for them to just return
                 // their output
                 if ($generatedResponse instanceof Response) {
                     $response = $generatedResponse;
                 } else {
                     $response->setContent($generatedResponse);
                 }
                 break;
             }
         }
     } catch (ForceResponseException $er) {
         $response = $er->getResponse();
         $filterResponse = false;
     } catch (StopGeneratingResponseException $er) {
         $filterResponse = false;
     } catch (RhubarbException $er) {
         $response = ExceptionHandler::processException($er);
     } catch (\Exception $er) {
         $response = ExceptionHandler::processException(new NonRhubarbException($er));
     }
     if ($filterResponse) {
         Log::createEntry(Log::PERFORMANCE_LEVEL | Log::DEBUG_LEVEL, "Output filters started", "ROUTER");
         Log::indent();
         $filters = self::getAllResponseFilters();
         foreach ($filters as $filter) {
             $response = $filter->processResponse($response);
         }
         Log::createEntry(Log::PERFORMANCE_LEVEL | Log::DEBUG_LEVEL, "Output filters finished", "ROUTER");
         Log::outdent();
     }
     Log::performance("Response generated", "ROUTER");
     Log::outdent();
     return $response;
 }