/**
  * {@inheritdoc}
  *
  * @return Folder
  */
 public function generate(ResourceInterface $resource = null)
 {
     $folder = new Folder();
     $folder->setId((string) Uuid::uuid4());
     $folder->setName($resource->getShortName());
     return $folder;
 }
 /**
  * Creates operation.
  *
  * @param ResourceInterface $resource
  * @param bool              $collection
  * @param string|array      $methods
  * @param string|null       $path
  * @param string|null       $controller
  * @param string|null       $routeName
  * @param array             $context
  *
  * @return Operation
  */
 private function createOperation(ResourceInterface $resource, $collection, $methods, $path = null, $controller = null, $routeName = null, array $context = [])
 {
     $shortName = $resource->getShortName();
     if (!isset(self::$inflectorCache[$shortName])) {
         self::$inflectorCache[$shortName] = Inflector::pluralize(Inflector::tableize($shortName));
     }
     // Populate path
     if (null === $path) {
         $path = '/' . self::$inflectorCache[$shortName];
         if (!$collection) {
             $path .= '/{id}';
         }
     }
     // Guess default method
     if (is_array($methods)) {
         $defaultMethod = $methods[0];
     } else {
         $defaultMethod = $methods;
     }
     // Populate controller
     if (null === $controller) {
         $defaultAction = strtolower($defaultMethod);
         if ($collection) {
             $defaultAction = 'c' . $defaultAction;
         }
         $controller = self::DEFAULT_CONTROLLER . ':' . $defaultAction;
         // Populate route name
         if (null === $routeName) {
             $routeName = self::ROUTE_NAME_PREFIX . self::$inflectorCache[$shortName] . '_' . $defaultAction;
         }
     }
     return new Operation(new Route($path, ['_controller' => $controller, '_resource' => $shortName], [], [], '', [], $methods), $routeName, $context);
 }
 /**
  * Creates operation.
  *
  * @param ResourceInterface $resource
  * @param bool              $collection
  * @param string|array      $methods
  * @param string|null       $path
  * @param string|null       $controller
  * @param string|null       $routeName
  * @param array             $context
  *
  * @return Operation
  */
 private function createOperation(ResourceInterface $resource, $collection, $methods, $path = null, $controller = null, $routeName = null, array $context = [])
 {
     $shortName = $resource->getShortName();
     if (!isset(self::$inflectorCache[$shortName])) {
         self::$inflectorCache[$shortName] = Inflector::pluralize(Inflector::tableize($shortName));
     }
     // Populate path
     if (null === $path) {
         $path = '/' . self::$inflectorCache[$shortName];
         if (!$collection) {
             $path .= '/{id}';
         }
     }
     // Guess default method
     if (is_array($methods)) {
         $defaultMethod = $methods[0];
     } else {
         $defaultMethod = $methods;
     }
     // Populate controller
     if (null === $controller) {
         $actionName = sprintf('%s_%s', strtolower($defaultMethod), $collection ? 'collection' : 'item');
         $controller = self::DEFAULT_ACTION_PATTERN . $actionName;
         // Populate route name
         if (null === $routeName) {
             $routeName = sprintf('%s%s_%s', self::ROUTE_NAME_PREFIX, self::$inflectorCache[$shortName], $actionName);
         }
     }
     return new Operation(new Route($path, ['_controller' => $controller, '_resource' => $shortName], [], [], '', [], $methods), $routeName, $context);
 }
 /**
  * @param ResourceInterface $resourceParent
  *
  * @return $this
  */
 public function setResourceParent($resourceParent)
 {
     $this->resourceParent = $resourceParent;
     if (null !== $this->resourceParent && null === $this->parentName) {
         $this->parentName = $this->resourceParent->getShortName();
     }
     return $this;
 }
Exemple #5
0
 /**
  * @param                   $object
  * @param ResourceInterface $dunglasResource
  * @param Manager           $fractalManager
  * @param Request|null      $request
  * @param bool              $defaultIncludes
  *
  * @return array
  * @throws \Exception
  */
 public function normalize($object, ResourceInterface $dunglasResource, Manager $fractalManager, Request $request = null, $defaultIncludes = true)
 {
     $transformer = $this->transformerHelper->getTransformer($dunglasResource->getShortName());
     if (null !== $request) {
         $fractalManager->parseIncludes($this->getEmbedsWithoutOptions($transformer, $request));
     }
     $resource = new Item($object, $transformer);
     if ($object instanceof Paginator || $object instanceof PersistentCollection) {
         $resource = new Collection($object, $transformer);
         if ($fractalManager->getSerializer() instanceof ArraySerializer) {
             $resource->setPaginator(new DunglasPaginatorAdapter($object, $resource));
         }
     }
     $rootScope = $fractalManager->createData($resource, $dunglasResource->getShortName());
     if ($defaultIncludes === false) {
         $transformer->setDefaultIncludes([]);
     }
     $transformer->setCurrentScope($rootScope)->setEmbed($dunglasResource->getShortName());
     return $rootScope->toArray();
 }
 /**
  * {@inheritdoc}
  */
 public function add(ResourceInterface $resource)
 {
     $entityClass = $resource->getEntityClass();
     if (isset($this->entityClassIndex[$entityClass])) {
         throw new \InvalidArgumentException(sprintf('A Resource class already exists for "%s".', $entityClass));
     }
     $shortName = $resource->getShortName();
     if (isset($this->shortNameIndex[$shortName])) {
         throw new \InvalidArgumentException(sprintf('A Resource class with the short name "%s" already exists.', $shortName));
     }
     $this->append($resource);
     $this->entityClassIndex[$entityClass] = $resource;
     $this->shortNameIndex[$shortName] = $resource;
 }
 /**
  * @param $data
  * @param $dataToSerialize
  */
 protected function setEmbed($data, $dataToSerialize)
 {
     if (isset($data['tags']['embed']) && isset($data['tags']['collection'])) {
         $filter = new EmbedFilter($this->managerRegistry, $this->propertyAccessor);
         $item = $dataToSerialize->getIterator()->current();
         if (null === ($params = $this->apiResource->getRouteKeyParams($item))) {
             $params['id'] = $this->propertyAccessor->getValue($item, 'id');
         }
         $params['embed'] = $this->apiResource->getShortName();
         $filter->setParameters($params);
         $filter->setRouteName($data['routeName']);
         $this->apiResource->addFilter($filter);
     }
 }
 /**
  * Creates operation.
  *
  * @param ResourceInterface $resource
  * @param bool              $collection
  * @param string|array      $methods
  * @param string|null       $path
  * @param null              $controller
  * @param null              $routeName
  * @param array             $context
  *
  * @return Operation
  */
 private function createOperation(ResourceInterface $resource, $collection, $methods, $path = null, $controller = null, $routeName = null, array $context = [])
 {
     $shortName = $resource->getShortName();
     if (!isset(self::$inflectorCache[$shortName])) {
         self::$inflectorCache[$shortName] = Inflector::pluralize(Inflector::tableize($shortName));
     }
     // Populate path
     if (!$path) {
         $path = '/' . self::$inflectorCache[$shortName];
         if (!$collection) {
             $path .= '/{id}';
         }
     }
     // Guess default method
     if (is_array($methods)) {
         $defaultMethod = $methods[0];
     } else {
         $defaultMethod = $methods;
     }
     // Populate controller
     if (!$controller) {
         $defaultAction = strtolower($defaultMethod);
         if ($collection) {
             $defaultAction = 'c' . $defaultAction;
         }
         $controller = self::DEFAULT_CONTROLLER . ':' . $defaultAction;
         // Populate route name
         if (!$routeName) {
             $routeName = self::$inflectorCache[$shortName] . '_' . $defaultAction;
         }
     }
     $requirements = [];
     if (strpos($path, '{id}')) {
         $requirements['id'] = '\\d+';
     }
     if (strpos($path, '{embed}')) {
         try {
             $embeds = $this->transformerHelper->getAvailableIncludes($shortName);
             $requirements['embed'] = implode('|', $embeds);
         } catch (\Exception $ex) {
             //commande sfroute symfony
         }
     }
     //        $requirements ['"context.getApiVersion() === '".$apiVersion."'"']
     return new Operation(new Route($path, ['_controller' => $controller, '_resource' => $shortName], $requirements, [], '', [], $methods), self::ROUTE_NAME_PREFIX . $resource->getVersion() . '_' . $routeName, $context);
 }
 /**
  * Gets the context URI for the given resource.
  *
  * @param ResourceInterface $resource
  *
  * @return string
  */
 public function getContextUri(ResourceInterface $resource)
 {
     return $this->router->generate('api_jsonld_context', ['shortName' => $resource->getShortName()]);
 }
 /**
  * Gets and populates if applicable a Hydra operation.
  *
  * @param ResourceInterface  $resource
  * @param OperationInterface $operation
  * @param string             $prefixedShortName
  * @param bool               $collection
  *
  * @return array
  */
 private function getHydraOperation(ResourceInterface $resource, OperationInterface $operation, $prefixedShortName, $collection)
 {
     $method = $operation->getRoute()->getMethods();
     if (is_array($method)) {
         $method = $method[0];
     }
     $hydraOperation = $operation->getContext();
     switch ($method) {
         case 'GET':
             if ($collection) {
                 if (!isset($hydraOperation['hydra:title'])) {
                     $hydraOperation['hydra:title'] = sprintf('Retrieves the collection of %s resources.', $resource->getShortName());
                 }
                 if (!isset($hydraOperation['returns'])) {
                     $hydraOperation['returns'] = 'hydra:PagedCollection';
                 }
             } else {
                 if (!isset($hydraOperation['hydra:title'])) {
                     $hydraOperation['hydra:title'] = sprintf('Retrieves %s resource.', $resource->getShortName());
                 }
             }
             break;
         case 'POST':
             if (!isset($hydraOperation['@type'])) {
                 $hydraOperation['@type'] = 'hydra:CreateResourceOperation';
             }
             if (!isset($hydraOperation['hydra:title'])) {
                 $hydraOperation['hydra:title'] = sprintf('Creates a %s resource.', $resource->getShortName());
             }
             break;
         case 'PUT':
             if (!isset($hydraOperation['@type'])) {
                 $hydraOperation['@type'] = 'hydra:ReplaceResourceOperation';
             }
             if (!isset($hydraOperation['hydra:title'])) {
                 $hydraOperation['hydra:title'] = sprintf('Replaces the %s resource.', $resource->getShortName());
             }
             break;
         case 'DELETE':
             if (!isset($hydraOperation['hydra:title'])) {
                 $hydraOperation['hydra:title'] = sprintf('Deletes the %s resource.', $resource->getShortName());
             }
             if (!isset($hydraOperation['returns'])) {
                 $hydraOperation['returns'] = 'owl:Nothing';
             }
             break;
     }
     if (!isset($hydraOperation['returns']) && ('GET' === $method && !$collection || 'POST' === $method || 'PUT' === $method)) {
         $hydraOperation['returns'] = $prefixedShortName;
     }
     if (!isset($hydraOperation['expects']) && ('POST' === $method || 'PUT' === $method)) {
         $hydraOperation['expects'] = $prefixedShortName;
     }
     if (!isset($hydraOperation['@type'])) {
         $hydraOperation['@type'] = 'hydra:Operation';
     }
     if (!isset($hydraOperation['hydra:method'])) {
         $hydraOperation['hydra:method'] = $method;
     }
     if (!isset($hydraOperation['rdfs:label']) && isset($hydraOperation['hydra:title'])) {
         $hydraOperation['rdfs:label'] = $hydraOperation['hydra:title'];
     }
     ksort($hydraOperation);
     return $hydraOperation;
 }
 /**
  * Builds ApiDoc annotation from DunglasApiBundle data.
  *
  * @param bool               $collection
  * @param ResourceInterface  $resource
  * @param OperationInterface $operation
  * @param array              $resourceHydraDoc
  * @param array              $entrypointHydraDoc
  *
  * @return ApiDoc
  */
 private function getApiDoc($collection, ResourceInterface $resource, OperationInterface $operation, array $resourceHydraDoc, array $entrypointHydraDoc = [])
 {
     $method = $operation->getRoute()->getMethods()[0];
     if ($collection) {
         $operationHydraDoc = $this->getCollectionOperationHydraDoc($resource->getShortName(), $method, $entrypointHydraDoc);
     } else {
         $operationHydraDoc = $this->getOperationHydraDoc($operation->getRoute()->getMethods()[0], $resourceHydraDoc);
     }
     $route = $operation->getRoute();
     $data = ['resource' => $route->getPath(), 'description' => $operationHydraDoc['hydra:title'], 'resourceDescription' => $resourceHydraDoc['hydra:title'], 'section' => $resourceHydraDoc['hydra:title']];
     $entityClass = $resource->getEntityClass();
     if (isset($operationHydraDoc['expects']) && 'owl:Nothing' !== $operationHydraDoc['expects']) {
         $data['input'] = sprintf('%s:%s', DunglasApiParser::IN_PREFIX, $entityClass);
     }
     if (isset($operationHydraDoc['returns']) && 'owl:Nothing' !== $operationHydraDoc['returns']) {
         $data['output'] = sprintf('%s:%s', DunglasApiParser::OUT_PREFIX, $entityClass);
     }
     if (Request::METHOD_GET === $method && $collection) {
         $data['filters'] = [];
         foreach ($resource->getFilters() as $filter) {
             foreach ($filter->getDescription($resource) as $name => $definition) {
                 $data['filters'][] = ['name' => $name] + $definition;
             }
         }
     }
     $apiDoc = new ApiDoc($data);
     $apiDoc->setRoute($route);
     return $apiDoc;
 }
 /**
  * Bootstrap a serialization context with the given resource.
  *
  * @param ResourceInterface $resource
  * @param array             $context
  *
  * @return array [array, array]
  */
 public function bootstrap(ResourceInterface $resource, array $context = [])
 {
     $data = [];
     if (!isset($context['json_ld_has_context'])) {
         $data['@context'] = $this->router->generate('api_json_ld_context', ['shortName' => $resource->getShortName()]);
         $context['json_ld_has_context'] = true;
     }
     return [$context, $data];
 }
 /**
  * Gets and populates if applicable a Hydra operation.
  *
  * @param ResourceInterface  $resource
  * @param OperationInterface $operation
  * @param string             $prefixedShortName
  * @param bool               $collection
  *
  * @return array
  */
 protected function getHydraOperation(ResourceInterface $resource, OperationInterface $operation, $prefixedShortName, $collection)
 {
     $method = $operation->getRoute()->getMethods();
     if (is_array($method)) {
         // If all methods are allowed, default to GET
         $method = isset($method[0]) ? $method[0] : 'GET';
     }
     $methodDoc = $this->documentationHelper->getReflectionMethod($operation->getRoute()->getDefault('_controller'));
     $annotation = $methodDoc !== null ? $this->documentationHelper->getMethodAnnotation($methodDoc) : null;
     $hydraOperation = $operation->getContext();
     $hydraOperation['hydra:entrypoint'] = $operation->getRoute()->getPath();
     switch ($method) {
         case 'GET':
             if ($collection) {
                 if (!isset($hydraOperation['hydra:title'])) {
                     $hydraOperation['hydra:title'] = sprintf('Retrieves the collection of %s resources.', $resource->getShortName());
                 }
                 if (!isset($hydraOperation['returns'])) {
                     $hydraOperation['returns'] = 'hydra:PagedCollection';
                 }
                 foreach ($resource->getFilters() as $filter) {
                     foreach ($filter->getDescription($resource) as $key => $value) {
                         $hydraOperation["hydra:search"][$key] = ['requirement' => '[a-zA-Z0-9-]+', 'description' => $key . ' filter', 'default' => ''];
                     }
                 }
             } else {
                 if (!isset($hydraOperation['hydra:title'])) {
                     $hydraOperation['hydra:title'] = null !== $annotation && null !== $annotation->getDescription() ? $annotation->getDescription() : sprintf('Retrieves %s resource.', $resource->getShortName());
                 }
                 if (null !== $annotation) {
                     $hydraOperation['returns'] = $annotation->getOutput();
                 }
             }
             break;
         case 'POST':
             if (!isset($hydraOperation['@type'])) {
                 $hydraOperation['@type'] = 'hydra:CreateResourceOperation';
             }
             if (!isset($hydraOperation['hydra:title'])) {
                 $hydraOperation['hydra:title'] = sprintf('Creates a %s resource.', $resource->getShortName());
             }
             break;
         case 'PUT':
             if (!isset($hydraOperation['@type'])) {
                 $hydraOperation['@type'] = 'hydra:ReplaceResourceOperation';
             }
             if (!isset($hydraOperation['hydra:title'])) {
                 $hydraOperation['hydra:title'] = sprintf('Replaces the %s resource.', $resource->getShortName());
             }
             break;
         case 'DELETE':
             if (!isset($hydraOperation['hydra:title'])) {
                 $hydraOperation['hydra:title'] = sprintf('Deletes the %s resource.', $resource->getShortName());
             }
             if (!isset($hydraOperation['returns'])) {
                 $hydraOperation['returns'] = 'owl:Nothing';
             }
             break;
     }
     if (!isset($hydraOperation['returns']) && ('GET' === $method && !$collection || 'POST' === $method || 'PUT' === $method)) {
         $hydraOperation['returns'] = $prefixedShortName;
     }
     if (!isset($hydraOperation['expects']) && ('POST' === $method || 'PUT' === $method)) {
         $hydraOperation['expects'] = $prefixedShortName;
     }
     if (!isset($hydraOperation['@type'])) {
         $hydraOperation['@type'] = 'hydra:Operation';
     }
     if (!isset($hydraOperation['hydra:method'])) {
         $hydraOperation['hydra:method'] = $method;
     }
     if (!isset($hydraOperation['rdfs:label']) && isset($hydraOperation['hydra:title'])) {
         $hydraOperation['rdfs:label'] = $hydraOperation['hydra:title'];
     }
     ksort($hydraOperation);
     return $hydraOperation;
 }