示例#1
0
 public function canResolve(Context $context)
 {
     $uri = $context->getRequest()->getUrl();
     $u = ltrim($uri, "/");
     $ns = ltrim($this->ns, "/");
     return substr($u, 0, strlen($ns)) == $ns;
 }
 public function create(Context $context, $paramName)
 {
     $headers = $context->getRequest()->getHeaders();
     $msg = new Message();
     $created = isset($headers['created']) ? $headers['created'] : null;
     $expiration = isset($headers['expiration']) ? $headers['expiration'] : null;
     $msg->setHeaders($headers);
     $msg->setExpiration($expiration);
     $msg->setCreated($created);
     return $msg;
 }
示例#3
0
文件: RestApp.php 项目: fliglio/flfc
 public function call(Context $context)
 {
     $response = $context->getResponse();
     try {
         $this->wrappedApp->call($context);
     } catch (NotFoundException $e) {
         $response->setStatus(Http::STATUS_NOT_FOUND);
         $response->setContentType('application/json');
         return;
     } catch (InternalServerErrorException $e) {
         $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
         $response->setContentType('application/json');
         return;
     } catch (BadRequestException $e) {
         error_log((string) $e);
         $response->setStatus(Http::STATUS_BAD_REQUEST);
         $response->setContentType('application/json');
         $response->setBody(new DefaultBody($e->getMessage()));
         return;
     } catch (LocationStatusException $e) {
         $response->setStatus($e->getStatusCode());
         $response->addHeader("Location", (string) $e->getLocation());
         return;
     } catch (HttpStatusException $e) {
         $response->setStatus($e->getStatusCode());
         $response->setContentType('application/json');
         $response->setBody(new DefaultBody($e->getMessage()));
         return;
     } catch (\Exception $e) {
         error_log((string) $e);
         $response->setStatus(Http::STATUS_INTERNAL_SERVER_ERROR);
         $response->setContentType('application/json');
         $response->setBody(new DefaultBody($e->getMessage()));
         return;
     }
     $body = $response->getBody();
     if (!is_null($body)) {
         if (!$body instanceof RenderableResponseBody) {
             $json = is_null($body->getContent()) ? '' : json_encode($body->getContent());
             $response->setBody(new DefaultBody($json));
         }
     } else {
         $response->setBody(new DefaultBody(''));
     }
     if (is_null($response->getContentType())) {
         $response->setContentType('application/json');
     }
 }
示例#4
0
 public function call(Context $context)
 {
     $route = $context->getProp(RoutingApp::CURRENT_ROUTE);
     $instance = $route->getResourceInstance();
     $methodName = $route->getResourceMethod();
     $routeParams = $route->getParams();
     $getParams = $_GET;
     $rMethod = $this->getReflectionMethod($instance, $methodName);
     $methodArgs = $this->getMethodArgs($rMethod, $context, $routeParams, $getParams);
     $to = $rMethod->invokeArgs($instance, $methodArgs);
     $body;
     if ($to instanceof ResponseBody) {
         $body = $to;
     } else {
         $body = new UnmarshalledBody($to);
     }
     $context->getResponse()->setBody($body);
 }
示例#5
0
 public function call(Context $context)
 {
     $currentUrl = $context->getRequest()->getUrl();
     $currentMethod = $context->getRequest()->getHttpMethod();
     if ($currentMethod == Http::METHOD_GET) {
         $lintedPath = $this->lintPath($currentUrl);
         if ((string) $currentUrl != $lintedPath) {
             $protocol = $context->getRequest()->getProtocol();
             $host = $context->getRequest()->getHost();
             if ($this->redirect) {
                 $url = Url::fromParts(['scheme' => $protocol, 'host' => $host, 'path' => $lintedPath, 'query' => $this->arrayToQuery($context->getRequest()->getGetParams())]);
                 throw new RedirectException("Linting Url and redirecting browser", 301, $url);
             } else {
                 $context->getRequest()->setUrl($lintedPath);
             }
         }
     }
     $this->wrappedApp->call($context);
 }
示例#6
0
 public function call(Context $context)
 {
     $currentHost = $context->getRequest()->getHost();
     $currentProtocol = $context->getRequest()->getProtocol();
     $currentUrl = $context->getRequest()->getUrl();
     $currentMethod = $context->getRequest()->getHttpMethod();
     // Identify current Command; register RouteMap & params with Context
     $route;
     try {
         $route = $this->routeMap->getRoute($context->getRequest());
     } catch (RouteException $e) {
         throw new NotFoundException(sprintf("Route not found for request: %s %s://%s%s", $currentMethod, $currentProtocol, $currentHost, $currentUrl));
     }
     $context->setProp(self::CURRENT_ROUTE, $route);
     // Force pages to their designated protocol if specified
     if ($route->getProtocol() != null) {
         if ($currentProtocol != $route->getProtocol()) {
             $url = Url::fromString(sprintf("%s://%s/", $route->getProtocol(), $currentHost))->join($currentUrl)->addParams($_GET);
             throw new RedirectException('Change Protocol', 301, $url);
         }
     }
     $this->wrappedApp->call($context);
 }