Пример #1
0
 /**
  * Returns an array of data where each data is an array with the following keys:
  *  - annotation
  *  - resource
  *
  * @param array  $routes array of Route-objects for which the annotations should be extracted
  * @param string $view
  *
  * @return array
  */
 public function extractAnnotations(array $routes, $view = 'default')
 {
     foreach ($routes as &$route) {
         if (!$route instanceof Route) {
             throw new \InvalidArgumentException(sprintf('All elements of $routes must be instances of Route. "%s" given', gettype($route)));
         }
         if (!$route->getDefault('_defaultHandler')) {
             $route = clone $route;
             $parameters = new ParameterBag($route->getDefaults());
             $route->setDefault('_controller', \Controller::getController($parameters)->getAction());
         }
     }
     return parent::extractAnnotations($routes);
 }
Пример #2
0
 /**
  * Returns an array of data where each data is an array with the following keys:
  *  - annotation
  *  - resource
  *
  * @param array $routes array of Route-objects for which the annotations should be extracted
  *
  * @param $view
  * @return array
  */
 public function extractAnnotations(array $routes, $view = parent::DEFAULT_VIEW)
 {
     $array = array();
     $resources = array();
     $excludeSections = $this->container->getParameter('nelmio_api_doc.exclude_sections');
     if (in_array(strtolower($this->versionApi), $this->nelmioDocStandardVersion)) {
         return parent::extractAnnotations($routes, $view);
     }
     $this->transformerHelper->setVersion($this->versionApi);
     $routesName = [];
     foreach ($routes as $name => $route) {
         $routeName = $name;
         if (!$route instanceof Route) {
             throw new \InvalidArgumentException(sprintf('All elements of $routes must be instances of Route. "%s" given', gettype($route)));
         }
         if (is_null($route->getDefault('_resource'))) {
             continue;
         }
         if ($method = $this->getReflectionMethod($route->getDefault('_controller'))) {
             $annotation = $this->reader->getMethodAnnotation($method, self::ANNOTATION_CLASS);
             if ($annotation && !in_array($annotation->getSection(), $excludeSections)) {
                 if ($annotation->isResource()) {
                     if ($resource = $annotation->getResource()) {
                         $resources[] = $resource;
                     } else {
                         // remove format from routes used for resource grouping
                         $resources[] = str_replace('.{_format}', '', $route->getPattern());
                     }
                 }
                 $path = $route->getPath();
                 if (false === strstr($path, '{embed}')) {
                     $dunglasResource = $this->resourceCollection->getResourceForShortName($route->getDefault('_resource'), $this->versionApi);
                     $array[] = ['annotation' => $this->extractData($annotation, $route, $method, $dunglasResource)];
                     continue;
                 }
                 $availableIncludes = $this->transformerHelper->getAvailableIncludes($route->getDefault('_resource'));
                 foreach ($availableIncludes as $include) {
                     $route->setPath(str_replace('{embed}', $include, $path));
                     $name = Inflector::singularize($include);
                     $dunglasResource = $this->resourceCollection->getResourceForShortName(ucfirst($name), $this->versionApi);
                     if (null === $dunglasResource) {
                         $dunglasResource = $this->resourceCollection->getResourceForShortName(ucfirst($include), $this->versionApi);
                     }
                     $route->addDefaults(['_resource' => $dunglasResource->getShortName()]);
                     $apiDoc = $this->extractData($annotation, $route, $method, $dunglasResource);
                     $routesNames[$apiDoc->getRoute()->getPattern()] = $routeName;
                     $array[] = ['annotation' => $apiDoc];
                 }
             }
         }
     }
     rsort($resources);
     foreach ($array as $index => $element) {
         $hasResource = false;
         $pattern = $element['annotation']->getRoute()->getPattern();
         $routeName = isset($routesNames[$pattern]) ? $routesNames[$pattern] : null;
         foreach ($resources as $resource) {
             if (0 === strpos($pattern, $resource) || $resource === $element['annotation']->getResource()) {
                 $data = false !== $element['annotation']->getResource() ? $resource : $pattern;
                 $array[$index]['resource'] = $data;
                 $hasResource = true;
                 break;
             }
         }
         if (false === $hasResource) {
             $array[$index]['resource'] = 'others';
         }
         $array[$index]['routeName'] = $routeName;
     }
     $methodOrder = array('GET', 'POST', 'PUT', 'DELETE');
     usort($array, function ($a, $b) use($methodOrder) {
         if ($a['resource'] === $b['resource']) {
             if ($a['annotation']->getRoute()->getPattern() === $b['annotation']->getRoute()->getPattern()) {
                 $methodA = array_search($a['annotation']->getRoute()->getRequirement('_method'), $methodOrder);
                 $methodB = array_search($b['annotation']->getRoute()->getRequirement('_method'), $methodOrder);
                 if ($methodA === $methodB) {
                     return strcmp($a['annotation']->getRoute()->getRequirement('_method'), $b['annotation']->getRoute()->getRequirement('_method'));
                 }
                 return $methodA > $methodB ? 1 : -1;
             }
             return strcmp($a['annotation']->getRoute()->getPattern(), $b['annotation']->getRoute()->getPattern());
         }
         return strcmp($a['resource'], $b['resource']);
     });
     return $array;
 }