public function getHeaders(ResponseBag $bag)
 {
     if (!$bag->has(self::FILENAME)) {
         throw new \Exception('Missing filename for streamed response');
     }
     $bag->addHeader('X-Sendfile', $bag->get(self::FILENAME));
     $bag->addHeader('X-Accel-Buffering', 'no');
     $bag->addHeader('Transfer-Encoding', 'chunked');
     $bag->addHeader('Content-Type', 'application/force-download');
     $bag->addHeader('Content-Disposition', sprintf('%s; filename="%s"', $bag->get(self::ATTACHMENT_DISPOSITION, 'attachment'), $bag->get(self::FILENAME)));
     return $bag->getHeaders();
 }
Beispiel #2
0
 public function after(ResponseBag $bag)
 {
     $format = $bag->get('format');
     $builder = isset($this->outputBuilders[$format]) ? $this->outputBuilders[$format] : $this->defaultOutputBuilder;
     $data = $builder->buildOutput($bag);
     $headers = $builder->getHeaders($bag);
     foreach ($headers as $header) {
         header($header);
     }
     $bag->setResult($data);
 }
 public function get(Request $request, ResponseBag $bag)
 {
     $restConfiguration = $bag->get('__PyRestConfiguration', new PyRestConfiguration());
     $resourceName = $restConfiguration->getConfig(ResourceNameParser::NAME);
     $resourceId = $restConfiguration->getConfig(ResourceIdParser::NAME);
     $service = $this->getBertheService($resourceName);
     // run query against DAL
     try {
         $result = $service->getById($resourceId);
         $bag->set('data', $result);
         return $result;
     } catch (\Berthe\Exception\NotFoundException $exception) {
         throw new \Pyrite\PyRest\Exception\NotFoundException('Main resource not found', $exception, array('resource' => $resourceName, 'id' => $resourceId));
     }
 }
Beispiel #4
0
 public function after(ResponseBag $bag)
 {
     $this->bag = $bag;
     $actionResult = $bag->get(ResponseBag::ACTION_RESULT, false);
     $hasActionResult = !(false === $actionResult);
     $this->bag->set('request', $this->request);
     if (!$hasActionResult && $this->hasDefaultTemplate()) {
         $bag->setResult($this->getDefaultTemplate());
         return;
     }
     if ($this->hasTemplate($actionResult)) {
         $bag->setResult($this->renderTemplate($actionResult));
         return;
     }
 }
Beispiel #5
0
 public function after(ResponseBag $responseBag)
 {
     $actionResult = $responseBag->get(ResponseBag::ACTION_RESULT, false);
     $hasActionResult = !(false === $actionResult);
     if ($hasActionResult) {
         if ($actionResult[0] === self::MAGIC_KEY_URL_REFERENCE) {
             $this->redirect(substr($actionResult, 1), $responseBag);
         } elseif ($actionResult[0] === self::MAGIC_KEY_ROUTE_REFERENCE && $this->hasRedirection($actionResult)) {
             $url = $this->urlGenerator->generate(substr($actionResult, 1));
             $this->redirect($url, $responseBag);
         } elseif ($this->hasRedirection($actionResult)) {
             $this->redirect($this->config[$actionResult], $responseBag);
         }
     }
     return $responseBag;
 }
 /**
  * @param             $actionResult
  * @param ResponseBag $bag
  */
 public function redirect($actionResult, ResponseBag $bag)
 {
     $redirectionPath = $bag->get(RedirectionFromBagLayer::REDIRECTION_BAG_KEY);
     $bag->setResultCode(Response::HTTP_FOUND);
     $bag->addHeader('Location', $redirectionPath);
 }
Beispiel #7
0
 public function after(ResponseBag $bag)
 {
     $view = sprintf("<h1>DEBUG MODE</h1>\n<br />%s %s %s %s %s", $this->getRequest(), $this->getServer(), $this->getPost(), $this->getGet(), $this->getSession());
     $bag->set('view', $bag->get('view') . $view);
 }
Beispiel #8
0
 protected function buildResponseFromResponseBag(ResponseBag $responseBag)
 {
     $result = $responseBag->getResult();
     $resultCode = $responseBag->getResultCode();
     $headers = $responseBag->getHeaders();
     $type = $responseBag->getType();
     if ($type === ResponseBag::TYPE_DEFAULT) {
         return new Response($result, $resultCode, $headers);
     }
     if ($type === ResponseBag::TYPE_STREAMED) {
         $callback = $responseBag->getCallback();
         if (null === $callback) {
             throw new \Exception('Streamed response need callback');
         }
         return new StreamedResponse($callback, $resultCode, $headers);
     }
     if ($type === ResponseBag::TYPE_BINARY) {
         $filepath = $responseBag->get(BinaryOutputBuilder::FILEPATH, null);
         $visibility = $responseBag->get(BinaryOutputBuilder::VISIBILITY_PUBLIC, true);
         $autoEtag = $responseBag->get(BinaryOutputBuilder::AUTO_ETAG, false);
         $autoLastModified = $responseBag->get(BinaryOutputBuilder::AUTO_LAST_MODIFIED, true);
         $contentDisposition = $responseBag->get(BinaryOutputBuilder::CONTENT_DISPOSITION, null);
         return new BinaryFileResponse($filepath, $contentDisposition, $visibility, $autoEtag, $autoLastModified);
     }
 }
 public function redirect($actionResult, ResponseBag $bag)
 {
     $redirectionPath = $bag->get(RedirectionFromBagLayer::REDIRECTION_BAG_KEY);
     $bag->setResultCode(302);
     header("Location: " . $redirectionPath);
 }
Beispiel #10
0
 public function buildOutput(ResponseBag $bag)
 {
     return json_encode($bag->get('data'), JSON_NUMERIC_CHECK);
 }
Beispiel #11
0
 public function buildOutput(ResponseBag $bag)
 {
     return xmlrpc_encode($bag->get('data'));
 }
Beispiel #12
0
 public function buildOutput(ResponseBag $bag)
 {
     return '<pre>' . print_r($bag->get('data'), true) . '</pre>';
 }