Exemplo n.º 1
0
 /**
  * Dispatch the request
  *
  * @param \Cundd\Rest\Request $request         Overwrite the request
  * @param Response            $responsePointer Reference to be filled with the response
  * @return boolean Returns if the request has been successfully dispatched
  */
 public function dispatch(Request $request = NULL, Response &$responsePointer = NULL)
 {
     if ($request) {
         $this->request = $request;
         $this->objectManager->reassignRequest();
     } else {
         $request = $this->getRequest();
     }
     if (!$this->getPath()) {
         return $this->greet();
     }
     // Checks if the request needs authentication
     switch ($this->objectManager->getAccessController()->getAccess()) {
         case AccessControllerInterface::ACCESS_ALLOW:
             break;
         case AccessControllerInterface::ACCESS_UNAUTHORIZED:
             echo $this->createErrorResponse('Unauthorized', 401);
             return FALSE;
         case AccessControllerInterface::ACCESS_DENY:
         default:
             echo $this->createErrorResponse('Forbidden', 403);
             return FALSE;
     }
     /** @var Cache $cache */
     $cache = $this->objectManager->getCache();
     $response = $cache->getCachedValueForRequest($request);
     $success = TRUE;
     // If no cached response exists
     if (!$response) {
         // If a path is given let the handler build up the routes
         if ($this->getPath()) {
             $this->logRequest('path: "' . $this->getPath() . '" method: "' . $request->method() . '"');
             $this->objectManager->getHandler()->configureApiPaths();
         }
         // Let Bullet PHP do the hard work
         $response = $this->app->run($request);
         //			$response->content($response->content());
         // Handle exceptions
         if ($response->content() instanceof \Exception) {
             $success = FALSE;
             $exception = $response->content();
             $this->logException($exception);
             $response = $this->exceptionToResponse($exception);
         }
         // Cache the response
         $cache->setCachedValueForRequest($request, $response);
     }
     $responsePointer = $response;
     $responseString = (string) $response;
     $this->logResponse('response: ' . $response->status(), array('response' => '' . $responseString));
     echo $responseString;
     return $success;
 }
Exemplo n.º 2
0
 /**
  * Dispatch the request
  *
  * @param \Cundd\Rest\Request $request Overwrite the request
  * @param Response $responsePointer Reference to be filled with the response
  * @return boolean Returns if the request has been successfully dispatched
  */
 public function dispatch(Request $request = NULL, Response &$responsePointer = NULL)
 {
     if ($request) {
         $this->requestFactory->registerCurrentRequest($request);
         $this->objectManager->reassignRequest();
     } else {
         $request = $this->requestFactory->getRequest();
     }
     $requestPath = $request->path();
     if (!$requestPath) {
         return $this->greet();
     }
     // Checks if the request needs authentication
     switch ($this->objectManager->getAccessController()->getAccess()) {
         case AccessControllerInterface::ACCESS_ALLOW:
             break;
         case AccessControllerInterface::ACCESS_UNAUTHORIZED:
             echo $this->responseFactory->createErrorResponse('Unauthorized', 401);
             return FALSE;
         case AccessControllerInterface::ACCESS_DENY:
         default:
             echo $this->responseFactory->createErrorResponse('Forbidden', 403);
             return FALSE;
     }
     /** @var Cache $cache */
     $cache = $this->objectManager->getCache();
     $response = $cache->getCachedValueForRequest($request);
     $success = TRUE;
     // If no cached response exists
     if (!$response) {
         // If a path is given let the handler build up the routes
         if ($requestPath) {
             $this->logRequest(sprintf('path: "%s" method: "%s"', $requestPath, $request->method()));
             $this->objectManager->getHandler()->configureApiPaths();
         }
         // Let Bullet PHP do the hard work
         $response = $this->app->run($request);
         // Handle exceptions
         if ($response->content() instanceof \Exception) {
             $success = FALSE;
             $exception = $response->content();
             $this->logException($exception);
             $response = $this->exceptionToResponse($exception);
         } else {
             // Cache the response
             $cache->setCachedValueForRequest($request, $response);
         }
     }
     // Additional custom headers
     $additionalResponseHeaders = $this->objectManager->getConfigurationProvider()->getSetting('responseHeaders', array());
     if (is_array($additionalResponseHeaders) && count($additionalResponseHeaders)) {
         foreach ($additionalResponseHeaders as $responseHeaderType => $value) {
             if (is_string($value)) {
                 $response->header($responseHeaderType, $value);
             } elseif (is_array($value) && array_key_exists('userFunc', $value)) {
                 $response->header(rtrim($responseHeaderType, '.'), GeneralUtility::callUserFunction($value['userFunc'], $value, $this));
             }
         }
     }
     $responsePointer = $response;
     $responseString = (string) $response;
     $this->logResponse('response: ' . $response->status(), array('response' => '' . $responseString));
     echo $responseString;
     return $success;
 }