/** * @param Request $request * @param $id * @param $embed * * @return ResourceInterface */ public function initFilterEmbed(Request $request, $id, $embed) { $embedShortname = ucwords(Inflector::singularize($embed)); /** @var $resourceEmbed ResourceInterface */ $resourceEmbed = $this->resourceResolver->getResourceForShortName($embedShortname, $this->router->getContext()->getApiVersion()); $filter = new EmbedFilter($this->managerRegistry, $this->propertyAccessor); $params = !$request->request->has('embedParams') ? ['embed' => $embed, 'id' => $id] : $request->request->get('embedParams'); $filter->setParameters($params); $filter->setRouteName($request->get('_route')); $resourceEmbed->addFilter($filter); return $resourceEmbed; }
private function renderResourceSection($section, array $resources) { $markdown = ''; if ('_others' !== $section) { $markdown = sprintf("# %s #\n\n", $section); } foreach ($resources as $resource => $methods) { $this->resourceName = strtolower($section); if (!($this->apiResource = $this->resourceCollection->getResourceForShortName(ucfirst($this->resourceName), 'v2'))) { throw new \InvalidArgumentException(sprintf('The resource "%s" cannot be found.', ucfirst($this->resourceName))); } if ('_others' === $section && 'others' !== $resource) { $markdown .= sprintf("## %s ##\n\n", $resource); } elseif ('others' !== $resource) { $markdown .= sprintf("## %s ##\n\n", $resource); } foreach ($methods as $method) { $basePath = $this->currentPath . '/resources/' . $this->resourceName; $method['methodePath'] = $basePath . '/' . strtolower($method['method']); if (isset($method['tags']['collection'])) { $method['methodePath'] = $basePath . '/collection/' . strtolower($method['method']); } $this->fs->mkdir($method['methodePath']); $markdown .= $this->renderOne($method); $markdown .= "\n"; } } return $markdown; }
/** * 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; }