Example #1
0
 public function run()
 {
     if ($this->_isRequestMatching()) {
         $this->_controller = $this->_getControllerInstance();
         $this->_response->setBody($this->_callAction());
         $this->_response->send();
     }
 }
Example #2
0
 /**
  * @return void
  */
 public function next()
 {
     $this->load();
     if ($url = static::parseLink($this->response->getHeader('Link'), 'next')) {
         $this->request = new Http\Request($this->request->getMethod(), $url, $this->request->getHeaders(), $this->request->getContent());
     } else {
         $this->request = NULL;
     }
     $this->response = NULL;
     $this->counter++;
 }
Example #3
0
 public static function init()
 {
     if (null === self::$responseInstance) {
         parent::init();
         // 默认禁止缓存
         self::setCache(0);
     }
     if (null === self::$instance) {
         self::$instance = new self();
     }
     return self::$instance;
 }
Example #4
0
 /**
  * @param  Http\Response
  * @param  array|NULL  these codes are treated as success; code < 300 if NULL
  * @return mixed
  *
  * @throws ApiException
  */
 public function decode(Http\Response $response, array $okCodes = NULL)
 {
     $content = $response->getContent();
     if (preg_match('~application/json~i', $response->getHeader('Content-Type', ''))) {
         try {
             $content = Helpers::jsonDecode($response->getContent());
         } catch (JsonException $e) {
             throw new InvalidResponseException('JSON decoding failed.', 0, $e, $response);
         }
         if (!is_array($content) && !is_object($content)) {
             throw new InvalidResponseException('Decoded JSON is not an array or object.', 0, NULL, $response);
         }
     }
     $code = $response->getCode();
     if ($okCodes === NULL && $code >= 300 && $code != 304 || is_array($okCodes) && !in_array($code, $okCodes)) {
         /** @var $content \stdClass */
         switch ($code) {
             case Http\Response::S400_BAD_REQUEST:
                 throw new BadRequestException($content->message, $code, NULL, $response);
             case Http\Response::S401_UNAUTHORIZED:
                 throw new UnauthorizedException($content->message, $code, NULL, $response);
             case Http\Response::S403_FORBIDDEN:
                 if ($response->getHeader('X-RateLimit-Remaining') === '0') {
                     throw new RateLimitExceedException($content->message, $code, NULL, $response);
                 }
                 throw new ForbiddenException($content->message, $code, NULL, $response);
             case Http\Response::S404_NOT_FOUND:
                 throw new NotFoundException('Resource not found or not authorized to access.', $code, NULL, $response);
             case Http\Response::S422_UNPROCESSABLE_ENTITY:
                 $message = $content->message . implode(', ', array_map(function ($error) {
                     return '[' . implode(':', (array) $error) . ']';
                 }, $content->errors));
                 throw new UnprocessableEntityException($message, $code, NULL, $response);
         }
         $message = $okCodes === NULL ? '< 300' : implode(' or ', $okCodes);
         throw new UnexpectedResponseException("Expected response with code {$message}.", $code, NULL, $response);
     }
     return $content;
 }
Example #5
0
 public function renderPage(Page $page)
 {
     $rendered = false;
     $cacheId = 'page-' . static::$DI['Request']->getRoute();
     if (empty($page->nocache)) {
         $rendered = static::$DI['Cache\\PageCache']->get($cacheId);
     }
     if (false === $rendered) {
         $content = new \stdClass();
         $content->string = '';
         try {
             if (empty($page->layout)) {
                 $content = $page->getSegment(0);
                 $content->string = Hook::trigger(Hook::FILTER, 'renderContent', $content->string, $page->getData());
             } else {
                 $content->string = Hook::trigger(Hook::FILTER, 'renderLayout', $page);
             }
         } catch (\Exception $e) {
             $page->setError($e);
             $content->string = Hook::trigger(Hook::FILTER, 'renderLayout', $page);
         }
         if (empty($page->nocache)) {
             static::$DI['Cache\\PageCache']->set($cacheId, $content->string);
         }
         $rendered = $content->string;
     }
     $response = new Http\Response($rendered);
     $response->setStatus($page->getStatusCode());
     $response->setHeader('Content-Type', $page->content_type);
     return $response;
 }