예제 #1
0
파일: Identity.php 프로젝트: vgrish/tacit
 public static function identities(Tacit $app)
 {
     if (!is_array(static::$identities)) {
         $identitiesFile = $app->config('tacit.identitiesFile');
         if (is_readable($identitiesFile)) {
             static::$identities = (include $identitiesFile);
         }
     }
     return static::$identities;
 }
예제 #2
0
파일: Restful.php 프로젝트: vgrish/tacit
 /**
  * Respond to the request with a resource.
  *
  * @param ResourceInterface $resource A fractal resource.
  * @param int   $type The type of resource to respond with.
  * @param int   $status The HTTP status code to respond with.
  * @param array $meta An optional array of metadata for the response.
  *
  * @return void
  */
 protected function respond($resource, $type = self::RESOURCE_TYPE_ITEM, $status = 200, $meta = array())
 {
     $bodyRaw = null;
     if ($resource !== null && $status !== 204) {
         $bodyRaw = [];
         $scope = $this->fractal->createData($resource)->toArray();
         switch ($type) {
             case self::RESOURCE_TYPE_ITEM:
                 $bodyRaw['_links'] = $this->refs();
                 foreach ($scope['data'] as $key => $value) {
                     $bodyRaw[$key] = $value;
                 }
                 break;
             case self::RESOURCE_TYPE_COLLECTION:
                 $links = array();
                 $total = $meta['total'];
                 $limit = isset($meta['limit']) && (int) $meta['limit'] > 0 ? (int) $meta['limit'] : (int) $this->app->request->get('limit', 25);
                 $offset = isset($meta['offset']) ? (int) $meta['offset'] : (int) $this->app->request->get('offset', 0);
                 if ($total > $offset) {
                     $links['first'] = static::ref($this->app, $this->route->getParams(), $offset > 0 ? ['offset' => 0] : [], '(First)');
                     $links['prev'] = $offset > 0 ? static::ref($this->app, $this->route->getParams(), ['offset' => $offset - $limit], '(Previous)') : null;
                     $links['next'] = $offset + $limit < $total ? static::ref($this->app, $this->route->getParams(), ['offset' => $offset + $limit], '(Next)') : null;
                     $links['last'] = static::ref($this->app, $this->route->getParams(), ['offset' => floor(($total - 1) / $limit) * $limit], '(Last)');
                 }
                 $bodyRaw['_links'] = $this->refs(array_filter($links));
                 $bodyRaw['_embedded'][$meta['collectionName']] = $scope['data'];
                 $bodyRaw['total_items'] = $total;
                 $bodyRaw['returned_items'] = count($scope['data']);
                 $bodyRaw['limit'] = $limit;
                 $bodyRaw['offset'] = $offset;
                 break;
             case self::RESOURCE_TYPE_ERROR:
                 $bodyRaw = $scope['data'];
                 break;
         }
     }
     /* @var \Slim\Http\Response $response */
     $response = $this->app->response;
     $response->headers->set('Content-Type', static::$responseType);
     if (isset($meta['headers']) && is_array($meta['headers'])) {
         foreach ($meta['headers'] as $headerKey => $headerValue) {
             $response->headers->set($headerKey, $headerValue);
         }
     }
     $response->setStatus($status);
     if ($bodyRaw !== null && $status !== 204) {
         if ($this->app->config('debug') === true) {
             $bodyRaw['execution_time'] = microtime(true) - $this->app->config('startTime');
         }
         $response->setBody($this->encode($bodyRaw));
     }
     $this->app->stop();
 }